feat: return a not found response if requested a wrong API endpoints

This commit is contained in:
JustSong 2023-06-17 09:46:07 +08:00
parent 57b213a035
commit 70ed126ccb
2 changed files with 18 additions and 0 deletions

View File

@ -398,3 +398,15 @@ func RelayNotImplemented(c *gin.Context) {
"error": err, "error": err,
}) })
} }
func RelayNotFound(c *gin.Context) {
err := OpenAIError{
Message: fmt.Sprintf("API not found: %s:%s", c.Request.Method, c.Request.URL.Path),
Type: "one_api_error",
Param: "",
Code: "api_not_found",
}
c.JSON(http.StatusOK, gin.H{
"error": err,
})
}

View File

@ -7,7 +7,9 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"net/http" "net/http"
"one-api/common" "one-api/common"
"one-api/controller"
"one-api/middleware" "one-api/middleware"
"strings"
) )
func SetWebRouter(router *gin.Engine, buildFS embed.FS, indexPage []byte) { func SetWebRouter(router *gin.Engine, buildFS embed.FS, indexPage []byte) {
@ -16,6 +18,10 @@ func SetWebRouter(router *gin.Engine, buildFS embed.FS, indexPage []byte) {
router.Use(middleware.Cache()) router.Use(middleware.Cache())
router.Use(static.Serve("/", common.EmbedFolder(buildFS, "web/build"))) router.Use(static.Serve("/", common.EmbedFolder(buildFS, "web/build")))
router.NoRoute(func(c *gin.Context) { router.NoRoute(func(c *gin.Context) {
if strings.HasPrefix(c.Request.RequestURI, "/v1") {
controller.RelayNotFound(c)
return
}
c.Header("Cache-Control", "no-cache") c.Header("Cache-Control", "no-cache")
c.Data(http.StatusOK, "text/html; charset=utf-8", indexPage) c.Data(http.StatusOK, "text/html; charset=utf-8", indexPage)
}) })