Merge branch 'errcode' of github.com:OpenIMSDK/Open-IM-Server into errcode

# Conflicts:
#	pkg/common/mw/rpc_server_interceptor.go
test-errcode
wangchuxiao 2 years ago
parent cbfe679f06
commit 51d8c0ba51

@ -28,7 +28,7 @@ func run(port int) error {
if port == 0 { if port == 0 {
port = config.Config.Api.GinPort[0] port = config.Config.Api.GinPort[0]
} }
cache, err := cache.NewRedis() rdb, err := cache.NewRedis()
if err != nil { if err != nil {
return err return err
} }
@ -37,7 +37,7 @@ func run(port int) error {
return err return err
} }
log.NewPrivateLog(constant.LogFileName) log.NewPrivateLog(constant.LogFileName)
router := api.NewGinRouter(zk, cache) router := api.NewGinRouter(zk, rdb)
var address string var address string
if config.Config.Api.ListenIP != "" { if config.Config.Api.ListenIP != "" {
address = net.JoinHostPort(config.Config.Api.ListenIP, strconv.Itoa(port)) address = net.JoinHostPort(config.Config.Api.ListenIP, strconv.Itoa(port))

@ -10,19 +10,17 @@ import (
"github.com/go-redis/redis/v8" "github.com/go-redis/redis/v8"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure" "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) gin.SetMode(gin.ReleaseMode)
f, _ := os.Create("../logs/api.log") //f, _ := os.Create("../logs/api.log")
gin.DefaultWriter = io.MultiWriter(f) //gin.DefaultWriter = io.MultiWriter(f)
//gin.SetMode(gin.DebugMode) //gin.SetMode(gin.DebugMode)
r := gin.New() r := gin.New()
log.Info("load config: ", config.Config) 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 { if config.Config.Prometheus.Enable {
prome.NewApiRequestCounter() prome.NewApiRequestCounter()
prome.NewApiRequestFailedCounter() prome.NewApiRequestFailedCounter()
@ -30,17 +28,18 @@ func NewGinRouter(zk discoveryregistry.SvcDiscoveryRegistry, cache redis.Univers
r.Use(prome.PrometheusMiddleware) r.Use(prome.PrometheusMiddleware)
r.GET("/metrics", prome.PrometheusHandler()) r.GET("/metrics", prome.PrometheusHandler())
} }
zk.AddOption(mw.GrpcClient(), grpc.WithTransportCredentials(insecure.NewCredentials())) // 默认RPC中间件
userRouterGroup := r.Group("/user") userRouterGroup := r.Group("/user")
{ {
u := NewUser(zk) u := NewUser(zk)
userRouterGroup.POST("/user_register", u.UserRegister) userRouterGroupChild1 := mw.NewRouterGroup(userRouterGroup, "",)
userRouterGroup.POST("/update_user_info", u.UpdateUserInfo) //1 userRouterGroupChild2 := mw.NewRouterGroup(userRouterGroup, "", mw.WithGinParseToken(rdb))
userRouterGroup.POST("/set_global_msg_recv_opt", u.SetGlobalRecvMessageOpt) userRouterGroupChild1.POST("/user_register", u.UserRegister)
userRouterGroup.POST("/get_users_info", u.GetUsersPublicInfo) //1 userRouterGroupChild2.POST("/update_user_info", u.UpdateUserInfo) //1
userRouterGroup.POST("/get_all_users_uid", u.GetAllUsersID) // todo userRouterGroupChild2.POST("/set_global_msg_recv_opt", u.SetGlobalRecvMessageOpt)
userRouterGroup.POST("/account_check", u.AccountCheck) // todo userRouterGroupChild2.POST("/get_users_info", u.GetUsersPublicInfo) //1
userRouterGroup.POST("/get_users", u.GetUsers) 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 ////friend routing group
friendRouterGroup := r.Group("/friend") 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("/remove_black", f.RemoveBlack) //1
friendRouterGroup.POST("/import_friend", f.ImportFriends) //1 friendRouterGroup.POST("/import_friend", f.ImportFriends) //1
friendRouterGroup.POST("/is_friend", f.IsFriend) //1 friendRouterGroup.POST("/is_friend", f.IsFriend) //1
} }
groupRouterGroup := r.Group("/group") groupRouterGroup := r.Group("/group")
g := NewGroup(zk) g := NewGroup(zk)
@ -96,10 +94,12 @@ func NewGinRouter(zk discoveryregistry.SvcDiscoveryRegistry, cache redis.Univers
{ {
a := NewAuth(zk) a := NewAuth(zk)
u := NewUser(zk) u := NewUser(zk)
authRouterGroup.POST("/user_register", u.UserRegister) //1 authRouterGroupChild1 := mw.NewRouterGroup(authRouterGroup, "",)
authRouterGroup.POST("/user_token", a.UserToken) //1 authRouterGroupChild2 := mw.NewRouterGroup(authRouterGroup, "", mw.WithGinParseToken(rdb))
authRouterGroup.POST("/parse_token", a.ParseToken) //1 authRouterGroupChild1.POST("/user_register", u.UserRegister) //1
authRouterGroup.POST("/force_logout", a.ForceLogout) //1 authRouterGroupChild1.POST("/user_token", a.UserToken) //1
authRouterGroupChild2.POST("/parse_token", a.ParseToken) //1
authRouterGroupChild2.POST("/force_logout", a.ForceLogout) //1
} }
////Third service ////Third service
thirdGroup := r.Group("/third") thirdGroup := r.Group("/third")
@ -115,7 +115,6 @@ func NewGinRouter(zk discoveryregistry.SvcDiscoveryRegistry, cache redis.Univers
thirdGroup.POST("/confirm_put", t.ConfirmPut) thirdGroup.POST("/confirm_put", t.ConfirmPut)
thirdGroup.GET("/get_url", t.GetURL) thirdGroup.GET("/get_url", t.GetURL)
thirdGroup.GET("/object", t.GetURL) thirdGroup.GET("/object", t.GetURL)
} }
////Message ////Message
chatGroup := r.Group("/msg") chatGroup := r.Group("/msg")
@ -139,7 +138,7 @@ func NewGinRouter(zk discoveryregistry.SvcDiscoveryRegistry, cache redis.Univers
} }
////Conversation ////Conversation
conversationGroup := r.Group("/conversation") conversationGroup := r.Group("/conversation")
{ //1 {
c := NewConversation(zk) c := NewConversation(zk)
conversationGroup.POST("/get_all_conversations", c.GetAllConversations) conversationGroup.POST("/get_all_conversations", c.GetAllConversations)
conversationGroup.POST("/get_conversation", c.GetConversation) conversationGroup.POST("/get_conversation", c.GetConversation)
@ -151,3 +150,4 @@ func NewGinRouter(zk discoveryregistry.SvcDiscoveryRegistry, cache redis.Univers
} }
return r return r
} }

@ -16,6 +16,40 @@ import (
"net/http" "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 { func CorsHandler() gin.HandlerFunc {
return func(context *gin.Context) { return func(context *gin.Context) {
context.Writer.Header().Set("Access-Control-Allow-Origin", "*") 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.Set(constant.OpUserID, claims.UID)
c.Next() c.Next()
} }
} }
} }

Loading…
Cancel
Save