使用 Firestore 處理工作階段

本教學課程說明如何在 Cloud Run 處理工作階段。

許多應用程式都需要處理工作階段,以便進行驗證及設定使用者偏好設定。Gorilla Web Toolkit sessions 套件隨附以檔案系統為基礎的實作,可執行這項功能。不過,如果應用程式可由多個執行個體提供服務,則不適合採用這種實作方式,因為在某個執行個體中記錄的工作階段可能與其他執行個體不同。gorilla/sessions 套件也提供以 Cookie 為基礎的實作。不過,這種做法需要加密 Cookie,並將整個工作階段 (而非僅工作階段 ID) 儲存在用戶端,對某些應用程式來說可能過大。

目標

  • 編寫應用程式。
  • 在本機執行應用程式。
  • 在 Cloud Run 部署應用程式。

費用

在本文件中,您會使用下列 Google Cloud計費元件:

您可以使用 Pricing Calculator,根據預測用量估算費用。

初次使用 Google Cloud 的使用者可能符合免費試用期資格。

完成本文所述工作後,您可以刪除建立的資源,避免繼續計費,詳情請參閱「清除所用資源」。

事前準備

  1. 登入 Google Cloud 帳戶。如果您未曾使用過 Google Cloud,歡迎建立帳戶,親自體驗實際使用我們產品的成效。新客戶還能獲得價值 $300 美元的免費抵免額,用於執行、測試及部署工作負載。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Firestore API, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Firestore API, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  8. 在 Google Cloud 控制台的 Cloud Shell 中開啟應用程式。

    前往 Cloud Shell

    Cloud Shell 可讓您直接在瀏覽器中使用指令列存取雲端資源。在瀏覽器中開啟 Cloud Shell,然後按一下「Proceed」(繼續),下載程式碼範例並切換至應用程式目錄。

  9. 在 Cloud Shell 中,將 gcloud CLI 設為使用新的 Google Cloud 專案:
    # Configure gcloud for your project
    gcloud config set project YOUR_PROJECT_ID
    

設定專案

  1. 在終端機視窗中,將範例應用程式存放區複製到本機電腦:

    git clone https://github.com/GoogleCloudPlatform/golang-samples.git
  2. 變更為包含程式碼範例的目錄:

    cd golang-samples/getting-started/sessions

瞭解網頁應用程式

這個應用程式為每個使用者顯示不同語言的問候語。系統一律會用與上次相同的語言問候回訪者。

多個應用程式視窗會顯示不同語言的問候語。

