web-router.go 839 B

12345678910111213141516171819202122232425262728
  1. package router
  2. import (
  3. "embed"
  4. "github.com/gin-contrib/gzip"
  5. "github.com/gin-contrib/static"
  6. "github.com/gin-gonic/gin"
  7. "net/http"
  8. "one-api/common"
  9. "one-api/controller"
  10. "one-api/middleware"
  11. "strings"
  12. )
  13. func SetWebRouter(router *gin.Engine, buildFS embed.FS, indexPage []byte) {
  14. router.Use(gzip.Gzip(gzip.DefaultCompression))
  15. router.Use(middleware.GlobalWebRateLimit())
  16. router.Use(middleware.Cache())
  17. router.Use(static.Serve("/", common.EmbedFolder(buildFS, "web/dist")))
  18. router.NoRoute(func(c *gin.Context) {
  19. if strings.HasPrefix(c.Request.RequestURI, "/v1") || strings.HasPrefix(c.Request.RequestURI, "/api") || strings.HasPrefix(c.Request.RequestURI, "/assets") {
  20. controller.RelayNotFound(c)
  21. return
  22. }
  23. c.Header("Cache-Control", "no-cache")
  24. c.Data(http.StatusOK, "text/html; charset=utf-8", indexPage)
  25. })
  26. }