一塊兒學context(一)——上下文值傳遞

系列開篇

本文開始將針對context的用法進行系統化討論,在這裏你將可以在工做中合理使用context解決一些比較棘手的問題。golang

context處理超時處理以外還能夠用來保存數據,當你須要在多個上下文傳遞時傳遞數據,那麼本文提到的知識能夠排上用場。算法

示例代碼

示例代碼爲一個簡單的http服務,流程是登陸以後會跳轉首頁,首頁經過guard中間件進行鑑權。固然,示例代碼未作其餘諸如鏈接數據庫之類的處理,這不是本文的重點。
守衛函數讀取cookie以後將cookie值寫入context並向下傳遞,在整個請求中能夠說是「透明」的。當訪問到須要保護的接口時檢測到沒有提供cookie,則直接終端請求,不然經過r.WithContext將username的值存入cookie,避免的業務接口直接讀取cookie的弊端。由於若是後期更改鑑權算法的話,業務代碼能夠不用更改,直接更改中間件便可。數據庫

package main

import (
    "context"
    "fmt"
    "log"
    "net/http"
    "time"
)

func main() {
    mux := http.NewServeMux()
    mux.HandleFunc("/", guard(home))
    mux.HandleFunc("/login", login)
    log.Fatal(http.ListenAndServe(":8080", mux))
}

// 登陸
func login(w http.ResponseWriter, r *http.Request) {
    if r.URL.Query().Get("username") != "root" {
        http.Error(w, http.StatusText(401), 401)
        return
    }
    cookie := &http.Cookie{Name: "username", Value: "root", Expires: time.Now().Add(time.Hour)}
    http.SetCookie(w, cookie)
    http.Redirect(w, r, "/", 302)
}

func home(w http.ResponseWriter, r *http.Request) {
    username := r.Context().Value("username")
    fmt.Fprintf(w, "welcome login: %s", username.(string))
}

// 守衛
func guard(handleFunc http.HandlerFunc) http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
        // check username
        log.Printf("%s - %s\n", r.Method, r.RequestURI)
        cookie, err := r.Cookie("username")
        if err != nil || cookie == nil { // 若是username爲空直接攔截
            http.Error(w, http.StatusText(401), 401)
            return
        }
        handleFunc(w, r.WithContext(context.WithValue(r.Context(), "username", cookie.Value)))
    }
}

本文的代碼就這麼多,內容也不多,但願你們能好好用上這個利器。
關於context與協程超時控制將在下一篇文章中講到。cookie

個人博客

天天進步一點點,歡迎你們做客!函數

相關文章
相關標籤/搜索