|
- package router
-
- import (
- "embed"
- "net/http"
- "strings"
-
- "github.com/QuantumNous/new-api/common"
- "github.com/QuantumNous/new-api/controller"
- "github.com/QuantumNous/new-api/middleware"
- "github.com/gin-contrib/gzip"
- "github.com/gin-contrib/static"
- "github.com/gin-gonic/gin"
- )
-
- func SetWebRouter(router *gin.Engine, buildFS embed.FS, indexPage []byte) {
- // LOGO_FILE_PATH 优先级最高:注册 /logo.png 路由提供磁盘文件
- // 必须在 static.Serve("/") 之前注册,否则会被 embed 静态文件拦截
- if common.LogoFilePath != "" {
- router.GET("/logo.png", func(c *gin.Context) {
- c.File(common.LogoFilePath)
- })
- }
-
- router.Use(gzip.Gzip(gzip.DefaultCompression))
- router.Use(middleware.GlobalWebRateLimit())
- router.Use(middleware.Cache())
- router.Use(static.Serve("/", common.EmbedFolder(buildFS, "web/dist")))
- router.NoRoute(func(c *gin.Context) {
- if strings.HasPrefix(c.Request.RequestURI, "/v1") || strings.HasPrefix(c.Request.RequestURI, "/api") || strings.HasPrefix(c.Request.RequestURI, "/assets") {
- controller.RelayNotFound(c)
- return
- }
- c.Header("Cache-Control", "no-cache")
- c.Data(http.StatusOK, "text/html; charset=utf-8", indexPage)
- })
- }
|