應用程式必須先在工作階段中儲存目前使用者的資訊,才能儲存使用者的偏好設定。這個範例應用程式使用 Firestore 儲存工作階段資料。

  1. 應用程式會先匯入依附元件、定義 app 類型來保存 sessions.Store 和 HTML 範本,並定義問候語清單。

    import (
    	"context"
    	"html/template"
    	"log"
    	"math/rand"
    	"net/http"
    	"os"
    
    	"cloud.google.com/go/firestore"
    )
    
    // app stores a sessions.Store. Create a new app with newApp.
    type app struct {
    	tmpl         *template.Template
    	collectionID string
    	projectID    string
    }
    
    // session stores the client's session information.
    // This type is also used for executing the template.
    type session struct {
    	Greetings string `json:"greeting"`
    	Views     int    `json:"views"`
    }
    
    // greetings are the random greetings that will be assigned to sessions.
    var greetings = []string{
    	"Hello World",
    	"Hallo Welt",
    	"Ciao Mondo",
    	"Salut le Monde",
    	"Hola Mundo",
    }
    
  2. 接著,應用程式會定義 main 函式,建立新的 app 執行個體、註冊索引處理常式,並啟動 HTTP 伺服器。newApp 函式會設定 projectID 和 collectionID 值,並剖析 HTML 範本,藉此建立 app 執行個體。

    func main() {
    	port := os.Getenv("PORT")
    	if port == "" {
    		port = "8080"
    	}
    
    	projectID := os.Getenv("GOOGLE_CLOUD_PROJECT")
    	if projectID == "" {
    		log.Fatal("GOOGLE_CLOUD_PROJECT must be set")
    	}
    
    	// collectionID is a non-empty identifier for this app, it is used as the Firestore
    	// collection name that stores the sessions.
    	//
    	// Set it to something more descriptive for your app.
    	collectionID := "hello-views"
    
    	a, err := newApp(projectID, collectionID)
    	if err != nil {
    		log.Fatalf("newApp: %v", err)
    	}
    
    	http.HandleFunc("/", a.index)
    
    	log.Printf("Listening on port %s", port)
    	if err := http.ListenAndServe(":"+port, nil); err != nil {
    		log.Fatal(err)
    	}
    }
    
    // newApp creates a new app.
    func newApp(projectID, collectionID string) (app, error) {
    	tmpl, err := template.New("Index").Parse(`<body>{{.Views}} {{if eq .Views 1}}view{{else}}views{{end}} for "{{.Greetings}}"</body>`)
    	if err != nil {
    		log.Fatalf("template.New: %v", err)
    	}
    
    	return app{
    		tmpl:         tmpl,
    		collectionID: collectionID,
    		projectID:    projectID,
    	}, nil
    }
    
  3. 索引處理常式會取得使用者的工作階段,並視需要建立工作階段。新工作階段會隨機指派語言,觀看次數為 0。接著,觀看次數會增加一次,工作階段會儲存,HTML 範本會寫入回應。

    
    // index uses sessions to assign users a random greeting and keep track of
    // views.
    func (a *app) index(w http.ResponseWriter, r *http.Request) {
    	// Allows requests only for the root path ("/") to prevent duplicate calls.
    	if r.RequestURI != "/" {
    		http.NotFound(w, r)
    		return
    	}
    
    	var session session
    	var doc *firestore.DocumentRef
    
    	isNewSession := false
    
    	ctx := context.Background()
    
    	client, err := firestore.NewClient(ctx, a.projectID)
    	if err != nil {
    		log.Fatalf("firestore.NewClient: %v", err)
    	}
    	defer client.Close()
    
    	// cookieName is a non-empty identifier for this app, it is used as the key name
    	// that contains the session's id value.
    	//
    	// Set it to something more descriptive for your app.
    	cookieName := "session_id"
    
    	// If err is different to nil, it means the cookie has not been set, so it will be created.
    	cookie, err := r.Cookie(cookieName)
    	if err != nil {
    		// isNewSession flag is set to true
    		isNewSession = true
    	}
    
    	// If isNewSession flag is true, the session will be created
    	if isNewSession {
    		// Get unique id for new document
    		doc = client.Collection(a.collectionID).NewDoc()
    
    		session.Greetings = greetings[rand.Intn(len(greetings))]
    		session.Views = 1
    
    		// Cookie is set
    		cookie = &http.Cookie{
    			Name:     cookieName,
    			Value:    doc.ID,
    			HttpOnly: true,                 // Prevents client-side scripts from accessing the cookie
    			SameSite: http.SameSiteLaxMode, // Protects the session cookie from Cross-Site Request Forgery (CSRF) attacks
    		}
    		http.SetCookie(w, cookie)
    	} else {
    		// The session exists
    
    		// Retrieve document from collection by ID
    		docSnapshot, err := client.Collection(a.collectionID).Doc(cookie.Value).Get(ctx)
    		if err != nil {
    			log.Printf("doc.Get error: %v", err)
    			http.Error(w, "Error getting session", http.StatusInternalServerError)
    			return
    		}
    
    		// Unmarshal documents's content to local type
    		err = docSnapshot.DataTo(&session)
    		if err != nil {
    			log.Printf("doc.DataTo error: %v", err)
    			http.Error(w, "Error parsing session", http.StatusInternalServerError)
    			return
    		}
    
    		doc = docSnapshot.Ref
    
    		// Add 1 to current views value
    		session.Views++
    	}
    
    	// The document is created/updated
    	_, err = doc.Set(ctx, session)
    	if err != nil {
    		log.Printf("doc.Set error: %v", err)
    		http.Error(w, "Error creating session", http.StatusInternalServerError)
    		return
    	}
    
    	if err := a.tmpl.Execute(w, session); err != nil {
    		log.Printf("Execute: %v", err)
    	}
    }
    

    下圖說明 Firestore 如何處理 Cloud Run 應用程式的工作階段。

    架構圖:使用者、Cloud Run、Firestore。

