Browse Source

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

JustSong 2 years ago
parent
commit
70ed126ccb
2 changed files with 18 additions and 0 deletions
  1. 12 0
      controller/relay.go
  2. 6 0
      router/web-router.go

+ 12 - 0
controller/relay.go

@@ -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,
+	})
+}

+ 6 - 0
router/web-router.go

@@ -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)
 	})
 	})