This tutorial shows how to handle sessions on Cloud Run.
Many apps need session handling for authentication and user preferences.
The Gorilla Web Toolkit
sessions
package comes with a file system based implementation to perform this
function. However, this implementation is unsuitable for an app that can be served
from multiple instances, because the session that is recorded in one instance
might differ from other instances. The
gorilla/sessions
package also comes with a cookie-based implementation. But, this implementation
requires encrypting cookies and storing the entire session on the client, rather
than just a session ID, which may be too large for some apps.
Setting up the project
In your terminal window, clone the sample app repository to your local machine:
git clone https://github.com/GoogleCloudPlatform/golang-samples.git
Change to the directory that contains the sample code:
cd golang-samples/getting-started/sessions
Understanding the web app
This app displays greetings in different languages for every user. Returning users are always greeted in the same language.
Before the app can store preferences for a user, you need a way to store information about the current user in a session. This sample app uses Firestore to store session data.
The app starts by importing dependencies, defining an
app
type to hold asessions.Store
and an HTML template, and defining the list of greetings.Next, the app defines a
main
function, which creates a newapp
instance, registers the index handler, and starts the HTTP server. ThenewApp
function creates theapp
instance by settingprojectID
andcollectionID
values and parses the HTML template.The index handler gets the user's session, creating one if needed. New sessions are assigned a random language and a view count of 0. Then, the view count is increased by one, the session is saved, and the HTML template writes the response.
The following diagram illustrates how Firestore handles sessions for the Cloud Run app.
Deleting sessions
You can delete session data in the Cloud de Confiance console or implement an automated deletion strategy. If you use storage solutions for sessions such as Memcache or Redis, expired sessions are automatically deleted.
Running locally
In your terminal window, build the
sessions
binary:go build
Start the HTTP server:
./sessions
View the app in your web browser:
Cloud Shell
In the Cloud Shell toolbar, click Web preview
and select Preview on port 8080.
Local machine
In your browser, go to
http://localhost:8080
You see one of five greetings: “Hello World”, “Hallo Welt”, "Hola mundo”, “Salut le Monde”, or “Ciao Mondo.” The language changes if you open the page in a different browser or in incognito mode. You can see and edit the session data in the Cloud de Confiance console.
To stop the HTTP server, in your terminal window, press
Control+C
.
Deploying and running on Cloud Run
You can use the Cloud Run to build and deploy an app that runs reliably under heavy load and with large amounts of data.
- Deploy the app on Cloud Run:
gcloud run deploy firestore-tutorial-go
--source . --allow-unauthenticated --port=8080
--set-env-vars=GOOGLE_CLOUD_PROJECT=YOUR_PROJECT_ID - Visit the URL returned by this command to see how session data persists between page loads.
The greeting is now delivered by a web server running on an Cloud Run instance.
Debugging the app
If you cannot connect to your Cloud Run app, check the following:
- Check that the
gcloud
deploy commands successfully completed and didn't output any errors. If there were errors (for example,message=Build failed
), fix them, and try deploying the Cloud Run app again. In the Cloud de Confiance console, go to the Logs Explorer page.
In the Recently selected resources drop-down list, click Cloud Run Application, and then click All module_id. You see a list of requests from when you visited your app. If you don't see a list of requests, confirm you selected All module_id from the drop-down list. If you see error messages printed to the Cloud de Confiance console, check that your app's code matches the code in the section about writing the web app.
Make sure that the Firestore API is enabled.