main.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. package main
  2. import (
  3. "bytes"
  4. "embed"
  5. "fmt"
  6. "log"
  7. "net/http"
  8. "os"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "github.com/QuantumNous/new-api/common"
  13. "github.com/QuantumNous/new-api/constant"
  14. "github.com/QuantumNous/new-api/controller"
  15. "github.com/QuantumNous/new-api/i18n"
  16. "github.com/QuantumNous/new-api/logger"
  17. "github.com/QuantumNous/new-api/middleware"
  18. "github.com/QuantumNous/new-api/model"
  19. "github.com/QuantumNous/new-api/oauth"
  20. "github.com/QuantumNous/new-api/router"
  21. "github.com/QuantumNous/new-api/service"
  22. _ "github.com/QuantumNous/new-api/setting/performance_setting"
  23. "github.com/QuantumNous/new-api/setting/ratio_setting"
  24. "github.com/bytedance/gopkg/util/gopool"
  25. "github.com/gin-contrib/sessions"
  26. "github.com/gin-contrib/sessions/cookie"
  27. "github.com/gin-gonic/gin"
  28. "github.com/joho/godotenv"
  29. _ "net/http/pprof"
  30. )
  31. //go:embed web/dist
  32. var buildFS embed.FS
  33. //go:embed web/dist/index.html
  34. var indexPage []byte
  35. func main() {
  36. startTime := time.Now()
  37. err := InitResources()
  38. if err != nil {
  39. common.FatalLog("failed to initialize resources: " + err.Error())
  40. return
  41. }
  42. common.SysLog("New API " + common.Version + " started")
  43. if os.Getenv("GIN_MODE") != "debug" {
  44. gin.SetMode(gin.ReleaseMode)
  45. }
  46. if common.DebugEnabled {
  47. common.SysLog("running in debug mode")
  48. }
  49. defer func() {
  50. err := model.CloseDB()
  51. if err != nil {
  52. common.FatalLog("failed to close database: " + err.Error())
  53. }
  54. }()
  55. if common.RedisEnabled {
  56. // for compatibility with old versions
  57. common.MemoryCacheEnabled = true
  58. }
  59. if common.MemoryCacheEnabled {
  60. common.SysLog("memory cache enabled")
  61. common.SysLog(fmt.Sprintf("sync frequency: %d seconds", common.SyncFrequency))
  62. // Add panic recovery and retry for InitChannelCache
  63. func() {
  64. defer func() {
  65. if r := recover(); r != nil {
  66. common.SysLog(fmt.Sprintf("InitChannelCache panic: %v, retrying once", r))
  67. // Retry once
  68. _, _, fixErr := model.FixAbility()
  69. if fixErr != nil {
  70. common.FatalLog(fmt.Sprintf("InitChannelCache failed: %s", fixErr.Error()))
  71. }
  72. }
  73. }()
  74. model.InitChannelCache()
  75. }()
  76. go model.SyncChannelCache(common.SyncFrequency)
  77. }
  78. // 热更新配置
  79. go model.SyncOptions(common.SyncFrequency)
  80. // 数据看板
  81. go model.UpdateQuotaData()
  82. if os.Getenv("CHANNEL_UPDATE_FREQUENCY") != "" {
  83. frequency, err := strconv.Atoi(os.Getenv("CHANNEL_UPDATE_FREQUENCY"))
  84. if err != nil {
  85. common.FatalLog("failed to parse CHANNEL_UPDATE_FREQUENCY: " + err.Error())
  86. }
  87. go controller.AutomaticallyUpdateChannels(frequency)
  88. }
  89. go controller.AutomaticallyTestChannels()
  90. // Codex credential auto-refresh check every 10 minutes, refresh when expires within 1 day
  91. service.StartCodexCredentialAutoRefreshTask()
  92. // Subscription quota reset task (daily/weekly/monthly/custom)
  93. service.StartSubscriptionQuotaResetTask()
  94. if common.IsMasterNode && constant.UpdateTask {
  95. gopool.Go(func() {
  96. controller.UpdateMidjourneyTaskBulk()
  97. })
  98. gopool.Go(func() {
  99. controller.UpdateTaskBulk()
  100. })
  101. }
  102. if os.Getenv("BATCH_UPDATE_ENABLED") == "true" {
  103. common.BatchUpdateEnabled = true
  104. common.SysLog("batch update enabled with interval " + strconv.Itoa(common.BatchUpdateInterval) + "s")
  105. model.InitBatchUpdater()
  106. }
  107. if os.Getenv("ENABLE_PPROF") == "true" {
  108. gopool.Go(func() {
  109. log.Println(http.ListenAndServe("0.0.0.0:8005", nil))
  110. })
  111. go common.Monitor()
  112. common.SysLog("pprof enabled")
  113. }
  114. err = common.StartPyroScope()
  115. if err != nil {
  116. common.SysError(fmt.Sprintf("start pyroscope error : %v", err))
  117. }
  118. // Initialize HTTP server
  119. server := gin.New()
  120. server.Use(gin.CustomRecovery(func(c *gin.Context, err any) {
  121. common.SysLog(fmt.Sprintf("panic detected: %v", err))
  122. c.JSON(http.StatusInternalServerError, gin.H{
  123. "error": gin.H{
  124. "message": fmt.Sprintf("Panic detected, error: %v. Please submit a issue here: https://github.com/Calcium-Ion/new-api", err),
  125. "type": "new_api_panic",
  126. },
  127. })
  128. }))
  129. // This will cause SSE not to work!!!
  130. //server.Use(gzip.Gzip(gzip.DefaultCompression))
  131. server.Use(middleware.RequestId())
  132. server.Use(middleware.PoweredBy())
  133. server.Use(middleware.I18n())
  134. middleware.SetUpLogger(server)
  135. // Initialize session store
  136. store := cookie.NewStore([]byte(common.SessionSecret))
  137. store.Options(sessions.Options{
  138. Path: "/",
  139. MaxAge: 2592000, // 30 days
  140. HttpOnly: true,
  141. Secure: false,
  142. SameSite: http.SameSiteStrictMode,
  143. })
  144. server.Use(sessions.Sessions("session", store))
  145. InjectUmamiAnalytics()
  146. InjectGoogleAnalytics()
  147. // 设置路由
  148. router.SetRouter(server, buildFS, indexPage)
  149. var port = os.Getenv("PORT")
  150. if port == "" {
  151. port = strconv.Itoa(*common.Port)
  152. }
  153. // Log startup success message
  154. common.LogStartupSuccess(startTime, port)
  155. err = server.Run(":" + port)
  156. if err != nil {
  157. common.FatalLog("failed to start HTTP server: " + err.Error())
  158. }
  159. }
  160. func InjectUmamiAnalytics() {
  161. analyticsInjectBuilder := &strings.Builder{}
  162. if os.Getenv("UMAMI_WEBSITE_ID") != "" {
  163. umamiSiteID := os.Getenv("UMAMI_WEBSITE_ID")
  164. umamiScriptURL := os.Getenv("UMAMI_SCRIPT_URL")
  165. if umamiScriptURL == "" {
  166. umamiScriptURL = "https://analytics.umami.is/script.js"
  167. }
  168. analyticsInjectBuilder.WriteString("<script defer src=\"")
  169. analyticsInjectBuilder.WriteString(umamiScriptURL)
  170. analyticsInjectBuilder.WriteString("\" data-website-id=\"")
  171. analyticsInjectBuilder.WriteString(umamiSiteID)
  172. analyticsInjectBuilder.WriteString("\"></script>")
  173. }
  174. analyticsInjectBuilder.WriteString("<!--Umami QuantumNous-->\n")
  175. analyticsInject := analyticsInjectBuilder.String()
  176. indexPage = bytes.ReplaceAll(indexPage, []byte("<!--umami-->\n"), []byte(analyticsInject))
  177. }
  178. func InjectGoogleAnalytics() {
  179. analyticsInjectBuilder := &strings.Builder{}
  180. if os.Getenv("GOOGLE_ANALYTICS_ID") != "" {
  181. gaID := os.Getenv("GOOGLE_ANALYTICS_ID")
  182. // Google Analytics 4 (gtag.js)
  183. analyticsInjectBuilder.WriteString("<script async src=\"https://www.googletagmanager.com/gtag/js?id=")
  184. analyticsInjectBuilder.WriteString(gaID)
  185. analyticsInjectBuilder.WriteString("\"></script>")
  186. analyticsInjectBuilder.WriteString("<script>")
  187. analyticsInjectBuilder.WriteString("window.dataLayer = window.dataLayer || [];")
  188. analyticsInjectBuilder.WriteString("function gtag(){dataLayer.push(arguments);}")
  189. analyticsInjectBuilder.WriteString("gtag('js', new Date());")
  190. analyticsInjectBuilder.WriteString("gtag('config', '")
  191. analyticsInjectBuilder.WriteString(gaID)
  192. analyticsInjectBuilder.WriteString("');")
  193. analyticsInjectBuilder.WriteString("</script>")
  194. }
  195. analyticsInjectBuilder.WriteString("<!--Google Analytics QuantumNous-->\n")
  196. analyticsInject := analyticsInjectBuilder.String()
  197. indexPage = bytes.ReplaceAll(indexPage, []byte("<!--Google Analytics-->\n"), []byte(analyticsInject))
  198. }
  199. func InitResources() error {
  200. // Initialize resources here if needed
  201. // This is a placeholder function for future resource initialization
  202. err := godotenv.Load(".env")
  203. if err != nil {
  204. if common.DebugEnabled {
  205. common.SysLog("No .env file found, using default environment variables. If needed, please create a .env file and set the relevant variables.")
  206. }
  207. }
  208. // 加载环境变量
  209. common.InitEnv()
  210. logger.SetupLogger()
  211. // Initialize model settings
  212. ratio_setting.InitRatioSettings()
  213. service.InitHttpClient()
  214. service.InitTokenEncoders()
  215. // Initialize SQL Database
  216. err = model.InitDB()
  217. if err != nil {
  218. common.FatalLog("failed to initialize database: " + err.Error())
  219. return err
  220. }
  221. model.CheckSetup()
  222. // Initialize options, should after model.InitDB()
  223. model.InitOptionMap()
  224. // 清理旧的磁盘缓存文件
  225. common.CleanupOldCacheFiles()
  226. // 初始化模型
  227. model.GetPricing()
  228. // Initialize SQL Database
  229. err = model.InitLogDB()
  230. if err != nil {
  231. return err
  232. }
  233. // Initialize Redis
  234. err = common.InitRedisClient()
  235. if err != nil {
  236. return err
  237. }
  238. // 启动系统监控
  239. common.StartSystemMonitor()
  240. // Initialize i18n
  241. err = i18n.Init()
  242. if err != nil {
  243. common.SysError("failed to initialize i18n: " + err.Error())
  244. // Don't return error, i18n is not critical
  245. } else {
  246. common.SysLog("i18n initialized with languages: " + strings.Join(i18n.SupportedLanguages(), ", "))
  247. }
  248. // Register user language loader for lazy loading
  249. i18n.SetUserLangLoader(model.GetUserLanguage)
  250. // Load custom OAuth providers from database
  251. err = oauth.LoadCustomProviders()
  252. if err != nil {
  253. common.SysError("failed to load custom OAuth providers: " + err.Error())
  254. // Don't return error, custom OAuth is not critical
  255. }
  256. return nil
  257. }