一起学context(一)——上下文值传递

2次阅读

共计 1431 个字符,预计需要花费 4 分钟才能阅读完成。

系列开篇
本文开始将针对 context 的用法进行系统化讨论,在这里你将能够在工作中合理使用 context 解决一些比较棘手的问题。
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 与协程超时控制将在下一篇文章中讲到。
我的博客
每天进步一点点,欢迎大家作客!

正文完
 0