feat app 模块化启动

This commit is contained in:
2025-12-13 18:22:35 +08:00
parent 71d4e593c7
commit bc656247c9
41 changed files with 730 additions and 253 deletions

View File

@@ -1,44 +1,43 @@
package http_gateway
import (
"common/log"
"gateway/config"
"gateway/handler/http_handler"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"net/http"
"time"
)
func InitRouter(cfg *config.Config) *gin.Engine {
r := gin.Default()
r.Use(gin.Recovery())
r.Use(cors.New(getCorsConfig()))
gin.SetMode(gin.ReleaseMode)
r := gin.New()
r.Use(
gin.Recovery(),
ginLogger(log.GetLogger().Named("GIN")),
cors.New(corsConfig()),
)
r.MaxMultipartMemory = 8 << 20
r.HandleMethodNotAllowed = true
r.NoMethod(func(c *gin.Context) {
c.JSON(http.StatusMethodNotAllowed, gin.H{
"result": false,
"error": "Method Not Allowed",
})
return
})
r.NoRoute(func(c *gin.Context) {
c.JSON(http.StatusNotFound, gin.H{
"result": false,
"error": "Endpoint Not Found",
})
return
})
initBaseRouter(r)
return r
}
func getCorsConfig() cors.Config {
return cors.Config{
AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD"},
AllowHeaders: []string{"Origin", "Content-Length", "Content-Type", "Authorization", "X-Forwarded-For", "User-Agent", "Referer", "X-Token", "token", "Token", "company-id", "lang", "source"},
AllowCredentials: false,
AllowAllOrigins: true,
MaxAge: 12 * time.Hour,
}
func initBaseRouter(router *gin.Engine) {
g := router.Group("/b")
g.POST("/snowflake", http_handler.GenSnowflake) // 生成雪花
}