mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-10-28 08:35:33 +08:00
* feat(i18n): translate web console messages in the frontend and AI-client errors in the backend
Chinese text was hard-coded in API messages, stored log content, emails and
server logs, so it reached users of every interface language.
Text is now translated where its reader is:
- Web console APIs return the English source text as message_key with
message_params, and the web console translates it in all seven languages.
message keeps the English text for other API callers.
- Errors returned to AI clients, emails and notifications are translated by
the backend (en, zh-CN, zh-TW) in the language saved on the account, then
the language of the request's Accept-Language header.
- A reader who states no language still gets the original Chinese text of the
messages that were Chinese before, word for word. Gateways that use another
new-api as their upstream match that text, for example to disable a channel
whose account ran out of quota.
- Usage, top-up and system log content is stored as content_parts (key and
params) and rendered in the viewer's language. Task fail reasons and refund
reasons written by the gateway are English keys. Rows written earlier are
shown as before.
- Server logs are English.
Also:
- The saved interface language zhTW, and zh-HK, zh-MO and zh-Hant, now select
Traditional Chinese in the backend instead of Simplified Chinese.
- The built-in group descriptions are translated; descriptions an
administrator saved are shown as written.
- .agents/rules/i18n.md records the conventions. Tests in i18n/ fail on a
non-English string literal, on a web console message without translations
and on a locale key without a constant.
* feat(i18n): print server logs in Chinese when LOG_LANGUAGE asks for it
Server log lines are English by default. With LOG_LANGUAGE=zh-CN, the lines
that were Chinese before print their original Chinese text.
common.LogText formats a log line from its English format, which is also the
key of its Chinese text in common/log_text.zh-CN.json. A line without an entry
prints in English. go vet checks the arguments of every call, and
TestServerLogTexts checks that each entry still matches a call and has the
same verbs as its key.
* docs: list LOG_LANGUAGE in the docker compose example
* feat(i18n): translate every server log line and add DEFAULT_LANGUAGE
Every line the gateway prints goes through common.LogText. With
DEFAULT_LANGUAGE=zh-CN, the lines that have a Chinese text in
common/log_text.zh-CN.json print it and the others print English.
DEFAULT_LANGUAGE replaces LOG_LANGUAGE. It also sets the language of
backend messages, emails and notifications for a reader who states no
language: en, zh-CN or zh-TW, English when unset. The list of messages
that stayed Chinese for such readers is removed.
Review fixes:
- Channel test logs store the token name "Model test"; the console shows
it in the interface language.
- Task fail reasons written by the gateway are fixed sentences the console
translates. The HTTP status and the upstream detail go to the server log
when a polled task is failed.
- The auto-disable reason of a slow channel is written in DEFAULT_LANGUAGE.
Also:
- Drop the repository-scanning lint tests, the i18n rules file and its
read gate. AGENTS.md keeps the conventions in four lines.
- Payment error toasts keep their previous text and only add the
translated message key.
- Remove guards and helpers that no caller needs.
* fix(i18n): address review on language lookup, notices and error responses
- Relay requests no longer load the user a second time to find the
language: the settings TokenAuth put into the request are used as they
are, also when they hold no language.
- Upstream model update notices are rendered in each recipient's saved
language, DEFAULT_LANGUAGE when none.
- /api/usage/token and /api/log/token answer with backend-translated
messages again, with the keys and status codes used before this PR.
Their callers are scripts, not the web console.
- Webhook notifications fill {{value}} placeholders like the other
senders. Running the rendered text through Sprintf appended
%!(EXTRA ...) and broke %2F in links.
- RootUserLanguage reads the setting of the root user it already loaded.
- zh_TW and zh_HK are read as Traditional Chinese.
- Add common.ApiErrorStatus for error responses under another HTTP status
and use it where those responses were built by hand.
- Remove middleware.GetLanguage, which had no callers.
380 lines
12 KiB
Go
380 lines
12 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"embed"
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"strconv"
|
|
"strings"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/QuantumNous/new-api/common"
|
|
"github.com/QuantumNous/new-api/constant"
|
|
"github.com/QuantumNous/new-api/controller"
|
|
"github.com/QuantumNous/new-api/i18n"
|
|
"github.com/QuantumNous/new-api/logger"
|
|
"github.com/QuantumNous/new-api/middleware"
|
|
"github.com/QuantumNous/new-api/model"
|
|
"github.com/QuantumNous/new-api/oauth"
|
|
"github.com/QuantumNous/new-api/pkg/jsplugin"
|
|
perfmetrics "github.com/QuantumNous/new-api/pkg/perf_metrics"
|
|
"github.com/QuantumNous/new-api/pkg/wsmanager"
|
|
"github.com/QuantumNous/new-api/relay"
|
|
kitutil "github.com/QuantumNous/new-api/relaykit/relayconvert/kitutil"
|
|
"github.com/QuantumNous/new-api/router"
|
|
"github.com/QuantumNous/new-api/service"
|
|
"github.com/QuantumNous/new-api/service/authz"
|
|
_ "github.com/QuantumNous/new-api/setting/performance_setting"
|
|
"github.com/QuantumNous/new-api/setting/ratio_setting"
|
|
|
|
"github.com/bytedance/gopkg/util/gopool"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/joho/godotenv"
|
|
|
|
_ "net/http/pprof"
|
|
)
|
|
|
|
//go:embed web/dist
|
|
var buildFS embed.FS
|
|
|
|
//go:embed web/dist/index.html
|
|
var indexPage []byte
|
|
|
|
func main() {
|
|
if len(os.Args) > 1 && os.Args[1] == "plugin" {
|
|
os.Exit(jsplugin.RunCLI(os.Args[2:], os.Stdout, os.Stderr))
|
|
}
|
|
startTime := time.Now()
|
|
kitutil.SetLogging(common.SysLog, func(message string) {
|
|
logger.LogError(nil, message)
|
|
})
|
|
kitutil.SetSystemErrorLogging(common.SysError)
|
|
|
|
err := InitResources()
|
|
if err != nil {
|
|
common.FatalLog(common.LogText("failed to initialize resources: %s", err.Error()))
|
|
return
|
|
}
|
|
|
|
common.SysLog(common.LogText("New API %s started", common.Version))
|
|
if os.Getenv("GIN_MODE") != "debug" {
|
|
gin.SetMode(gin.ReleaseMode)
|
|
}
|
|
if common.DebugEnabled {
|
|
common.SysLog(common.LogText("running in debug mode"))
|
|
}
|
|
|
|
kitutil.Debug.Store(common.DebugEnabled)
|
|
|
|
defer func() {
|
|
err := model.CloseDB()
|
|
if err != nil {
|
|
common.FatalLog(common.LogText("failed to close database: %s", err.Error()))
|
|
}
|
|
}()
|
|
|
|
if common.RedisEnabled {
|
|
// for compatibility with old versions
|
|
common.MemoryCacheEnabled = true
|
|
}
|
|
if common.MemoryCacheEnabled {
|
|
common.SysLog(common.LogText("memory cache enabled"))
|
|
common.SysLog(common.LogText("sync frequency: %d seconds", common.SyncFrequency))
|
|
|
|
// Add panic recovery and retry for InitChannelCache
|
|
func() {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
common.SysLog(common.LogText("InitChannelCache panic: %v, retrying once", r))
|
|
// Retry once
|
|
_, _, fixErr := model.FixAbility()
|
|
if fixErr != nil {
|
|
common.FatalLog(common.LogText("InitChannelCache failed: %s", fixErr.Error()))
|
|
}
|
|
}
|
|
}()
|
|
model.InitChannelCache()
|
|
}()
|
|
|
|
go model.SyncChannelCache(common.SyncFrequency)
|
|
}
|
|
wsmanager.StartSubscriber(context.Background())
|
|
|
|
// Warm pricing after channel cache initialization so Advanced Custom
|
|
// endpoint inference can read cached route settings on first request.
|
|
model.GetPricing()
|
|
|
|
// 热更新配置
|
|
go model.SyncOptions(common.SyncFrequency)
|
|
go controller.SyncTaskPlugins()
|
|
|
|
// 周期性重载授权策略,保证多节点/多 master 部署下权限变更能传播到每个实例
|
|
go authz.StartPolicySync(common.SyncFrequency)
|
|
|
|
// 数据看板
|
|
go model.UpdateQuotaData()
|
|
|
|
if os.Getenv("CHANNEL_UPDATE_FREQUENCY") != "" {
|
|
frequency, err := strconv.Atoi(os.Getenv("CHANNEL_UPDATE_FREQUENCY"))
|
|
if err != nil {
|
|
common.FatalLog(common.LogText("failed to parse CHANNEL_UPDATE_FREQUENCY: %s", err.Error()))
|
|
}
|
|
go controller.AutomaticallyUpdateChannels(frequency)
|
|
}
|
|
|
|
// Codex credential auto-refresh check every 10 minutes, refresh when expires within 1 day
|
|
service.StartCodexCredentialAutoRefreshTask()
|
|
|
|
// Subscription quota reset task (daily/weekly/monthly/custom)
|
|
service.StartSubscriptionQuotaResetTask()
|
|
|
|
// Report this process as a system instance so the System Info page can show
|
|
// all currently alive nodes in multi-instance deployments.
|
|
service.StartSystemInstanceReporter()
|
|
|
|
// Wire task polling adaptor factory (breaks service -> relay import cycle).
|
|
// Must run before the system task runner starts: the async_task_poll handler
|
|
// calls service.RunTaskPollingOnce, which needs this factory set.
|
|
service.GetTaskAdaptorFunc = func(platform constant.TaskPlatform) service.TaskPollingAdaptor {
|
|
a := relay.GetTaskAdaptor(platform)
|
|
if a == nil {
|
|
return nil
|
|
}
|
|
return a
|
|
}
|
|
|
|
// Register the periodic channel test, upstream model update, and async task
|
|
// polling (Midjourney / Suno / video) jobs as scheduled system tasks
|
|
// (DB-lease dedup across masters + run history), then start the runner that
|
|
// schedules and executes them. Master-only execution and the UpdateTask
|
|
// switch are enforced inside the runner and each handler's Enabled().
|
|
controller.RegisterScheduledSystemTasks()
|
|
service.StartSystemTaskRunner()
|
|
|
|
if os.Getenv("BATCH_UPDATE_ENABLED") == "true" {
|
|
common.BatchUpdateEnabled = true
|
|
common.SysLog(common.LogText("batch update enabled with interval %ss", strconv.Itoa(common.BatchUpdateInterval)))
|
|
model.InitBatchUpdater()
|
|
}
|
|
|
|
if os.Getenv("ENABLE_PPROF") == "true" {
|
|
gopool.Go(func() {
|
|
log.Println(http.ListenAndServe("0.0.0.0:8005", nil))
|
|
})
|
|
go common.Monitor()
|
|
common.SysLog(common.LogText("pprof enabled"))
|
|
}
|
|
|
|
err = common.StartPyroScope()
|
|
if err != nil {
|
|
common.SysError(common.LogText("start pyroscope error : %v", err))
|
|
}
|
|
|
|
// Initialize HTTP server
|
|
server := gin.New()
|
|
if err := middleware.ConfigureTrustedProxies(server); err != nil {
|
|
common.FatalLog(common.LogText("failed to configure trusted proxies: %s", err.Error()))
|
|
return
|
|
}
|
|
server.Use(gin.CustomRecovery(func(c *gin.Context, err any) {
|
|
common.SysLog(common.LogText("panic detected: %v", err))
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
"error": gin.H{
|
|
"message": fmt.Sprintf("Panic detected, error: %v. Please submit a issue here: https://github.com/Calcium-Ion/new-api", err),
|
|
"type": "new_api_panic",
|
|
},
|
|
})
|
|
}))
|
|
// This will cause SSE not to work!!!
|
|
//server.Use(gzip.Gzip(gzip.DefaultCompression))
|
|
server.Use(middleware.RequestId())
|
|
server.Use(middleware.Version())
|
|
server.Use(middleware.I18n())
|
|
middleware.SetUpLogger(server)
|
|
InjectUmamiAnalytics()
|
|
InjectGoogleAnalytics()
|
|
|
|
// 设置路由
|
|
router.SetRouter(server, router.WebAssets{
|
|
BuildFS: buildFS,
|
|
IndexPage: indexPage,
|
|
})
|
|
var port = os.Getenv("PORT")
|
|
if port == "" {
|
|
port = strconv.Itoa(*common.Port)
|
|
}
|
|
|
|
srv := &http.Server{
|
|
Addr: ":" + port,
|
|
Handler: server,
|
|
}
|
|
|
|
go func() {
|
|
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
common.FatalLog(common.LogText("failed to start HTTP server: %s", err.Error()))
|
|
}
|
|
}()
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
common.LogStartupSuccess(startTime, port)
|
|
|
|
quit := make(chan os.Signal, 1)
|
|
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
|
sig := <-quit
|
|
common.SysLog(common.LogText("received signal: %v, shutting down...", sig))
|
|
|
|
// SSE streams may run for minutes; give them time to finish before forced exit
|
|
shutdownTimeout := time.Duration(common.GetEnvOrDefault("SHUTDOWN_TIMEOUT_SECONDS", 120)) * time.Second
|
|
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
|
|
defer cancel()
|
|
if err := srv.Shutdown(ctx); err != nil {
|
|
common.SysError(common.LogText("server forced to shutdown: %v", err))
|
|
}
|
|
// 内存中的看板数据保存入库,避免重启丢失未落库数据 (issue #5679)
|
|
if common.DataExportEnabled {
|
|
model.SaveQuotaDataCache()
|
|
}
|
|
common.SysLog(common.LogText("server exited"))
|
|
}
|
|
|
|
func InjectUmamiAnalytics() {
|
|
analyticsInjectBuilder := &strings.Builder{}
|
|
if os.Getenv("UMAMI_WEBSITE_ID") != "" {
|
|
umamiSiteID := os.Getenv("UMAMI_WEBSITE_ID")
|
|
umamiScriptURL := os.Getenv("UMAMI_SCRIPT_URL")
|
|
if umamiScriptURL == "" {
|
|
umamiScriptURL = "https://analytics.umami.is/script.js"
|
|
}
|
|
analyticsInjectBuilder.WriteString("<script defer src=\"")
|
|
analyticsInjectBuilder.WriteString(umamiScriptURL)
|
|
analyticsInjectBuilder.WriteString("\" data-website-id=\"")
|
|
analyticsInjectBuilder.WriteString(umamiSiteID)
|
|
analyticsInjectBuilder.WriteString("\"></script>")
|
|
}
|
|
analyticsInjectBuilder.WriteString("<!--Umami QuantumNous-->\n")
|
|
analyticsInject := []byte(analyticsInjectBuilder.String())
|
|
placeholder := []byte("<!--umami-->\n")
|
|
indexPage = bytes.ReplaceAll(indexPage, placeholder, analyticsInject)
|
|
}
|
|
|
|
func InjectGoogleAnalytics() {
|
|
analyticsInjectBuilder := &strings.Builder{}
|
|
if os.Getenv("GOOGLE_ANALYTICS_ID") != "" {
|
|
gaID := os.Getenv("GOOGLE_ANALYTICS_ID")
|
|
// Google Analytics 4 (gtag.js)
|
|
analyticsInjectBuilder.WriteString("<script async src=\"https://www.googletagmanager.com/gtag/js?id=")
|
|
analyticsInjectBuilder.WriteString(gaID)
|
|
analyticsInjectBuilder.WriteString("\"></script>")
|
|
analyticsInjectBuilder.WriteString("<script>")
|
|
analyticsInjectBuilder.WriteString("window.dataLayer = window.dataLayer || [];")
|
|
analyticsInjectBuilder.WriteString("function gtag(){dataLayer.push(arguments);}")
|
|
analyticsInjectBuilder.WriteString("gtag('js', new Date());")
|
|
analyticsInjectBuilder.WriteString("gtag('config', '")
|
|
analyticsInjectBuilder.WriteString(gaID)
|
|
analyticsInjectBuilder.WriteString("');")
|
|
analyticsInjectBuilder.WriteString("</script>")
|
|
}
|
|
analyticsInjectBuilder.WriteString("<!--Google Analytics QuantumNous-->\n")
|
|
analyticsInject := []byte(analyticsInjectBuilder.String())
|
|
placeholder := []byte("<!--Google Analytics-->\n")
|
|
indexPage = bytes.ReplaceAll(indexPage, placeholder, analyticsInject)
|
|
}
|
|
|
|
func InitResources() error {
|
|
// Initialize resources here if needed
|
|
// This is a placeholder function for future resource initialization
|
|
err := godotenv.Load(".env")
|
|
if err != nil {
|
|
if common.DebugEnabled {
|
|
common.SysLog(common.LogText("No .env file found, using default environment variables. If needed, please create a .env file and set the relevant variables."))
|
|
}
|
|
}
|
|
|
|
// 加载环境变量
|
|
common.InitEnv()
|
|
|
|
logger.SetupLogger()
|
|
|
|
// Initialize model settings
|
|
ratio_setting.InitRatioSettings()
|
|
|
|
service.InitHttpClient()
|
|
|
|
// Initialize SQL Database
|
|
err = model.InitDB()
|
|
if err != nil {
|
|
common.FatalLog(common.LogText("failed to initialize database: %s", err.Error()))
|
|
return err
|
|
}
|
|
if err = authz.Init(model.DB); err != nil {
|
|
common.FatalLog(common.LogText("failed to initialize authorization: %s", err.Error()))
|
|
return err
|
|
}
|
|
if common.PasswordLoginEncryptionEnabled {
|
|
if err = model.InitPasswordEncryption(); err != nil {
|
|
common.FatalLog(common.LogText("failed to initialize password encryption: %s", err.Error()))
|
|
return err
|
|
}
|
|
}
|
|
|
|
model.CheckSetup()
|
|
|
|
// Initialize options, should after model.InitDB()
|
|
if common.IsMasterNode {
|
|
if err := model.MigrateRetiredFrontendOptions(); err != nil {
|
|
common.SysError(common.LogText("failed to migrate retired frontend options: %s", err.Error()))
|
|
}
|
|
}
|
|
model.InitOptionMap()
|
|
|
|
// 清理旧的磁盘缓存文件
|
|
common.CleanupOldCacheFiles()
|
|
|
|
// Initialize SQL Database
|
|
err = model.InitLogDB()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Initialize Redis
|
|
err = common.InitRedisClient()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
perfmetrics.Init()
|
|
|
|
// 启动系统监控
|
|
common.StartSystemMonitor()
|
|
|
|
// Initialize i18n
|
|
err = i18n.Init()
|
|
if err != nil {
|
|
common.SysError(common.LogText("failed to initialize i18n: %s", err.Error()))
|
|
// Don't return error, i18n is not critical
|
|
} else {
|
|
common.SysLog(common.LogText("i18n initialized with languages: %s", strings.Join(i18n.SupportedLanguages(), ", ")))
|
|
}
|
|
// Register user language loader for lazy loading
|
|
i18n.SetUserLangLoader(model.GetUserLanguage)
|
|
|
|
// Load custom OAuth providers from database
|
|
err = oauth.LoadCustomProviders()
|
|
if err != nil {
|
|
common.SysError(common.LogText("failed to load custom OAuth providers: %s", err.Error()))
|
|
// Don't return error, custom OAuth is not critical
|
|
}
|
|
|
|
service.StartAuthArtifactCleanup()
|
|
|
|
return nil
|
|
}
|