一个应用级别的变量使用示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
type appContext struct {
db *sql.DB
}
func (c *appContext) authHandler(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
authToken := r.Header.Get("Authorization")
user, err := getUser(c.db, authToken)
if err != nil {
http.Error(w, http.StatusText(401), 401)
return
}
context.Set(r, "user", user)
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
func (c *appContext) adminHandler(w http.ResponseWriter, r *http.Request) {
user := context.Get(r, "user")
// Maybe other operations on the database
json.NewEncoder(w).Encode(user)
}
func main() {
db := sql.Open("postgres", "...")
appC := appContext{db}
commonHandlers := alice.New(context.ClearHandler, loggingHandler, recoverHandler)
http.Handle("/admin", commonHandlers.Append(appC.authHandler).ThenFunc(appC.adminHandler))
http.Handle("/about", commonHandlers.ThenFunc(aboutHandler))
http.Handle("/", commonHandlers.ThenFunc(indexHandler))
http.ListenAndServe(":8080", nil)
}
参考文章
- https://www.nicolasmerouze.com/share-values-between-middlewares-context-golang/
- https://joeshaw.org/net-context-and-http-handler/
- https://elithrar.github.io/article/custom-handlers-avoiding-globals/
- https://groups.google.com/forum/#!topic/golang-nuts/pCB81QylF6M
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"encoding/json"
"fmt"
"net"
"net/http"
"time"
"golang.org/x/net/context"
)
type ContextHandler interface {
ServeHTTPContext(context.Context, http.ResponseWriter, *http.Request) error
}
type ContextHandlerFunc func(context.Context, http.ResponseWriter, *http.Request) error
func (f ContextHandlerFunc) ServeHTTPContext(ctx context.Context, w http.ResponseWriter, req *http.Request) error {
return f(ctx, w, req)
}
type ContextAdapter struct {
ctx context.Context
handler ContextHandler // to wrap func(context.Context, ResponseWriter, *Request) error
}
func (ca *ContextAdapter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if err := ca.handler.ServeHTTPContext(ca.ctx, w, req); err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{
"status": "error",
"message": err.Error(),
})
}
}
func NewRootContext(ts string) context.Context {
return context.WithValue(context.Background(), appStartTSKey, ts)
}
func main() {
rootContext := context.Background()
mainRouter := http.NewServeMux()
mainRouter.Handle("/", &ContextAdapter{
ctx: rootContext,
handler: ContextHandlerFunc(handlerRoot),
})
mainRouter.Handle("/set", &ContextAdapter{
ctx: rootContext,
handler: ContextHandlerFunc(handlerSet),
})
port := ":8080"
fmt.Println("Serving http://localhost" + port)
if err := http.ListenAndServe(port, mainRouter); err != nil {
panic(err)
}
}
func handlerRoot(ctx context.Context, w http.ResponseWriter, req *http.Request) error {
switch req.Method {
case "GET":
fmt.Println("handlerRoot")
ts, _ := getAppStartTSFromContext(ctx)
ip, _ := getIPFromContext(ctx)
ua, _ := getUserAgentFromContext(ctx)
fmt.Fprintf(w, "Root = AppStartTS: %s / IP: %v / UserAgent: %s", ts, ip, ua)
return nil
default:
http.Error(w, "Method Not Allowed", 405)
return fmt.Errorf("Method Not Allowed:", req.Method)
}
}
func handlerSet(ctx context.Context, w http.ResponseWriter, req *http.Request) error {
switch req.Method {
case "GET":
fmt.Println("handlerSet")
ts := time.Now().String()
ip, err := getIP(req)
if err != nil {
return err
}
ua := req.UserAgent()
ctx = setContextWithAppStartTS(ctx, ts)
ctx = setContextWithIP(ctx, ip)
ctx = setContextWithUserAgent(ctx, ua)
fmt.Fprintf(w, "Set = AppStartTS: %s / IP: %v / UserAgent: %s", ts, ip, ua)
return nil
default:
http.Error(w, "Method Not Allowed", 405)
return fmt.Errorf("Method Not Allowed:", req.Method)
}
}
type key int
const appStartTSKey key = 0
const userIPKey key = 1
const userAgentKey key = 2
func setContextWithAppStartTS(ctx context.Context, ts string) context.Context {
return context.WithValue(ctx, appStartTSKey, ts)
}
func setContextWithIP(ctx context.Context, userIP net.IP) context.Context {
return context.WithValue(ctx, userIPKey, userIP)
}
func setContextWithUserAgent(ctx context.Context, userAgent string) context.Context {
return context.WithValue(ctx, userAgentKey, userAgent)
}
func getAppStartTSFromContext(ctx context.Context) (string, bool) {
// ctx.Value returns nil if ctx has no value for the key;
// the string type assertion returns ok=false for nil.
ts, ok := ctx.Value(appStartTSKey).(string)
return ts, ok
}
func getIPFromContext(ctx context.Context) (net.IP, bool) {
userIP, ok := ctx.Value(userIPKey).(net.IP)
return userIP, ok
}
func getUserAgentFromContext(ctx context.Context) (string, bool) {
userAgent, ok := ctx.Value(userAgentKey).(string)
return userAgent, ok
}
// getIP extracts the user IP address from req, if present.
// https://blog.golang.org/context/userip/userip.go
func getIP(req *http.Request) (net.IP, error) {
ip, _, err := net.SplitHostPort(req.RemoteAddr)
if err != nil {
return nil, fmt.Errorf("userip: %q is not IP:port", req.RemoteAddr)
}
userIP := net.ParseIP(ip)
if userIP == nil {
return nil, fmt.Errorf("userip: %q is not IP:port", req.RemoteAddr)
}
return userIP, nil
}
本文网址: https://golangnote.com/topic/123.html 转摘请注明来源