🔖 chore: Rename relay/util to relay/relay_util package and add utils package

This commit is contained in:
MartialBE
2024-05-29 00:36:54 +08:00
parent 853f2681f4
commit 79524108a3
61 changed files with 309 additions and 265 deletions

View File

@@ -3,6 +3,7 @@ package middleware
import (
"net/http"
"one-api/common"
"one-api/common/utils"
"one-api/model"
"strings"
@@ -109,10 +110,10 @@ func tokenAuth(c *gin.Context, key string) {
if len(parts) > 1 {
if model.IsAdmin(token.UserId) {
if strings.HasPrefix(parts[1], "!") {
channelId := common.String2Int(parts[1][1:])
channelId := utils.String2Int(parts[1][1:])
c.Set("skip_channel_id", channelId)
} else {
channelId := common.String2Int(parts[1])
channelId := utils.String2Int(parts[1])
if channelId == 0 {
abortWithMessage(c, http.StatusForbidden, "无效的渠道 Id")
return

View File

@@ -4,6 +4,7 @@ import (
"context"
"net/http"
"one-api/common"
"one-api/common/utils"
"time"
"github.com/gin-gonic/gin"
@@ -99,11 +100,11 @@ func rateLimitFactory(maxRequestNum int, duration int64, mark string) func(c *gi
}
func GlobalWebRateLimit() func(c *gin.Context) {
return rateLimitFactory(common.GetOrDefault("global.web_rate_limit", GlobalWebRateLimitNum), GlobalWebRateLimitDuration, "GW")
return rateLimitFactory(utils.GetOrDefault("global.web_rate_limit", GlobalWebRateLimitNum), GlobalWebRateLimitDuration, "GW")
}
func GlobalAPIRateLimit() func(c *gin.Context) {
return rateLimitFactory(common.GetOrDefault("global.api_rate_limit", GlobalApiRateLimitNum), GlobalApiRateLimitDuration, "GA")
return rateLimitFactory(utils.GetOrDefault("global.api_rate_limit", GlobalApiRateLimitNum), GlobalApiRateLimitDuration, "GA")
}
func CriticalRateLimit() func(c *gin.Context) {

View File

@@ -3,6 +3,7 @@ package middleware
import (
"context"
"one-api/common"
"one-api/common/utils"
"time"
"github.com/gin-gonic/gin"
@@ -10,7 +11,7 @@ import (
func RequestId() func(c *gin.Context) {
return func(c *gin.Context) {
id := common.GetTimeString() + common.GetRandomString(8)
id := utils.GetTimeString() + utils.GetRandomString(8)
c.Set(common.RequestIdKey, id)
ctx := context.WithValue(c.Request.Context(), common.RequestIdKey, id)
ctx = context.WithValue(ctx, "requestStartTime", time.Now())

View File

@@ -1,14 +1,16 @@
package middleware
import (
"github.com/gin-gonic/gin"
"one-api/common"
"one-api/common/utils"
"github.com/gin-gonic/gin"
)
func abortWithMessage(c *gin.Context, statusCode int, message string) {
c.JSON(statusCode, gin.H{
"error": gin.H{
"message": common.MessageWithRequestId(message, c.GetString(common.RequestIdKey)),
"message": utils.MessageWithRequestId(message, c.GetString(common.RequestIdKey)),
"type": "one_api_error",
},
})