温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

context的上下文值如何进行传递

发布时间:2020-07-21 09:43:58 来源:亿速云 阅读:560 作者:Leah 栏目:编程语言

这篇文章运用简单易懂的例子给大家介绍context的上下文值如何进行传递,代码非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

本文开始将针对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的上下文值如何进行传递就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI