63 lines
1.6 KiB
Go
63 lines
1.6 KiB
Go
package router
|
|
|
|
import (
|
|
"embed"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/QuantumNous/new-api/common"
|
|
"github.com/QuantumNous/new-api/middleware"
|
|
"github.com/QuantumNous/new-api/setting/operation_setting"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func SetRouter(router *gin.Engine, buildFS embed.FS, indexPage []byte) {
|
|
SetApiRouter(router)
|
|
SetDashboardRouter(router)
|
|
SetRelayRouter(router)
|
|
SetVideoRouter(router)
|
|
|
|
// 文档重定向:拦截 /docs 和 /{lang}/docs 路径,转发到配置的 docs_link
|
|
router.Use(func(c *gin.Context) {
|
|
path := c.Request.URL.Path
|
|
var prefix string
|
|
if path == "/docs" || strings.HasPrefix(path, "/docs/") {
|
|
prefix = "/docs"
|
|
} else {
|
|
for _, lp := range []string{"/zh/docs", "/en/docs", "/ja/docs"} {
|
|
if path == lp || strings.HasPrefix(path, lp+"/") {
|
|
prefix = lp
|
|
break
|
|
}
|
|
}
|
|
}
|
|
if prefix == "" {
|
|
c.Next()
|
|
return
|
|
}
|
|
docsLink := operation_setting.GetGeneralSetting().DocsLink
|
|
subPath := strings.TrimPrefix(path, prefix)
|
|
target := strings.TrimRight(docsLink, "/") + subPath
|
|
c.Redirect(http.StatusFound, target)
|
|
c.Abort()
|
|
})
|
|
|
|
frontendBaseUrl := os.Getenv("FRONTEND_BASE_URL")
|
|
if common.IsMasterNode && frontendBaseUrl != "" {
|
|
frontendBaseUrl = ""
|
|
common.SysLog("FRONTEND_BASE_URL is ignored on master node")
|
|
}
|
|
if frontendBaseUrl == "" {
|
|
SetWebRouter(router, buildFS, indexPage)
|
|
} else {
|
|
frontendBaseUrl = strings.TrimSuffix(frontendBaseUrl, "/")
|
|
router.NoRoute(func(c *gin.Context) {
|
|
c.Set(middleware.RouteTagKey, "web")
|
|
c.Redirect(http.StatusMovedPermanently, fmt.Sprintf("%s%s", frontendBaseUrl, c.Request.RequestURI))
|
|
})
|
|
}
|
|
}
|