mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-11-17 13:43:42 +08:00
🔖 chore: migration constants
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
package common
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"one-api/common/config"
|
||||
)
|
||||
|
||||
func LogQuota(quota int) string {
|
||||
if DisplayInCurrencyEnabled {
|
||||
return fmt.Sprintf("$%.6f 额度", float64(quota)/QuotaPerUnit)
|
||||
if config.DisplayInCurrencyEnabled {
|
||||
return fmt.Sprintf("$%.6f 额度", float64(quota)/config.QuotaPerUnit)
|
||||
} else {
|
||||
return fmt.Sprintf("%d 点额度", quota)
|
||||
}
|
||||
|
||||
@@ -5,37 +5,22 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"one-api/cli"
|
||||
"one-api/common"
|
||||
"one-api/common/utils"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func InitConf() {
|
||||
cli.FlagConfig()
|
||||
defaultConfig()
|
||||
setConfigFile()
|
||||
setEnv()
|
||||
|
||||
if viper.GetBool("debug") {
|
||||
logger.SysLog("running in debug mode")
|
||||
}
|
||||
|
||||
common.IsMasterNode = viper.GetString("node_type") != "slave"
|
||||
common.RequestInterval = time.Duration(viper.GetInt("polling_interval")) * time.Second
|
||||
common.SessionSecret = utils.GetOrDefault("session_secret", common.SessionSecret)
|
||||
}
|
||||
|
||||
func setConfigFile() {
|
||||
if !utils.IsFileExist(*cli.Config) {
|
||||
return
|
||||
}
|
||||
|
||||
viper.SetConfigFile(*cli.Config)
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
IsMasterNode = viper.GetString("node_type") != "slave"
|
||||
RequestInterval = time.Duration(viper.GetInt("polling_interval")) * time.Second
|
||||
SessionSecret = utils.GetOrDefault("session_secret", SessionSecret)
|
||||
}
|
||||
|
||||
func setEnv() {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package common
|
||||
package config
|
||||
|
||||
import (
|
||||
"sync"
|
||||
@@ -3,7 +3,7 @@ package channel
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"one-api/common"
|
||||
"one-api/common/config"
|
||||
"one-api/common/stmp"
|
||||
|
||||
"github.com/gomarkdown/markdown"
|
||||
@@ -28,10 +28,10 @@ func (e *Email) Name() string {
|
||||
func (e *Email) Send(ctx context.Context, title, message string) error {
|
||||
to := e.To
|
||||
if to == "" {
|
||||
to = common.RootUserEmail
|
||||
to = config.RootUserEmail
|
||||
}
|
||||
|
||||
if common.SMTPServer == "" || common.SMTPAccount == "" || common.SMTPToken == "" || to == "" {
|
||||
if config.SMTPServer == "" || config.SMTPAccount == "" || config.SMTPToken == "" || to == "" {
|
||||
return errors.New("smtp config is not set, skip send email notifier")
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ func (e *Email) Send(ctx context.Context, title, message string) error {
|
||||
|
||||
body := markdown.Render(doc, renderer)
|
||||
|
||||
emailClient := stmp.NewStmp(common.SMTPServer, common.SMTPPort, common.SMTPAccount, common.SMTPToken, common.SMTPFrom)
|
||||
emailClient := stmp.NewStmp(config.SMTPServer, config.SMTPPort, config.SMTPAccount, config.SMTPToken, config.SMTPFrom)
|
||||
|
||||
return emailClient.Send(to, title, string(body))
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
"one-api/common/config"
|
||||
"one-api/common/logger"
|
||||
"time"
|
||||
|
||||
@@ -41,7 +42,7 @@ func InitRedisClient() (err error) {
|
||||
} else {
|
||||
RedisEnabled = true
|
||||
// for compatibility with old versions
|
||||
MemoryCacheEnabled = true
|
||||
config.MemoryCacheEnabled = true
|
||||
}
|
||||
|
||||
return err
|
||||
|
||||
@@ -3,6 +3,7 @@ package stmp
|
||||
import (
|
||||
"fmt"
|
||||
"one-api/common"
|
||||
"one-api/common/config"
|
||||
"one-api/common/utils"
|
||||
"strings"
|
||||
|
||||
@@ -38,7 +39,7 @@ func (s *StmpConfig) Send(to, subject, body string) error {
|
||||
message.Subject(subject)
|
||||
message.SetGenHeader("References", s.getReferences())
|
||||
message.SetBodyString(mail.TypeTextHTML, body)
|
||||
message.SetUserAgent(fmt.Sprintf("One API %s // https://github.com/MartialBE/one-api", common.Version))
|
||||
message.SetUserAgent(fmt.Sprintf("One API %s // https://github.com/MartialBE/one-api", config.Version))
|
||||
|
||||
client, err := mail.NewClient(
|
||||
s.Host,
|
||||
@@ -78,11 +79,11 @@ func (s *StmpConfig) Render(to, subject, content string) error {
|
||||
}
|
||||
|
||||
func GetSystemStmp() (*StmpConfig, error) {
|
||||
if common.SMTPServer == "" || common.SMTPPort == 0 || common.SMTPAccount == "" || common.SMTPToken == "" {
|
||||
if config.SMTPServer == "" || config.SMTPPort == 0 || config.SMTPAccount == "" || config.SMTPToken == "" {
|
||||
return nil, fmt.Errorf("SMTP 信息未配置")
|
||||
}
|
||||
|
||||
return NewStmp(common.SMTPServer, common.SMTPPort, common.SMTPAccount, common.SMTPToken, common.SMTPFrom), nil
|
||||
return NewStmp(config.SMTPServer, config.SMTPPort, config.SMTPAccount, config.SMTPToken, config.SMTPFrom), nil
|
||||
}
|
||||
|
||||
func SendPasswordResetEmail(userName, email, link string) error {
|
||||
@@ -106,7 +107,7 @@ func SendPasswordResetEmail(userName, email, link string) error {
|
||||
</p>
|
||||
<p style="color: #858585;">重置链接 %d 分钟内有效,如果不是本人操作,请忽略。</p>`
|
||||
|
||||
subject := fmt.Sprintf("%s密码重置", common.SystemName)
|
||||
subject := fmt.Sprintf("%s密码重置", config.SystemName)
|
||||
content := fmt.Sprintf(contentTemp, userName, link, link, common.VerificationValidMinutes)
|
||||
|
||||
return stmp.Render(email, subject, content)
|
||||
@@ -132,7 +133,7 @@ func SendVerificationCodeEmail(email, code string) error {
|
||||
验证码 %d 分钟内有效,如果不是本人操作,请忽略。
|
||||
</p>`
|
||||
|
||||
subject := fmt.Sprintf("%s邮箱验证邮件", common.SystemName)
|
||||
subject := fmt.Sprintf("%s邮箱验证邮件", config.SystemName)
|
||||
content := fmt.Sprintf(contentTemp, code, common.VerificationValidMinutes)
|
||||
|
||||
return stmp.Render(email, subject, content)
|
||||
@@ -162,7 +163,7 @@ func SendQuotaWarningCodeEmail(userName, email string, quota int, noMoreQuota bo
|
||||
if noMoreQuota {
|
||||
subject = "您的额度已用尽"
|
||||
}
|
||||
topUpLink := fmt.Sprintf("%s/topup", common.ServerAddress)
|
||||
topUpLink := fmt.Sprintf("%s/topup", config.ServerAddress)
|
||||
|
||||
content := fmt.Sprintf(contentTemp, userName, subject, quota, topUpLink, topUpLink)
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package stmp_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"one-api/common/config"
|
||||
"testing"
|
||||
|
||||
"one-api/common"
|
||||
@@ -56,7 +57,7 @@ func TestSend(t *testing.T) {
|
||||
验证码 %d 分钟内有效,如果不是本人操作,请忽略。
|
||||
</p>`
|
||||
|
||||
subject := fmt.Sprintf("%s邮箱验证邮件", common.SystemName)
|
||||
subject := fmt.Sprintf("%s邮箱验证邮件", config.SystemName)
|
||||
content := fmt.Sprintf(contentTemp, code, common.VerificationValidMinutes)
|
||||
|
||||
err := stmpClient.Render(email, subject, content)
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
package stmp
|
||||
|
||||
import (
|
||||
"one-api/common"
|
||||
"one-api/common/config"
|
||||
)
|
||||
|
||||
func getLogo() string {
|
||||
if common.Logo == "" {
|
||||
if config.Logo == "" {
|
||||
return ""
|
||||
}
|
||||
return `<table class="logo" width="100%">
|
||||
<tr>
|
||||
<td>
|
||||
<img src="` + common.Logo + `" width="130" style="max-width: 100%"
|
||||
<img src="` + config.Logo + `" width="130" style="max-width: 100%"
|
||||
/>
|
||||
</td>
|
||||
</tr>
|
||||
@@ -19,11 +19,11 @@ func getLogo() string {
|
||||
}
|
||||
|
||||
func getSystemName() string {
|
||||
if common.SystemName == "" {
|
||||
if config.SystemName == "" {
|
||||
return "One API"
|
||||
}
|
||||
|
||||
return common.SystemName
|
||||
return config.SystemName
|
||||
}
|
||||
|
||||
func getDefaultTemplate(content string) string {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"one-api/common"
|
||||
"one-api/common/config"
|
||||
"one-api/common/utils"
|
||||
"strings"
|
||||
|
||||
@@ -24,8 +24,8 @@ func commandAffStart(b *gotgbot.Bot, ctx *ext.Context) error {
|
||||
}
|
||||
|
||||
messae := "您可以通过分享您的邀请码来邀请朋友,每次成功邀请将获得奖励。\n\n您的邀请码是: " + user.AffCode
|
||||
if common.ServerAddress != "" {
|
||||
serverAddress := strings.TrimSuffix(common.ServerAddress, "/")
|
||||
if config.ServerAddress != "" {
|
||||
serverAddress := strings.TrimSuffix(config.ServerAddress, "/")
|
||||
messae += "\n\n页面地址:" + serverAddress + "/register?aff=" + user.AffCode
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ package telegram
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"one-api/common"
|
||||
"one-api/common/config"
|
||||
"one-api/model"
|
||||
"strings"
|
||||
|
||||
@@ -56,7 +56,7 @@ func getApikeyList(userId, page int) (message string, pageParams *paginationPara
|
||||
}
|
||||
|
||||
chatUrlTmp := ""
|
||||
if common.ServerAddress != "" {
|
||||
if config.ServerAddress != "" {
|
||||
chatUrlTmp = getChatUrl()
|
||||
}
|
||||
|
||||
@@ -75,11 +75,11 @@ func getApikeyList(userId, page int) (message string, pageParams *paginationPara
|
||||
}
|
||||
|
||||
func getChatUrl() string {
|
||||
serverAddress := strings.TrimSuffix(common.ServerAddress, "/")
|
||||
serverAddress := strings.TrimSuffix(config.ServerAddress, "/")
|
||||
chatNextUrl := fmt.Sprintf(`{"key":"setToken","url":"%s"}`, serverAddress)
|
||||
chatNextUrl = "https://chat.oneapi.pro/#/?settings=" + url.QueryEscape(chatNextUrl)
|
||||
if common.ChatLink != "" {
|
||||
chatLink := strings.TrimSuffix(common.ChatLink, "/")
|
||||
if config.ChatLink != "" {
|
||||
chatLink := strings.TrimSuffix(config.ChatLink, "/")
|
||||
chatNextUrl = strings.ReplaceAll(chatNextUrl, `https://chat.oneapi.pro`, chatLink)
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"one-api/common"
|
||||
"one-api/common/config"
|
||||
"one-api/common/logger"
|
||||
"one-api/model"
|
||||
"strings"
|
||||
@@ -56,13 +56,13 @@ func InitTelegramBot() {
|
||||
func StartTelegramBot() {
|
||||
botWebhook := viper.GetString("tg.webhook_secret")
|
||||
if botWebhook != "" {
|
||||
if common.ServerAddress == "" {
|
||||
if config.ServerAddress == "" {
|
||||
logger.SysLog("Telegram bot is not enabled: Server address is not set")
|
||||
StopTelegramBot()
|
||||
return
|
||||
}
|
||||
TGWebHookSecret = botWebhook
|
||||
serverAddress := strings.TrimSuffix(common.ServerAddress, "/")
|
||||
serverAddress := strings.TrimSuffix(config.ServerAddress, "/")
|
||||
urlPath := fmt.Sprintf("/api/telegram/%s", viper.GetString("tg.bot_api_key"))
|
||||
|
||||
webHookOpts := &ext.AddWebhookOpts{
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"one-api/common/config"
|
||||
"one-api/common/logger"
|
||||
"strings"
|
||||
|
||||
@@ -21,7 +22,7 @@ var gpt4oTokenEncoder *tiktoken.Tiktoken
|
||||
|
||||
func InitTokenEncoders() {
|
||||
if viper.GetBool("disable_token_encoders") {
|
||||
DISABLE_TOKEN_ENCODERS = true
|
||||
config.DISABLE_TOKEN_ENCODERS = true
|
||||
logger.SysLog("token encoders disabled")
|
||||
return
|
||||
}
|
||||
@@ -46,7 +47,7 @@ func InitTokenEncoders() {
|
||||
}
|
||||
|
||||
func getTokenEncoder(model string) *tiktoken.Tiktoken {
|
||||
if DISABLE_TOKEN_ENCODERS {
|
||||
if config.DISABLE_TOKEN_ENCODERS {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -75,7 +76,7 @@ func getTokenEncoder(model string) *tiktoken.Tiktoken {
|
||||
}
|
||||
|
||||
func getTokenNum(tokenEncoder *tiktoken.Tiktoken, text string) int {
|
||||
if DISABLE_TOKEN_ENCODERS || ApproximateTokenEnabled {
|
||||
if config.DISABLE_TOKEN_ENCODERS || config.ApproximateTokenEnabled {
|
||||
return int(float64(len(text)) * 0.38)
|
||||
}
|
||||
return len(tokenEncoder.Encode(text, nil, nil))
|
||||
|
||||
Reference in New Issue
Block a user