刪除工作階段

您可以在Google Cloud 控制台中刪除工作階段資料,或實施自動刪除策略。如果您使用 Memcache 或 Redis 等工作階段儲存空間解決方案,系統會自動刪除過期的工作階段。

在本機環境中執行

  1. 在終端機視窗中建構 sessions 二進位檔:

    go build
    
  2. 啟動 HTTP 伺服器:

    ./sessions
    
  3. 在網路瀏覽器中查看應用程式:

    Cloud Shell

    在 Cloud Shell 工具列中,按一下 [Web preview] (網頁預覽) 網頁預覽,然後選取 [Preview on port 8080] (透過以下通訊埠預覽:8080)。

    本機電腦

    透過瀏覽器前往以下網址:http://localhost:8080

    您會看到以下五種問候語之一:「Hello World」、「Hallo Welt」、「Hola mundo」、「Salut le Monde」或「Ciao Mondo」。如果您在其他瀏覽器或無痕模式中開啟頁面,語言就會變更。您可以在Google Cloud 控制台中查看及編輯工作階段資料。

     Google Cloud 控制台中的 Firestore 工作階段。

  4. 如要停止 HTTP 伺服器,請在您的終端機視窗中,按下 Control+C。

在 Cloud Run 部署及執行

您可以使用 Cloud Run 建構及部署應用程式,即使面對大量負載與資料,應用程式仍會穩定可靠地執行。

  1. 將應用程式部署至 Cloud Run:
        gcloud run deploy firestore-tutorial-go 
    --source . --allow-unauthenticated --port=8080
    --set-env-vars=GOOGLE_CLOUD_PROJECT=YOUR_PROJECT_ID
  2. 造訪這個指令傳回的網址,瞭解工作階段資料如何在載入網頁之間保留。

問候語現在由 Cloud Run 執行個體上執行的網路伺服器傳送。

應用程式偵錯

如果無法連線至 Cloud Run 應用程式,請檢查下列事項:

  1. 檢查 gcloud 部署指令是否順利完成,且未輸出任何錯誤。如果發生錯誤 (例如 message=Build failed),請修正錯誤,然後再次嘗試部署 Cloud Run 應用程式。
  2. 前往 Google Cloud 控制台的「Logs Explorer」頁面。

    前往「Logs Explorer」頁面

    1. 在「Recently selected resources」(最近選取的資源) 下拉式選單中,依序點選「Cloud Run Application」(Cloud Run 應用程式) 和「All module_id」(所有 module_id)。您會看到造訪應用程式時產生的要求清單。如果沒有看到要求清單,請確認您已從下拉式選單中選取「All module_id」。如果 Google Cloud 控制台顯示錯誤訊息,請確認應用程式的程式碼與編寫網頁應用程式一節中的程式碼相符。

    2. 確認已啟用 Firestore API。

清除所用資源

刪除專案

  1. 前往 Google Cloud 控制台的「Manage resources」(管理資源) 頁面。

    前往「Manage resources」(管理資源)

  2. 在專案清單中選取要刪除的專案,然後點選「Delete」(刪除)。
  3. 在對話方塊中輸入專案 ID,然後按一下 [Shut down] (關閉) 以刪除專案。

刪除 Cloud Run 執行個體

  1. 在 Google Cloud 控制台中,前往 App Engine 的「Versions」(版本) 頁面。

    前往「版本」

  2. 找出要刪除的非預設應用程式版本,然後勾選對應的核取方塊。
  3. 如要刪除應用程式版本,請按一下「 刪除」。

後續步驟