mirror of
https://github.com/songquanpeng/one-api.git
synced 2026-02-04 05:05:57 +08:00
feat: allow baseUrl
This commit is contained in:
@@ -9,7 +9,7 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func SetApiRouter(router *gin.Engine) {
|
||||
func SetApiRouter(router *gin.RouterGroup) {
|
||||
apiRouter := router.Group("/api")
|
||||
apiRouter.Use(gzip.Gzip(gzip.DefaultCompression))
|
||||
apiRouter.Use(middleware.GlobalAPIRateLimit())
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"github.com/songquanpeng/one-api/middleware"
|
||||
)
|
||||
|
||||
func SetDashboardRouter(router *gin.Engine) {
|
||||
func SetDashboardRouter(router *gin.RouterGroup) {
|
||||
apiRouter := router.Group("/")
|
||||
apiRouter.Use(middleware.CORS())
|
||||
apiRouter.Use(gzip.Gzip(gzip.DefaultCompression))
|
||||
|
||||
@@ -11,7 +11,13 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func SetRouter(router *gin.Engine, buildFS embed.FS) {
|
||||
func SetRouter(engine *gin.Engine, buildFS embed.FS) {
|
||||
var baseUrl = os.Getenv("BASE_URL")
|
||||
if baseUrl == "" {
|
||||
baseUrl = "/"
|
||||
}
|
||||
router := engine.Group(baseUrl)
|
||||
|
||||
SetApiRouter(router)
|
||||
SetDashboardRouter(router)
|
||||
SetRelayRouter(router)
|
||||
@@ -21,10 +27,10 @@ func SetRouter(router *gin.Engine, buildFS embed.FS) {
|
||||
logger.SysLog("FRONTEND_BASE_URL is ignored on master node")
|
||||
}
|
||||
if frontendBaseUrl == "" {
|
||||
SetWebRouter(router, buildFS)
|
||||
SetWebRouter(engine, baseUrl, buildFS)
|
||||
} else {
|
||||
frontendBaseUrl = strings.TrimSuffix(frontendBaseUrl, "/")
|
||||
router.NoRoute(func(c *gin.Context) {
|
||||
engine.NoRoute(func(c *gin.Context) {
|
||||
c.Redirect(http.StatusMovedPermanently, fmt.Sprintf("%s%s", frontendBaseUrl, c.Request.RequestURI))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func SetRelayRouter(router *gin.Engine) {
|
||||
func SetRelayRouter(router *gin.RouterGroup) {
|
||||
router.Use(middleware.CORS())
|
||||
// https://platform.openai.com/docs/api-reference/introduction
|
||||
modelsRouter := router.Group("/v1/models")
|
||||
|
||||
@@ -14,13 +14,15 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
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))
|
||||
router.Use(middleware.GlobalWebRateLimit())
|
||||
router.Use(middleware.Cache())
|
||||
router.Use(static.Serve("/", common.EmbedFolder(buildFS, fmt.Sprintf("web/build/%s", config.Theme))))
|
||||
router.NoRoute(func(c *gin.Context) {
|
||||
func SetWebRouter(engine *gin.Engine, baseUrl string, buildFS embed.FS) {
|
||||
basePath := fmt.Sprintf("web/build/%s", config.Theme)
|
||||
indexPageData, _ := buildFS.ReadFile(fmt.Sprintf("%s/index.html", basePath))
|
||||
engine.Use(gzip.Gzip(gzip.DefaultCompression))
|
||||
engine.Use(middleware.GlobalWebRateLimit())
|
||||
engine.Use(middleware.Cache())
|
||||
engine.Use(static.Serve(baseUrl, common.EmbedFolder(buildFS, basePath)))
|
||||
|
||||
engine.NoRoute(func(c *gin.Context) {
|
||||
if strings.HasPrefix(c.Request.RequestURI, "/v1") || strings.HasPrefix(c.Request.RequestURI, "/api") {
|
||||
controller.RelayNotFound(c)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user