-
Notifications
You must be signed in to change notification settings - Fork 28
/
service.go
128 lines (110 loc) · 4.1 KB
/
service.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
121
122
123
124
125
126
127
128
package service
import (
_ "embed"
"github.com/flowchartsman/swaggerui"
"github.com/go-chi/chi/v5"
"github.com/go-kratos/kratos/v2/middleware"
"github.com/go-kratos/kratos/v2/transport/grpc"
khttp "github.com/go-kratos/kratos/v2/transport/http"
"github.com/go-saas/kit/pkg/authz/authz"
"github.com/go-saas/kit/pkg/blob"
kconf "github.com/go-saas/kit/pkg/conf"
"github.com/go-saas/kit/pkg/server"
"github.com/go-saas/kit/user/api"
v13 "github.com/go-saas/kit/user/api/account/v1"
v14 "github.com/go-saas/kit/user/api/auth/v1"
v15 "github.com/go-saas/kit/user/api/permission/v1"
v1 "github.com/go-saas/kit/user/api/role/v1"
v12 "github.com/go-saas/kit/user/api/user/v1"
uhttp "github.com/go-saas/kit/user/private/service/http"
"github.com/google/wire"
client "github.com/ory/hydra-client-go"
"net/http"
)
//go:embed openapi/api.swagger.json
var spec []byte
// ProviderSet is service providers.
var ProviderSet = wire.NewSet(
NewGrpcServerRegister,
NewHttpServerRegister,
NewUserRoleContrib,
NewUserService,
wire.Bind(new(v12.UserServiceServer), new(*UserService)),
NewAccountService,
wire.Bind(new(v13.AccountServer), new(*AccountService)),
NewAuthService,
wire.Bind(new(v14.AuthServer), new(*AuthService)),
NewRoleServiceService,
wire.Bind(new(v1.RoleServiceServer), new(*RoleService)),
NewPermissionService,
wire.Bind(new(v15.PermissionServiceServer), new(*PermissionService)),
NewHydra,
api.NewUserTenantContrib,
api.NewRefreshProvider,
uhttp.NewAuth)
type HttpServerRegister server.HttpServiceRegister
type GrpcServerRegister server.GrpcServiceRegister
func NewHttpServerRegister(user *UserService,
resEncoder khttp.EncodeResponseFunc,
errEncoder khttp.EncodeErrorFunc,
account *AccountService,
auth *AuthService,
role *RoleService,
permission *PermissionService,
authHttp *uhttp.Auth,
dataCfg *kconf.Data,
authzSrv authz.Service,
factory blob.Factory) HttpServerRegister {
return server.HttpServiceRegisterFunc(func(srv *khttp.Server, middleware ...middleware.Middleware) {
router := chi.NewRouter()
//global filter
router.Use(
server.MiddlewareConvert(errEncoder, middleware...))
router.Get("/login", server.HandlerWrap(resEncoder, authHttp.LoginGet))
router.Post("/login", server.HandlerWrap(resEncoder, authHttp.LoginPost))
router.Get("/logout", server.HandlerWrap(resEncoder, authHttp.LoginOutGet))
router.Post("/logout", server.HandlerWrap(resEncoder, authHttp.Logout))
router.Get("/consent", server.HandlerWrap(resEncoder, authHttp.ConsentGet))
router.Post("/consent", server.HandlerWrap(resEncoder, authHttp.Consent))
server.HandleBlobs("", dataCfg.Blobs, srv, factory)
srv.HandlePrefix("/v1/auth/web", http.StripPrefix("/v1/auth/web", router))
v12.RegisterUserServiceHTTPServer(srv, user)
v13.RegisterAccountHTTPServer(srv, account)
route := srv.Route("/")
route.POST("/v1/account/avatar", account.UpdateAvatar)
route.POST("/v1/user/avatar", user.UpdateAvatar)
v14.RegisterAuthHTTPServer(srv, auth)
v1.RegisterRoleServiceHTTPServer(srv, role)
v15.RegisterPermissionServiceHTTPServer(srv, permission)
swaggerRouter := chi.NewRouter()
const apiPrefix = "/v1/user/dev/swagger"
swaggerRouter.Handle(apiPrefix+"*", http.StripPrefix(apiPrefix, server.AuthzGuardian(
authzSrv, authz.RequirementList{
authz.NewRequirement(authz.NewEntityResource("dev", "user"), authz.AnyAction),
}, errEncoder, swaggerui.Handler(spec),
)))
srv.HandlePrefix(apiPrefix, swaggerRouter)
})
}
func NewGrpcServerRegister(user *UserService,
account *AccountService,
auth *AuthService,
role *RoleService,
permission *PermissionService) GrpcServerRegister {
return server.GrpcServiceRegisterFunc(func(srv *grpc.Server, middleware ...middleware.Middleware) {
v12.RegisterUserServiceServer(srv, user)
v13.RegisterAccountServer(srv, account)
v14.RegisterAuthServer(srv, auth)
v1.RegisterRoleServiceServer(srv, role)
v15.RegisterPermissionServiceServer(srv, permission)
})
}
func NewHydra(c *kconf.Security) *client.APIClient {
cfg := client.NewConfiguration()
cfg.Servers = client.ServerConfigurations{
{
URL: c.Oidc.Hydra.AdminUrl,
},
}
return client.NewAPIClient(cfg)
}