/
transport.go
97 lines (88 loc) · 3.01 KB
/
transport.go
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
/**
* @Time : 2019-07-02 18:36
* @Author : ygqbasic@gmail.com
* @File : transport
* @Software: VS Code
*/
package middleware
import (
"context"
"github.com/go-kit/kit/auth/casbin"
"github.com/go-kit/kit/transport/http"
"github.com/gorilla/mux"
kplcasbin "github.com/fuhsicloud/fuhsicloud/src/casbin"
"github.com/fuhsicloud/fuhsicloud/src/util/uid"
stdhttp "net/http"
"strconv"
)
func NamespaceToContext() http.RequestFunc {
return func(ctx context.Context, r *stdhttp.Request) context.Context {
vars := mux.Vars(r)
ns, ok := vars["namespace"]
if ok {
ctx = context.WithValue(ctx, NamespaceContext, ns)
}
name, ok := vars["name"]
if ok {
ctx = context.WithValue(ctx, NameContext, name)
}
return ctx
}
}
func GroupIdToContext() http.RequestFunc {
return func(ctx context.Context, r *stdhttp.Request) context.Context {
vars := mux.Vars(r)
groupId, ok := vars["groupId"]
if ok {
groupIdInt, _ := strconv.Atoi(groupId)
ctx = context.WithValue(ctx, GroupIdContext, int64(groupIdInt))
}
return ctx
}
}
func CasbinToContext() http.RequestFunc {
return func(ctx context.Context, request *stdhttp.Request) context.Context {
casbinCtx := kplcasbin.GetCasbin().GetContext()
ctx = context.WithValue(ctx, casbin.CasbinModelContextKey, casbinCtx.Value(casbin.CasbinModelContextKey))
ctx = context.WithValue(ctx, casbin.CasbinPolicyContextKey, casbinCtx.Value(casbin.CasbinPolicyContextKey))
ctx = context.WithValue(ctx, casbin.CasbinEnforcerContextKey, casbinCtx.Value(casbin.CasbinEnforcerContextKey))
return ctx
}
}
func CookieToContext() http.RequestFunc {
return func(ctx context.Context, r *stdhttp.Request) context.Context {
if c, err := r.Cookie("Authorization"); err == nil {
ctx = context.WithValue(ctx, http.ContextKeyRequestAuthorization, c.Value)
r.Header.Set(string(http.ContextKeyRequestAuthorization), c.Value)
}
return ctx
}
}
func RequestIdToContext() http.RequestFunc {
return func(ctx context.Context, r *stdhttp.Request) context.Context {
if r.Header.Get("X-Request-Id") != "" {
ctx = context.WithValue(ctx, uid.RequestId, r.Header.Get("X-Request-Id"))
} else {
ctx = context.WithValue(ctx, uid.RequestId, uid.GenerateUid())
}
return ctx
}
}
func RequestIdToResponse() http.ServerResponseFunc {
return func(ctx context.Context, w stdhttp.ResponseWriter) context.Context {
if requestId, ok := ctx.Value(uid.RequestId).(string); ok {
w.Header().Set("X-Request-Id", requestId)
}
return ctx
}
}
func AllowCors() http.ServerResponseFunc {
return func(ctx context.Context, w stdhttp.ResponseWriter) context.Context {
w.Header().Set("Access-Control-Allow-Origin", "http://localhost:8000")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, DELETE")
w.Header().Set("Access-Control-Allow-Headers", "Origin, Content-Type,Authorization,x-requested-with,Access-Control-Allow-Origin,Access-Control-Allow-Credentials")
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Connection", "keep-alive")
return ctx
}
}