-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
95 lines (85 loc) · 2.58 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
package server
import (
"context"
"strings"
"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/middleware/logging"
"github.com/go-kratos/kratos/v2/middleware/recovery"
"github.com/go-kratos/kratos/v2/middleware/selector"
"github.com/go-kratos/kratos/v2/middleware/validate"
"github.com/go-kratos/kratos/v2/transport/http"
"github.com/gorilla/handlers"
v1 "github.com/hominsu/pallas/api/pallas/service/v1"
"github.com/hominsu/pallas/app/pallas/service/internal/biz"
"github.com/hominsu/pallas/app/pallas/service/internal/conf"
"github.com/hominsu/pallas/app/pallas/service/internal/service"
"github.com/hominsu/pallas/app/pallas/service/pkgs/middleware"
"github.com/hominsu/pallas/pkg/sessions"
)
func NewSkipSessionMatcher() selector.MatchFunc {
skipList := make(map[string]struct{})
skipList["/pallas.service.v1.SiteService/Ping"] = struct{}{}
skipList["/pallas.service.v1.UserService/Signup"] = struct{}{}
skipList["/pallas.service.v1.UserService/Signin"] = struct{}{}
return func(ctx context.Context, operation string) bool {
if _, ok := skipList[operation]; ok {
return false
}
return true
}
}
func NewAdminMatcher() selector.MatchFunc {
return func(ctx context.Context, operation string) bool {
return strings.HasPrefix("/pallas.service.v1.AdminService/", operation)
}
}
func NewHTTPServer(
c *conf.Server,
ss *service.SiteService,
us *service.UserService,
as *service.AdminService,
uu *biz.UserUsecase,
store *sessions.RedisStore,
logger log.Logger,
) *http.Server {
// server options
opts := []http.ServerOption{
http.Middleware(
recovery.Recovery(),
logging.Server(logger),
validate.Validator(),
middleware.Info(),
selector.Server(
middleware.Session(store, "pallas-session", logger),
).
Match(NewSkipSessionMatcher()).
Build(),
selector.Server(
middleware.Admin(uu, logger),
).
Match(NewAdminMatcher()).
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...)
v1.RegisterSiteServiceHTTPServer(srv, ss)
v1.RegisterUserServiceHTTPServer(srv, us)
v1.RegisterAdminServiceHTTPServer(srv, as)
return srv
}