diff --git a/docker-compose.yml b/docker-compose.yml index e657390a..a9d00967 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -30,11 +30,14 @@ services: # - SQL_DSN=root:123456@tcp(mysql:3306)/new-api # Point to the mysql service, uncomment if using MySQL - REDIS_CONN_STRING=redis://redis - TZ=Asia/Shanghai - - ERROR_LOG_ENABLED=true # 是否启用错误日志记录 - - BATCH_UPDATE_ENABLED=true # 是否启用批量更新 batch update enabled -# - STREAMING_TIMEOUT=300 # 流模式无响应超时时间,单位秒,默认120秒,如果出现空补全可以尝试改为更大值 Streaming timeout in seconds, default is 120s. Increase if experiencing empty completions -# - SESSION_SECRET=random_string # 多机部署时设置,必须修改这个随机字符串!! multi-node deployment, set this to a random string!!!!!!! + - ERROR_LOG_ENABLED=true # 是否启用错误日志记录 (Whether to enable error log recording) + - BATCH_UPDATE_ENABLED=true # 是否启用批量更新 (Whether to enable batch update) +# - STREAMING_TIMEOUT=300 # 流模式无响应超时时间,单位秒,默认120秒,如果出现空补全可以尝试改为更大值 (Streaming timeout in seconds, default is 120s. Increase if experiencing empty completions) +# - SESSION_SECRET=random_string # 多机部署时设置,必须修改这个随机字符串!! (multi-node deployment, set this to a random string!!!!!!!) # - SYNC_FREQUENCY=60 # Uncomment if regular database syncing is needed +# - GOOGLE_ANALYTICS_ID=G-XXXXXXXXXX # Google Analytics 的测量 ID (Google Analytics Measurement ID) +# - UMAMI_WEBSITE_ID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx # Umami 网站 ID (Umami Website ID) +# - UMAMI_SCRIPT_URL=https://analytics.umami.is/script.js # Umami 脚本 URL,默认为官方地址 (Umami Script URL, defaults to official URL) depends_on: - redis diff --git a/main.go b/main.go index 47f71a20..481d0a60 100644 --- a/main.go +++ b/main.go @@ -150,6 +150,26 @@ func main() { }) server.Use(sessions.Sessions("session", store)) + InjectUmamiAnalytics() + InjectGoogleAnalytics() + + // 设置路由 + router.SetRouter(server, buildFS, indexPage) + var port = os.Getenv("PORT") + if port == "" { + port = strconv.Itoa(*common.Port) + } + + // Log startup success message + common.LogStartupSuccess(startTime, port) + + err = server.Run(":" + port) + if err != nil { + common.FatalLog("failed to start HTTP server: " + err.Error()) + } +} + +func InjectUmamiAnalytics() { analyticsInjectBuilder := &strings.Builder{} if os.Getenv("UMAMI_WEBSITE_ID") != "" { umamiSiteID := os.Getenv("UMAMI_WEBSITE_ID") @@ -164,21 +184,28 @@ func main() { analyticsInjectBuilder.WriteString("\">") } analyticsInject := analyticsInjectBuilder.String() - indexPage = bytes.ReplaceAll(indexPage, []byte("\n"), []byte(analyticsInject)) + indexPage = bytes.ReplaceAll(indexPage, []byte("\n"), []byte(analyticsInject)) +} - router.SetRouter(server, buildFS, indexPage) - var port = os.Getenv("PORT") - if port == "" { - port = strconv.Itoa(*common.Port) - } - - // Log startup success message - common.LogStartupSuccess(startTime, port) - - err = server.Run(":" + port) - if err != nil { - common.FatalLog("failed to start HTTP server: " + err.Error()) +func InjectGoogleAnalytics() { + analyticsInjectBuilder := &strings.Builder{} + if os.Getenv("GOOGLE_ANALYTICS_ID") != "" { + gaID := os.Getenv("GOOGLE_ANALYTICS_ID") + // Google Analytics 4 (gtag.js) + analyticsInjectBuilder.WriteString("") + analyticsInjectBuilder.WriteString("") } + analyticsInject := analyticsInjectBuilder.String() + indexPage = bytes.ReplaceAll(indexPage, []byte("\n"), []byte(analyticsInject)) } func InitResources() error { diff --git a/web/index.html b/web/index.html index a9df87f5..b1bcf171 100644 --- a/web/index.html +++ b/web/index.html @@ -10,7 +10,8 @@ content="OpenAI 接口聚合管理,支持多种渠道包括 Azure,可用于二次分发管理 key,仅单可执行文件,已打包好 Docker 镜像,一键部署,开箱即用" /> New API - + +