🔖 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

@@ -8,6 +8,7 @@ import (
"net/http/httptest"
"one-api/common"
"one-api/common/notify"
"one-api/common/utils"
"one-api/model"
"one-api/providers"
providers_base "one-api/providers/base"
@@ -153,7 +154,7 @@ func testAllChannels(isNotify bool) error {
time.Sleep(common.RequestInterval)
isChannelEnabled := channel.Status == common.ChannelStatusEnabled
sendMessage += fmt.Sprintf("**通道 %s - #%d - %s** : \n\n", common.EscapeMarkdownText(channel.Name), channel.Id, channel.StatusToStr())
sendMessage += fmt.Sprintf("**通道 %s - #%d - %s** : \n\n", utils.EscapeMarkdownText(channel.Name), channel.Id, channel.StatusToStr())
tik := time.Now()
err, openaiErr := testChannel(channel, "")
tok := time.Now()
@@ -161,7 +162,7 @@ func testAllChannels(isNotify bool) error {
// 通道为禁用状态,并且还是请求错误 或者 响应时间超过阈值 直接跳过,也不需要更新响应时间。
if !isChannelEnabled {
if err != nil {
sendMessage += fmt.Sprintf("- 测试报错: %s \n\n- 无需改变状态,跳过\n\n", common.EscapeMarkdownText(err.Error()))
sendMessage += fmt.Sprintf("- 测试报错: %s \n\n- 无需改变状态,跳过\n\n", utils.EscapeMarkdownText(err.Error()))
continue
}
if milliseconds > disableThreshold {
@@ -187,13 +188,13 @@ func testAllChannels(isNotify bool) error {
}
if ShouldDisableChannel(openaiErr, -1) {
sendMessage += fmt.Sprintf("- 已被禁用,原因:%s\n\n", common.EscapeMarkdownText(err.Error()))
sendMessage += fmt.Sprintf("- 已被禁用,原因:%s\n\n", utils.EscapeMarkdownText(err.Error()))
DisableChannel(channel.Id, channel.Name, err.Error(), false)
continue
}
if err != nil {
sendMessage += fmt.Sprintf("- 测试报错: %s \n\n", common.EscapeMarkdownText(err.Error()))
sendMessage += fmt.Sprintf("- 测试报错: %s \n\n", utils.EscapeMarkdownText(err.Error()))
continue
}
}

View File

@@ -4,6 +4,7 @@ import (
"errors"
"net/http"
"one-api/common"
"one-api/common/utils"
"one-api/model"
"strconv"
"strings"
@@ -64,7 +65,7 @@ func AddChannel(c *gin.Context) {
})
return
}
channel.CreatedTime = common.GetTimestamp()
channel.CreatedTime = utils.GetTimestamp()
keys := strings.Split(channel.Key, "\n")
channels := make([]model.Channel, 0, len(keys))
for _, key := range keys {

View File

@@ -7,6 +7,7 @@ import (
"fmt"
"net/http"
"one-api/common"
"one-api/common/utils"
"one-api/model"
"strconv"
"time"
@@ -216,7 +217,7 @@ func GitHubBind(c *gin.Context) {
func GenerateOAuthCode(c *gin.Context) {
session := sessions.Default(c)
state := common.GetRandomString(12)
state := utils.GetRandomString(12)
session.Set("oauth_state", state)
err := session.Save()
if err != nil {

View File

@@ -4,6 +4,7 @@ import (
"encoding/json"
"net/http"
"one-api/common"
"one-api/common/utils"
"one-api/model"
"strings"
@@ -19,7 +20,7 @@ func GetOptions(c *gin.Context) {
}
options = append(options, &model.Option{
Key: k,
Value: common.Interface2String(v),
Value: utils.Interface2String(v),
})
}
common.OptionMapRWMutex.Unlock()

View File

@@ -6,7 +6,7 @@ import (
"net/url"
"one-api/common"
"one-api/model"
"one-api/relay/util"
"one-api/relay/relay_util"
"github.com/gin-gonic/gin"
)
@@ -14,7 +14,7 @@ import (
func GetPricesList(c *gin.Context) {
pricesType := c.DefaultQuery("type", "db")
prices := util.GetPricesList(pricesType)
prices := relay_util.GetPricesList(pricesType)
if len(prices) == 0 {
common.APIRespondWithError(c, http.StatusOK, errors.New("pricing data not found"))
@@ -33,7 +33,7 @@ func GetPricesList(c *gin.Context) {
}
func GetAllModelList(c *gin.Context) {
prices := util.PricingInstance.GetAllPrices()
prices := relay_util.PricingInstance.GetAllPrices()
channelModel := model.ChannelGroup.Rule
modelsMap := make(map[string]bool)
@@ -68,7 +68,7 @@ func AddPrice(c *gin.Context) {
return
}
if err := util.PricingInstance.AddPrice(&price); err != nil {
if err := relay_util.PricingInstance.AddPrice(&price); err != nil {
common.APIRespondWithError(c, http.StatusOK, err)
return
}
@@ -94,7 +94,7 @@ func UpdatePrice(c *gin.Context) {
return
}
if err := util.PricingInstance.UpdatePrice(modelName, &price); err != nil {
if err := relay_util.PricingInstance.UpdatePrice(modelName, &price); err != nil {
common.APIRespondWithError(c, http.StatusOK, err)
return
}
@@ -114,7 +114,7 @@ func DeletePrice(c *gin.Context) {
modelName = modelName[1:]
modelName, _ = url.PathUnescape(modelName)
if err := util.PricingInstance.DeletePrice(modelName); err != nil {
if err := relay_util.PricingInstance.DeletePrice(modelName); err != nil {
common.APIRespondWithError(c, http.StatusOK, err)
return
}
@@ -127,7 +127,7 @@ func DeletePrice(c *gin.Context) {
type PriceBatchRequest struct {
OriginalModels []string `json:"original_models"`
util.BatchPrices
relay_util.BatchPrices
}
func BatchSetPrices(c *gin.Context) {
@@ -137,7 +137,7 @@ func BatchSetPrices(c *gin.Context) {
return
}
if err := util.PricingInstance.BatchSetPrices(&pricesBatch.BatchPrices, pricesBatch.OriginalModels); err != nil {
if err := relay_util.PricingInstance.BatchSetPrices(&pricesBatch.BatchPrices, pricesBatch.OriginalModels); err != nil {
common.APIRespondWithError(c, http.StatusOK, err)
return
}
@@ -159,7 +159,7 @@ func BatchDeletePrices(c *gin.Context) {
return
}
if err := util.PricingInstance.BatchDeletePrices(pricesBatch.Models); err != nil {
if err := relay_util.PricingInstance.BatchDeletePrices(pricesBatch.Models); err != nil {
common.APIRespondWithError(c, http.StatusOK, err)
return
}
@@ -184,7 +184,7 @@ func SyncPricing(c *gin.Context) {
return
}
err := util.PricingInstance.SyncPricing(prices, overwrite == "true")
err := relay_util.PricingInstance.SyncPricing(prices, overwrite == "true")
if err != nil {
common.APIRespondWithError(c, http.StatusOK, err)
return

View File

@@ -3,6 +3,7 @@ package controller
import (
"net/http"
"one-api/common"
"one-api/common/utils"
"one-api/model"
"strconv"
@@ -85,12 +86,12 @@ func AddRedemption(c *gin.Context) {
}
var keys []string
for i := 0; i < redemption.Count; i++ {
key := common.GetUUID()
key := utils.GetUUID()
cleanRedemption := model.Redemption{
UserId: c.GetInt("id"),
Name: redemption.Name,
Key: key,
CreatedTime: common.GetTimestamp(),
CreatedTime: utils.GetTimestamp(),
Quota: redemption.Quota,
}
err = cleanRedemption.Insert()

View File

@@ -3,6 +3,7 @@ package controller
import (
"net/http"
"one-api/common"
"one-api/common/utils"
"one-api/model"
"strconv"
@@ -62,9 +63,9 @@ func GetPlaygroundToken(c *gin.Context) {
cleanToken := model.Token{
UserId: userId,
Name: tokenName,
Key: common.GenerateKey(),
CreatedTime: common.GetTimestamp(),
AccessedTime: common.GetTimestamp(),
Key: utils.GenerateKey(),
CreatedTime: utils.GetTimestamp(),
AccessedTime: utils.GetTimestamp(),
ExpiredTime: 0,
RemainQuota: 0,
UnlimitedQuota: true,
@@ -132,9 +133,9 @@ func AddToken(c *gin.Context) {
cleanToken := model.Token{
UserId: c.GetInt("id"),
Name: token.Name,
Key: common.GenerateKey(),
CreatedTime: common.GetTimestamp(),
AccessedTime: common.GetTimestamp(),
Key: utils.GenerateKey(),
CreatedTime: utils.GetTimestamp(),
AccessedTime: utils.GetTimestamp(),
ExpiredTime: token.ExpiredTime,
RemainQuota: token.RemainQuota,
UnlimitedQuota: token.UnlimitedQuota,
@@ -199,7 +200,7 @@ func UpdateToken(c *gin.Context) {
return
}
if token.Status == common.TokenStatusEnabled {
if cleanToken.Status == common.TokenStatusExpired && cleanToken.ExpiredTime <= common.GetTimestamp() && cleanToken.ExpiredTime != -1 {
if cleanToken.Status == common.TokenStatusExpired && cleanToken.ExpiredTime <= utils.GetTimestamp() && cleanToken.ExpiredTime != -1 {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "令牌已过期,无法启用,请先修改令牌过期时间,或者设置为永不过期",

View File

@@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"one-api/common"
"one-api/common/utils"
"one-api/model"
"strconv"
"time"
@@ -261,7 +262,7 @@ func GenerateAccessToken(c *gin.Context) {
})
return
}
user.AccessToken = common.GetUUID()
user.AccessToken = utils.GetUUID()
if model.DB.Where("access_token = ?", user.AccessToken).First(user).RowsAffected != 0 {
c.JSON(http.StatusOK, gin.H{
@@ -297,7 +298,7 @@ func GetAffCode(c *gin.Context) {
return
}
if user.AffCode == "" {
user.AffCode = common.GetRandomString(4)
user.AffCode = utils.GetRandomString(4)
if err := user.Update(false); err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,