/
http.go
100 lines (92 loc) · 3.38 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
package server
import (
"context"
v1 "github.com/csyangpeng/go-kratos-admin/api/center/admin/v1"
"github.com/csyangpeng/go-kratos-admin/app/center/admin/internal/conf"
"github.com/csyangpeng/go-kratos-admin/app/center/admin/internal/service"
prom "github.com/go-kratos/kratos/contrib/metrics/prometheus/v2"
"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"
"github.com/go-kratos/swagger-api/openapiv2"
jwtv4 "github.com/golang-jwt/jwt/v4"
"github.com/gorilla/handlers"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func NewWhiteListMatcher() selector.MatchFunc {
whiteList := make(map[string]struct{})
whiteList["/center.admin.v1.CenterAdmin/Login"] = struct{}{}
whiteList["/center.admin.v1.CenterAdmin/Register"] = struct{}{}
return func(ctx context.Context, operation string) bool {
if _, ok := whiteList[operation]; ok {
return false
}
return true
}
}
// NewHTTPServer new a HTTP server.
func NewHTTPServer(c *conf.Server, ac *conf.Auth, s *service.CenterAdmin, logger log.Logger) *http.Server {
// https://github.com/go-kratos/examples/tree/main/metrics
_metricSeconds := prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "server",
Subsystem: "requests",
Name: "duration_ms",
Help: "server requests duration(ms).",
Buckets: []float64{5, 10, 25, 50, 100, 250, 500, 1000},
}, []string{"kind", "operation"})
_metricRequests := prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "client",
Subsystem: "requests",
Name: "code_total",
Help: "The total number of processed requests",
}, []string{"kind", "operation", "code", "reason"})
prometheus.MustRegister(_metricSeconds, _metricRequests)
var opts = []http.ServerOption{
http.Middleware(
recovery.Recovery(),
logging.Server(logger),
tracing.Server(),
metrics.Server(
metrics.WithSeconds(prom.NewHistogram(_metricSeconds)),
metrics.WithRequests(prom.NewCounter(_metricRequests)),
),
validate.Validator(),
selector.Server(
jwt.Server(func(token *jwtv4.Token) (interface{}, error) {
return []byte(ac.ApiKey), nil
}, jwt.WithSigningMethod(jwtv4.SigningMethodHS256), jwt.WithClaims(func() jwtv4.Claims {
return &jwtv4.MapClaims{}
})),
).
Match(NewWhiteListMatcher()).
Build(),
),
http.Filter(handlers.CORS(
handlers.AllowedHeaders([]string{"X-Requested-With", "Content-Type", "Authorization"}),
handlers.AllowedMethods([]string{"GET", "POST", "PUT", "HEAD", "OPTIONS"}),
handlers.AllowedOrigins([]string{"*"}),
)),
}
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()))
}
srv := http.NewServer(opts...)
h := openapiv2.NewHandler()
srv.HandlePrefix("/q/", h)
srv.Handle("/metrics", promhttp.Handler())
v1.RegisterCenterAdminHTTPServer(srv, s)
return srv
}