Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

38 lignes
1.2 KiB

  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. // LOGO_FILE_PATH 优先级最高:注册 /logo.png 路由提供磁盘文件
  15. // 必须在 static.Serve("/") 之前注册,否则会被 embed 静态文件拦截
  16. if common.LogoFilePath != "" {
  17. router.GET("/logo.png", func(c *gin.Context) {
  18. c.File(common.LogoFilePath)
  19. })
  20. }
  21. router.Use(gzip.Gzip(gzip.DefaultCompression))
  22. router.Use(middleware.GlobalWebRateLimit())
  23. router.Use(middleware.Cache())
  24. router.Use(static.Serve("/", common.EmbedFolder(buildFS, "web/dist")))
  25. router.NoRoute(func(c *gin.Context) {
  26. if strings.HasPrefix(c.Request.RequestURI, "/v1") || strings.HasPrefix(c.Request.RequestURI, "/api") || strings.HasPrefix(c.Request.RequestURI, "/assets") {
  27. controller.RelayNotFound(c)
  28. return
  29. }
  30. c.Header("Cache-Control", "no-cache")
  31. c.Data(http.StatusOK, "text/html; charset=utf-8", indexPage)
  32. })
  33. }