45 lines
1.0 KiB
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,
|
|
}
|
|
}
|