55 lines
1.3 KiB
Go
55 lines
1.3 KiB
Go
package http_gateway
|
|
|
|
import (
|
|
"common/log"
|
|
"common/net/http/http_resp"
|
|
wrapper2 "gateway/internal/net/http_gateway/wrapper"
|
|
"github.com/gin-contrib/cors"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
|
"google.golang.org/protobuf/encoding/protojson"
|
|
"net/http"
|
|
)
|
|
|
|
func InitServeMux() *runtime.ServeMux {
|
|
baseMarshaler := &runtime.JSONPb{
|
|
MarshalOptions: protojson.MarshalOptions{
|
|
UseProtoNames: false,
|
|
EmitUnpopulated: true,
|
|
},
|
|
UnmarshalOptions: protojson.UnmarshalOptions{
|
|
DiscardUnknown: true,
|
|
},
|
|
}
|
|
unifiedMarshaler := wrapper2.NewWrappedMarshaler(baseMarshaler)
|
|
|
|
mux := runtime.NewServeMux(
|
|
runtime.WithMarshalerOption(runtime.MIMEWildcard, unifiedMarshaler),
|
|
runtime.WithErrorHandler(wrapper2.ErrorHandler),
|
|
)
|
|
return mux
|
|
}
|
|
|
|
func InitRouter(mux *runtime.ServeMux) *gin.Engine {
|
|
gin.SetMode(gin.ReleaseMode)
|
|
|
|
r := gin.New()
|
|
r.Use(
|
|
gin.Recovery(),
|
|
ginLogger(log.GetLogger().Named("GIN")),
|
|
cors.New(corsConfig()),
|
|
)
|
|
|
|
r.HandleMethodNotAllowed = true
|
|
r.NoMethod(func(c *gin.Context) {
|
|
c.JSON(http.StatusMethodNotAllowed, http_resp.Error(http_resp.Failed.Code(), "Method Not Allowed"))
|
|
})
|
|
r.NoRoute(func(c *gin.Context) {
|
|
c.JSON(http.StatusNotFound, http_resp.Error(http_resp.Failed.Code(), "Endpoint Not Found"))
|
|
})
|
|
|
|
r.Any("/*any", gin.WrapH(mux))
|
|
|
|
return r
|
|
}
|