package httpgateway import ( "context" "git.hlsq.asia/mmorpg/service-common/discover/common" "git.hlsq.asia/mmorpg/service-common/log" "git.hlsq.asia/mmorpg/service-common/net/grpc/grpcclient" "git.hlsq.asia/mmorpg/service-common/net/http/httpresp" "git.hlsq.asia/mmorpg/service-common/proto/rs/rspb" "git.hlsq.asia/mmorpg/service-gateway/internal/handler/httphandler" "git.hlsq.asia/mmorpg/service-gateway/internal/net/httpgateway/wrapper" "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" "go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin" "google.golang.org/protobuf/encoding/protojson" "strings" ) func InitServeMux() *runtime.ServeMux { baseMarshaler := &runtime.JSONPb{ MarshalOptions: protojson.MarshalOptions{ UseProtoNames: false, EmitUnpopulated: true, }, UnmarshalOptions: protojson.UnmarshalOptions{ DiscardUnknown: true, }, } unifiedMarshaler := wrapper.NewWrappedMarshaler(baseMarshaler) mux := runtime.NewServeMux( runtime.WithMarshalerOption(runtime.MIMEWildcard, unifiedMarshaler), runtime.WithErrorHandler(wrapper.ErrorHandler), runtime.WithIncomingHeaderMatcher(func(header string) (string, bool) { if header == "X-Usn" { return "X-Usn", true } return runtime.DefaultHeaderMatcher(header) }), ) return mux } func InitRouter() *gin.Engine { gin.SetMode(gin.ReleaseMode) r := gin.New() r.Use( gin.Recovery(), ginLogger(log.GetLogger().Named("GIN")), cors.New(corsConfig()), otelgin.Middleware( common.KeyDiscoverServiceNameGateway, otelgin.WithSpanNameFormatter(func(c *gin.Context) string { method := strings.ToUpper(c.Request.Method) return method + " " + c.Request.URL.Path }), ), ) r.HandleMethodNotAllowed = true r.NoMethod(func(c *gin.Context) { httpresp.JsonMethodNotAllowed(c) c.Abort() }) r.NoRoute(func(c *gin.Context) { httpresp.JsonNotFound(c) c.Abort() }) auth := r.Group("/") auth.Use(authJwt()) // 网关 initGatewayPath(auth) // 用户中心 initUserPath(auth) // 奇怪的知识-服务端 initQgdzsPath(auth) return r } func initGatewayPath(r *gin.RouterGroup) { g := r.Group("/" + common.KeyDiscoverServiceNameGateway) g.POST("/open/login", httphandler.Login) g.POST("/open/refresh_token", httphandler.RefreshToken) g.POST("/open/logout", httphandler.Logout) } func initUserPath(r *gin.RouterGroup) { g := r.Group("/" + common.KeyDiscoverServiceNameUser) client, err := grpcclient.UserNewClientLB() if err != nil { log.Errorf("get user conn failed: %v", err) return } gwMux := InitServeMux() if err = rspb.RegisterUserHandlerClient(context.Background(), gwMux, client); err != nil { log.Errorf("RegisterUserHandlerClient err: %v", err) return } g.Any("/*path", gin.WrapH(gwMux)) } func initQgdzsPath(r *gin.RouterGroup) { g := r.Group("/" + common.KeyDiscoverServiceNameQgdzs) client, err := grpcclient.QgdzsNewClientLB() if err != nil { log.Errorf("get qgdzs conn failed: %v", err) return } gwMux := InitServeMux() if err = rspb.RegisterQgdzsHandlerClient(context.Background(), gwMux, client); err != nil { log.Errorf("RegisterQgdzsHandlerClient err: %v", err) return } g.Any("/*path", gin.WrapH(gwMux)) }