diff --git a/cmd/api/main.go b/cmd/api/main.go index ad7d5a6f4..6fb83080a 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -28,7 +28,7 @@ func run(port int) error { if port == 0 { port = config.Config.Api.GinPort[0] } - cache, err := cache.NewRedis() + rdb, err := cache.NewRedis() if err != nil { return err } @@ -37,7 +37,7 @@ func run(port int) error { return err } log.NewPrivateLog(constant.LogFileName) - router := api.NewGinRouter(zk, cache) + router := api.NewGinRouter(zk, rdb) var address string if config.Config.Api.ListenIP != "" { address = net.JoinHostPort(config.Config.Api.ListenIP, strconv.Itoa(port)) diff --git a/internal/api/route.go b/internal/api/route.go index 875a80817..95631170d 100644 --- a/internal/api/route.go +++ b/internal/api/route.go @@ -10,19 +10,17 @@ import ( "github.com/go-redis/redis/v8" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" - "io" - "os" ) -func NewGinRouter(zk discoveryregistry.SvcDiscoveryRegistry, cache redis.UniversalClient) *gin.Engine { +func NewGinRouter(zk discoveryregistry.SvcDiscoveryRegistry, rdb redis.UniversalClient) *gin.Engine { + zk.AddOption(mw.GrpcClient(), grpc.WithTransportCredentials(insecure.NewCredentials())) // 默认RPC中间件 gin.SetMode(gin.ReleaseMode) - f, _ := os.Create("../logs/api.log") - gin.DefaultWriter = io.MultiWriter(f) - // gin.SetMode(gin.DebugMode) + //f, _ := os.Create("../logs/api.log") + //gin.DefaultWriter = io.MultiWriter(f) + //gin.SetMode(gin.DebugMode) r := gin.New() log.Info("load config: ", config.Config) - r.Use(gin.Recovery(), mw.CorsHandler(), mw.GinParseOperationID(), mw.GinParseToken(cache)) - + r.Use(gin.Recovery(), mw.CorsHandler(), mw.GinParseOperationID()) if config.Config.Prometheus.Enable { prome.NewApiRequestCounter() prome.NewApiRequestFailedCounter() @@ -30,17 +28,18 @@ func NewGinRouter(zk discoveryregistry.SvcDiscoveryRegistry, cache redis.Univers r.Use(prome.PrometheusMiddleware) r.GET("/metrics", prome.PrometheusHandler()) } - zk.AddOption(mw.GrpcClient(), grpc.WithTransportCredentials(insecure.NewCredentials())) // 默认RPC中间件 userRouterGroup := r.Group("/user") { u := NewUser(zk) - userRouterGroup.POST("/user_register", u.UserRegister) - userRouterGroup.POST("/update_user_info", u.UpdateUserInfo) //1 - userRouterGroup.POST("/set_global_msg_recv_opt", u.SetGlobalRecvMessageOpt) - userRouterGroup.POST("/get_users_info", u.GetUsersPublicInfo) //1 - userRouterGroup.POST("/get_all_users_uid", u.GetAllUsersID) // todo - userRouterGroup.POST("/account_check", u.AccountCheck) // todo - userRouterGroup.POST("/get_users", u.GetUsers) + userRouterGroupChild1 := mw.NewRouterGroup(userRouterGroup, "",) + userRouterGroupChild2 := mw.NewRouterGroup(userRouterGroup, "", mw.WithGinParseToken(rdb)) + userRouterGroupChild1.POST("/user_register", u.UserRegister) + userRouterGroupChild2.POST("/update_user_info", u.UpdateUserInfo) //1 + userRouterGroupChild2.POST("/set_global_msg_recv_opt", u.SetGlobalRecvMessageOpt) + userRouterGroupChild2.POST("/get_users_info", u.GetUsersPublicInfo) //1 + userRouterGroupChild2.POST("/get_all_users_uid", u.GetAllUsersID) // todo + userRouterGroupChild2.POST("/account_check", u.AccountCheck) // todo + userRouterGroupChild2.POST("/get_users", u.GetUsers) } ////friend routing group friendRouterGroup := r.Group("/friend") @@ -58,7 +57,6 @@ func NewGinRouter(zk discoveryregistry.SvcDiscoveryRegistry, cache redis.Univers friendRouterGroup.POST("/remove_black", f.RemoveBlack) //1 friendRouterGroup.POST("/import_friend", f.ImportFriends) //1 friendRouterGroup.POST("/is_friend", f.IsFriend) //1 - } groupRouterGroup := r.Group("/group") g := NewGroup(zk) @@ -96,10 +94,12 @@ func NewGinRouter(zk discoveryregistry.SvcDiscoveryRegistry, cache redis.Univers { a := NewAuth(zk) u := NewUser(zk) - authRouterGroup.POST("/user_register", u.UserRegister) //1 - authRouterGroup.POST("/user_token", a.UserToken) //1 - authRouterGroup.POST("/parse_token", a.ParseToken) //1 - authRouterGroup.POST("/force_logout", a.ForceLogout) //1 + authRouterGroupChild1 := mw.NewRouterGroup(authRouterGroup, "",) + authRouterGroupChild2 := mw.NewRouterGroup(authRouterGroup, "", mw.WithGinParseToken(rdb)) + authRouterGroupChild1.POST("/user_register", u.UserRegister) //1 + authRouterGroupChild1.POST("/user_token", a.UserToken) //1 + authRouterGroupChild2.POST("/parse_token", a.ParseToken) //1 + authRouterGroupChild2.POST("/force_logout", a.ForceLogout) //1 } ////Third service thirdGroup := r.Group("/third") @@ -115,7 +115,6 @@ func NewGinRouter(zk discoveryregistry.SvcDiscoveryRegistry, cache redis.Univers thirdGroup.POST("/confirm_put", t.ConfirmPut) thirdGroup.GET("/get_url", t.GetURL) thirdGroup.GET("/object", t.GetURL) - } ////Message chatGroup := r.Group("/msg") @@ -139,7 +138,7 @@ func NewGinRouter(zk discoveryregistry.SvcDiscoveryRegistry, cache redis.Univers } ////Conversation conversationGroup := r.Group("/conversation") - { //1 + { c := NewConversation(zk) conversationGroup.POST("/get_all_conversations", c.GetAllConversations) conversationGroup.POST("/get_conversation", c.GetConversation) @@ -151,3 +150,4 @@ func NewGinRouter(zk discoveryregistry.SvcDiscoveryRegistry, cache redis.Univers } return r } + diff --git a/pkg/common/mw/gin.go b/pkg/common/mw/gin.go index a793baca5..ddbd961c1 100644 --- a/pkg/common/mw/gin.go +++ b/pkg/common/mw/gin.go @@ -16,6 +16,40 @@ import ( "net/http" ) +type GinMwOptions func( *gin.RouterGroup ) + +func WithRecovery() GinMwOptions { + return func(group *gin.RouterGroup) { + group.Use(gin.Recovery()) + } +} + +func WithCorsHandler() GinMwOptions { + return func(group *gin.RouterGroup) { + group.Use(CorsHandler()) + } +} + +func WithGinParseOperationID() GinMwOptions { + return func(group *gin.RouterGroup) { + group.Use(GinParseOperationID()) + } +} + +func WithGinParseToken(rdb redis.UniversalClient) GinMwOptions { + return func(group *gin.RouterGroup) { + group.Use(GinParseToken(rdb)) + } +} + +func NewRouterGroup(routerGroup *gin.RouterGroup, route string, options ...GinMwOptions) *gin.RouterGroup { + routerGroup = routerGroup.Group(route) + for _, option := range options { + option(routerGroup) + } + return routerGroup +} + func CorsHandler() gin.HandlerFunc { return func(context *gin.Context) { context.Writer.Header().Set("Access-Control-Allow-Origin", "*") @@ -113,6 +147,5 @@ func GinParseToken(rdb redis.UniversalClient) gin.HandlerFunc { c.Set(constant.OpUserID, claims.UID) c.Next() } - } }