/
relation_gw_router.go
74 lines (63 loc) · 2.94 KB
/
relation_gw_router.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
// Code generated by https://github.com/zhufuyi/sponge
package routers
import (
"context"
community_gwV1 "community_gw/api/community_gw/v1"
"community_gw/internal/service"
"github.com/zhufuyi/sponge/pkg/gin/middleware"
"github.com/zhufuyi/sponge/pkg/logger"
"github.com/gin-gonic/gin"
"google.golang.org/grpc/metadata"
)
func init() {
allMiddlewareFns = append(allMiddlewareFns, func(c *middlewareConfig) {
relationServiceMiddlewares(c)
})
allRouteFns = append(allRouteFns,
func(r *gin.Engine, groupPathMiddlewares map[string][]gin.HandlerFunc, singlePathMiddlewares map[string][]gin.HandlerFunc) {
relationServiceRouter(r, groupPathMiddlewares, singlePathMiddlewares, service.NewRelationServiceClient())
})
}
func relationServiceRouter(
r *gin.Engine,
groupPathMiddlewares map[string][]gin.HandlerFunc,
singlePathMiddlewares map[string][]gin.HandlerFunc,
iService community_gwV1.RelationServiceLogicer) {
ctxFn := func(c *gin.Context) context.Context {
md := metadata.New(map[string]string{
// set metadata to be passed from http to rpc
middleware.ContextRequestIDKey: middleware.GCtxRequestID(c), // request_id
//middleware.HeaderAuthorizationKey: c.GetHeader(middleware.HeaderAuthorizationKey), // authorization
})
return metadata.NewOutgoingContext(c.Request.Context(), md)
}
community_gwV1.RegisterRelationServiceRouter(
r,
groupPathMiddlewares,
singlePathMiddlewares,
iService,
community_gwV1.WithRelationServiceRPCResponse(),
community_gwV1.WithRelationServiceLogger(logger.Get()),
community_gwV1.WithRelationServiceRPCStatusToHTTPCode(
// Set some error codes to standard http return codes,
// by default there is already ecode.StatusInternalServerError and ecode.StatusServiceUnavailable
// example:
// ecode.StatusUnimplemented, ecode.StatusAborted,
),
community_gwV1.WithRelationServiceWrapCtx(ctxFn),
)
}
// you can set the middleware of a route group, or set the middleware of a single route,
// or you can mix them, pay attention to the duplication of middleware when mixing them,
// it is recommended to set the middleware of a single route in preference
func relationServiceMiddlewares(c *middlewareConfig) {
// set up group route middleware, group path is left prefix rules,
// if the left prefix is hit, the middleware will take effect, e.g. group route is /api/v1, route /api/v1/relationService/:id will take effect
c.setGroupPath("/api/v1/relation", middleware.Auth(middleware.WithVerify(verify)))
// set up single route middleware, just uncomment the code and fill in the middlewares, nothing else needs to be changed
//c.setSinglePath("POST", "/api/v1/relation/follow", middleware.Auth())
//c.setSinglePath("POST", "/api/v1/relation/unfollow", middleware.Auth())
//c.setSinglePath("GET", "/api/v1/relation/following/list", middleware.Auth())
//c.setSinglePath("GET", "/api/v1/relation/follower/list", middleware.Auth())
//c.setSinglePath("POST", "/api/v1/relation/check/list", middleware.Auth())
}