-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
120 lines (114 loc) · 3.69 KB
/
http.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package server
import (
"context"
sj "encoding/json"
"github.com/gin-gonic/gin"
"github.com/go-atreus/atreus-server/app/admin/api/auth"
"github.com/go-atreus/atreus-server/app/admin/api/dict"
"github.com/go-atreus/atreus-server/app/admin/api/menu"
"github.com/go-atreus/atreus-server/app/admin/api/organization"
"github.com/go-atreus/atreus-server/app/admin/api/role"
"github.com/go-atreus/atreus-server/app/admin/api/user"
"github.com/go-atreus/atreus-server/app/interface/internal/conf"
"github.com/go-atreus/atreus-server/app/interface/internal/service"
"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/middleware/auth/jwt"
"github.com/go-kratos/kratos/v2/middleware/logging"
"github.com/go-kratos/kratos/v2/middleware/metrics"
"github.com/go-kratos/kratos/v2/middleware/recovery"
"github.com/go-kratos/kratos/v2/middleware/selector"
"github.com/go-kratos/kratos/v2/middleware/tracing"
"github.com/go-kratos/kratos/v2/middleware/validate"
"github.com/go-kratos/kratos/v2/transport/http"
jwt2 "github.com/golang-jwt/jwt/v5"
tracesdk "go.opentelemetry.io/otel/sdk/trace"
shttp "net/http"
"strings"
)
func NewWhiteListMatcher() selector.MatchFunc {
whiteList := make(map[string]struct{})
whiteList["/Atreus.auth.Auth/getUserToken"] = struct{}{}
whiteList["/atreus.auth.Auth/userLogin"] = struct{}{}
whiteList["/atreus.auth.Auth/forceLogout"] = struct{}{}
whiteList["/shop.interface.v1.ShopInterface/Register"] = struct{}{}
return func(ctx context.Context, operation string) bool {
if _, ok := whiteList[operation]; ok {
return false
}
return true
}
}
func NewHTTPServer(
adminInterface *service.AdminInterface,
logger log.Logger,
authConfig *conf.Auth,
bc *conf.Bootstrap,
tp *tracesdk.TracerProvider,
) *http.Server {
gin.SetMode(gin.ReleaseMode)
r := gin.New()
r.Use(gin.Recovery(), gin.Logger())
var opts = []http.ServerOption{
http.Middleware(
recovery.Recovery(),
tracing.Server(),
logging.Server(logger),
metrics.Server(),
validate.Validator(),
selector.Server(
jwt.Server(func(token *jwt2.Token) (interface{}, error) {
return []byte(authConfig.Key), nil
},
jwt.WithSigningMethod(jwt2.SigningMethodHS256),
jwt.WithClaims(func() jwt2.Claims {
return &jwt2.MapClaims{}
})),
).
Match(NewWhiteListMatcher()).
Build(),
),
http.ResponseEncoder(JsonResponseEncoder),
}
c := bc.Server
if c.Http.Network != "" {
opts = append(opts, http.Network(c.Http.Network))
}
if c.Http.Addr != "" {
opts = append(opts, http.Address(c.Http.Addr))
}
if c.Http.Timeout != nil {
opts = append(opts, http.Timeout(c.Http.Timeout.AsDuration()))
}
httpSrv := http.NewServer(opts...)
user.RegisterUserHTTPServer(httpSrv, adminInterface)
auth.RegisterAuthHTTPServer(httpSrv, adminInterface)
menu.RegisterMenuHTTPServer(httpSrv, adminInterface)
role.RegisterRoleHTTPServer(httpSrv, adminInterface)
organization.RegisterOrganizationHTTPServer(httpSrv, adminInterface)
dict.RegisterDictHTTPServer(httpSrv, adminInterface)
httpSrv.HandlePrefix("/", r)
return httpSrv
}
// JsonResponseEncoder encodes the object to the HTTP response.
func JsonResponseEncoder(w http.ResponseWriter, r *http.Request, v interface{}) error {
codec, _ := http.CodecForRequest(r, "Accept")
resBytes, err := codec.Marshal(v)
if err != nil {
return err
}
data, err := sj.Marshal(&Res{
Code: 0, Message: "", Data: sj.RawMessage(resBytes),
})
if err != nil {
return err
}
w.Header().Set("Content-Type", strings.Join([]string{"application", codec.Name()}, "/"))
w.WriteHeader(shttp.StatusOK)
_, _ = w.Write(data)
return nil
}
type Res struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data"`
}