ai-gateway/router/web-router.go

32 lines
1022 B
Go
Raw Normal View History

2023-04-22 12:39:27 +00:00
package router
import (
"embed"
2024-01-01 10:55:03 +00:00
"fmt"
"github.com/gin-contrib/gzip"
2023-04-22 12:39:27 +00:00
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
2024-01-28 11:38:58 +00:00
"github.com/songquanpeng/one-api/common"
"github.com/songquanpeng/one-api/common/config"
"github.com/songquanpeng/one-api/controller"
"github.com/songquanpeng/one-api/middleware"
2023-04-22 12:39:27 +00:00
"net/http"
"strings"
2023-04-22 12:39:27 +00:00
)
2024-01-01 10:55:03 +00:00
func SetWebRouter(router *gin.Engine, buildFS embed.FS) {
indexPageData, _ := buildFS.ReadFile(fmt.Sprintf("web/build/%s/index.html", config.Theme))
router.Use(gzip.Gzip(gzip.DefaultCompression))
2023-04-22 12:39:27 +00:00
router.Use(middleware.GlobalWebRateLimit())
router.Use(middleware.Cache())
router.Use(static.Serve("/", common.EmbedFolder(buildFS, fmt.Sprintf("web/build/%s", config.Theme))))
2023-04-22 12:39:27 +00:00
router.NoRoute(func(c *gin.Context) {
2023-08-11 11:53:01 +00:00
if strings.HasPrefix(c.Request.RequestURI, "/v1") || strings.HasPrefix(c.Request.RequestURI, "/api") {
controller.RelayNotFound(c)
return
}
2023-05-21 16:44:27 +00:00
c.Header("Cache-Control", "no-cache")
2024-01-01 12:25:53 +00:00
c.Data(http.StatusOK, "text/html; charset=utf-8", indexPageData)
2023-04-22 12:39:27 +00:00
})
}