44 lines
844 B
Go
44 lines
844 B
Go
package http_gateway
|
|
|
|
import (
|
|
"common/log"
|
|
"gateway/config"
|
|
"gateway/handler/http_handler"
|
|
"github.com/gin-contrib/cors"
|
|
"github.com/gin-gonic/gin"
|
|
"net/http"
|
|
)
|
|
|
|
func InitRouter(cfg *config.Config) *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, gin.H{
|
|
"result": false,
|
|
"error": "Method Not Allowed",
|
|
})
|
|
})
|
|
r.NoRoute(func(c *gin.Context) {
|
|
c.JSON(http.StatusNotFound, gin.H{
|
|
"result": false,
|
|
"error": "Endpoint Not Found",
|
|
})
|
|
})
|
|
initBaseRouter(r)
|
|
|
|
return r
|
|
}
|
|
|
|
func initBaseRouter(router *gin.Engine) {
|
|
g := router.Group("/b")
|
|
g.POST("/snowflake", http_handler.GenSnowflake) // 生成雪花
|
|
}
|