This repository has been archived on 2026-01-07. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
Game/Server/Gateway/net/http_gateway/router.go

45 lines
1.0 KiB
Go

package http_gateway
import (
"gateway/config"
"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()))
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
})
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,
}
}