web-router.go 948 B

123456789101112131415161718192021222324252627282930
  1. package router
  2. import (
  3. "embed"
  4. "net/http"
  5. "strings"
  6. "github.com/QuantumNous/new-api/common"
  7. "github.com/QuantumNous/new-api/controller"
  8. "github.com/QuantumNous/new-api/middleware"
  9. "github.com/gin-contrib/gzip"
  10. "github.com/gin-contrib/static"
  11. "github.com/gin-gonic/gin"
  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. c.Set(middleware.RouteTagKey, "web")
  20. if strings.HasPrefix(c.Request.RequestURI, "/v1") || strings.HasPrefix(c.Request.RequestURI, "/api") || strings.HasPrefix(c.Request.RequestURI, "/assets") {
  21. controller.RelayNotFound(c)
  22. return
  23. }
  24. c.Header("Cache-Control", "no-cache")
  25. c.Data(http.StatusOK, "text/html; charset=utf-8", indexPage)
  26. })
  27. }