2023-04-22 12:39:27 +00:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
2024-01-01 10:55:03 +00:00
|
|
|
"fmt"
|
2023-04-25 13:56:07 +00:00
|
|
|
"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"
|
2023-06-17 01:46:07 +00:00
|
|
|
"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) {
|
2024-01-21 15:21:42 +00:00
|
|
|
indexPageData, _ := buildFS.ReadFile(fmt.Sprintf("web/build/%s/index.html", config.Theme))
|
2023-04-25 13:56:07 +00:00
|
|
|
router.Use(gzip.Gzip(gzip.DefaultCompression))
|
2023-04-22 12:39:27 +00:00
|
|
|
router.Use(middleware.GlobalWebRateLimit())
|
|
|
|
router.Use(middleware.Cache())
|
2024-01-21 15:21:42 +00:00
|
|
|
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") {
|
2023-06-17 01:46:07 +00:00
|
|
|
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
|
|
|
})
|
|
|
|
}
|