feat: publish sanitized custom.97 source

This commit is contained in:
cneicy committed 2026-10-27 20:43:39 +08:00
1 parent 8e6ed0317b
commit 86c0276156
1022 files changed
+69903 -19207

No files matched your search

+26 -28
View File
@@ -59,16 +59,16 @@ func main() {
err := InitResources()
if err != nil {
common.FatalLog("failed to initialize resources: " + err.Error())
common.FatalLog(common.LogText("failed to initialize resources: %s", err.Error()))
return
}
common.SysLog("New API " + common.Version + " started")
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("running in debug mode")
common.SysLog(common.LogText("running in debug mode"))
}
kitutil.Debug.Store(common.DebugEnabled)
@@ -76,7 +76,7 @@ func main() {
defer func() {
err := model.CloseDB()
if err != nil {
common.FatalLog("failed to close database: " + err.Error())
common.FatalLog(common.LogText("failed to close database: %s", err.Error()))
}
}()
@@ -85,18 +85,18 @@ func main() {
common.MemoryCacheEnabled = true
}
if common.MemoryCacheEnabled {
common.SysLog("memory cache enabled")
common.SysLog(fmt.Sprintf("sync frequency: %d seconds", common.SyncFrequency))
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(fmt.Sprintf("InitChannelCache panic: %v, retrying once", r))
common.SysLog(common.LogText("InitChannelCache panic: %v, retrying once", r))
// Retry once
_, _, fixErr := model.FixAbility()
if fixErr != nil {
common.FatalLog(fmt.Sprintf("InitChannelCache failed: %s", fixErr.Error()))
common.FatalLog(common.LogText("InitChannelCache failed: %s", fixErr.Error()))
}
}
}()
@@ -124,7 +124,7 @@ func main() {
if os.Getenv("CHANNEL_UPDATE_FREQUENCY") != "" {
frequency, err := strconv.Atoi(os.Getenv("CHANNEL_UPDATE_FREQUENCY"))
if err != nil {
common.FatalLog("failed to parse CHANNEL_UPDATE_FREQUENCY: " + err.Error())
common.FatalLog(common.LogText("failed to parse CHANNEL_UPDATE_FREQUENCY: %s", err.Error()))
}
go controller.AutomaticallyUpdateChannels(frequency)
}
@@ -161,7 +161,7 @@ func main() {
if os.Getenv("BATCH_UPDATE_ENABLED") == "true" {
common.BatchUpdateEnabled = true
common.SysLog("batch update enabled with interval " + strconv.Itoa(common.BatchUpdateInterval) + "s")
common.SysLog(common.LogText("batch update enabled with interval %ss", strconv.Itoa(common.BatchUpdateInterval)))
model.InitBatchUpdater()
}
@@ -170,22 +170,22 @@ func main() {
log.Println(http.ListenAndServe("0.0.0.0:8005", nil))
})
go common.Monitor()
common.SysLog("pprof enabled")
common.SysLog(common.LogText("pprof enabled"))
}
err = common.StartPyroScope()
if err != nil {
common.SysError(fmt.Sprintf("start pyroscope error : %v", err))
common.SysError(common.LogText("start pyroscope error : %v", err))
}
// Initialize HTTP server
server := gin.New()
if err := middleware.ConfigureTrustedProxies(server); err != nil {
common.FatalLog("failed to configure trusted proxies: " + err.Error())
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(fmt.Sprintf("panic detected: %v", err))
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),
@@ -219,7 +219,7 @@ func main() {
go func() {
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
common.FatalLog("failed to start HTTP server: " + err.Error())
common.FatalLog(common.LogText("failed to start HTTP server: %s", err.Error()))
}
}()
@@ -230,20 +230,20 @@ func main() {
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
sig := <-quit
common.SysLog(fmt.Sprintf("received signal: %v, shutting down...", sig))
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(fmt.Sprintf("server forced to shutdown: %v", err))
common.SysError(common.LogText("server forced to shutdown: %v", err))
}
// 内存中的看板数据保存入库,避免重启丢失未落库数据 (issue #5679)
if common.DataExportEnabled {
model.SaveQuotaDataCache()
}
common.SysLog("server exited")
common.SysLog(common.LogText("server exited"))
}
func InjectUmamiAnalytics() {
@@ -295,7 +295,7 @@ func InitResources() error {
err := godotenv.Load(".env")
if err != nil {
if common.DebugEnabled {
common.SysLog("No .env file found, using default environment variables. If needed, please create a .env file and set the relevant variables.")
common.SysLog(common.LogText("No .env file found, using default environment variables. If needed, please create a .env file and set the relevant variables."))
}
}
@@ -309,21 +309,19 @@ func InitResources() error {
service.InitHttpClient()
service.InitTokenEncoders()
// Initialize SQL Database
err = model.InitDB()
if err != nil {
common.FatalLog("failed to initialize database: " + err.Error())
common.FatalLog(common.LogText("failed to initialize database: %s", err.Error()))
return err
}
if err = authz.Init(model.DB); err != nil {
common.FatalLog("failed to initialize authorization: " + err.Error())
common.FatalLog(common.LogText("failed to initialize authorization: %s", err.Error()))
return err
}
if common.PasswordLoginEncryptionEnabled {
if err = model.InitPasswordEncryption(); err != nil {
common.FatalLog("failed to initialize password encryption: " + err.Error())
common.FatalLog(common.LogText("failed to initialize password encryption: %s", err.Error()))
return err
}
}
@@ -333,7 +331,7 @@ func InitResources() error {
// Initialize options, should after model.InitDB()
if common.IsMasterNode {
if err := model.MigrateRetiredFrontendOptions(); err != nil {
common.SysError("failed to migrate retired frontend options: " + err.Error())
common.SysError(common.LogText("failed to migrate retired frontend options: %s", err.Error()))
}
}
model.InitOptionMap()
@@ -361,10 +359,10 @@ func InitResources() error {
// Initialize i18n
err = i18n.Init()
if err != nil {
common.SysError("failed to initialize i18n: " + err.Error())
common.SysError(common.LogText("failed to initialize i18n: %s", err.Error()))
// Don't return error, i18n is not critical
} else {
common.SysLog("i18n initialized with languages: " + strings.Join(i18n.SupportedLanguages(), ", "))
common.SysLog(common.LogText("i18n initialized with languages: %s", strings.Join(i18n.SupportedLanguages(), ", ")))
}
// Register user language loader for lazy loading
i18n.SetUserLangLoader(model.GetUserLanguage)
@@ -372,7 +370,7 @@ func InitResources() error {
// Load custom OAuth providers from database
err = oauth.LoadCustomProviders()
if err != nil {
common.SysError("failed to load custom OAuth providers: " + err.Error())
common.SysError(common.LogText("failed to load custom OAuth providers: %s", err.Error()))
// Don't return error, custom OAuth is not critical
}