main.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package main
  2. import (
  3. "embed"
  4. "fmt"
  5. "github.com/gin-contrib/sessions"
  6. "github.com/gin-contrib/sessions/cookie"
  7. "github.com/gin-gonic/gin"
  8. "log"
  9. "net/http"
  10. "one-api/common"
  11. "one-api/controller"
  12. "one-api/middleware"
  13. "one-api/model"
  14. "one-api/router"
  15. "os"
  16. "strconv"
  17. _ "net/http/pprof"
  18. )
  19. //go:embed web/build
  20. var buildFS embed.FS
  21. //go:embed web/build/index.html
  22. var indexPage []byte
  23. func main() {
  24. common.SetupLogger()
  25. common.SysLog("New API " + common.Version + " started")
  26. if os.Getenv("GIN_MODE") != "debug" {
  27. gin.SetMode(gin.ReleaseMode)
  28. }
  29. if common.DebugEnabled {
  30. common.SysLog("running in debug mode")
  31. }
  32. // Initialize SQL Database
  33. err := model.InitDB()
  34. if err != nil {
  35. common.FatalLog("failed to initialize database: " + err.Error())
  36. }
  37. defer func() {
  38. err := model.CloseDB()
  39. if err != nil {
  40. common.FatalLog("failed to close database: " + err.Error())
  41. }
  42. }()
  43. // Initialize Redis
  44. err = common.InitRedisClient()
  45. if err != nil {
  46. common.FatalLog("failed to initialize Redis: " + err.Error())
  47. }
  48. // Initialize options
  49. model.InitOptionMap()
  50. if common.RedisEnabled {
  51. // for compatibility with old versions
  52. common.MemoryCacheEnabled = true
  53. }
  54. if common.MemoryCacheEnabled {
  55. common.SysLog("memory cache enabled")
  56. common.SysError(fmt.Sprintf("sync frequency: %d seconds", common.SyncFrequency))
  57. model.InitChannelCache()
  58. }
  59. if common.MemoryCacheEnabled {
  60. go model.SyncOptions(common.SyncFrequency)
  61. go model.SyncChannelCache(common.SyncFrequency)
  62. }
  63. if os.Getenv("CHANNEL_UPDATE_FREQUENCY") != "" {
  64. frequency, err := strconv.Atoi(os.Getenv("CHANNEL_UPDATE_FREQUENCY"))
  65. if err != nil {
  66. common.FatalLog("failed to parse CHANNEL_UPDATE_FREQUENCY: " + err.Error())
  67. }
  68. go controller.AutomaticallyUpdateChannels(frequency)
  69. }
  70. if os.Getenv("CHANNEL_TEST_FREQUENCY") != "" {
  71. frequency, err := strconv.Atoi(os.Getenv("CHANNEL_TEST_FREQUENCY"))
  72. if err != nil {
  73. common.FatalLog("failed to parse CHANNEL_TEST_FREQUENCY: " + err.Error())
  74. }
  75. go controller.AutomaticallyTestChannels(frequency)
  76. }
  77. go controller.UpdateMidjourneyTaskBulk()
  78. if os.Getenv("BATCH_UPDATE_ENABLED") == "true" {
  79. common.BatchUpdateEnabled = true
  80. common.SysLog("batch update enabled with interval " + strconv.Itoa(common.BatchUpdateInterval) + "s")
  81. model.InitBatchUpdater()
  82. }
  83. if os.Getenv("ENABLE_PPROF") == "true" {
  84. go func() {
  85. log.Println(http.ListenAndServe("0.0.0.0:8005", nil))
  86. }()
  87. go common.Monitor()
  88. common.SysLog("pprof enabled")
  89. }
  90. controller.InitTokenEncoders()
  91. // Initialize HTTP server
  92. server := gin.New()
  93. server.Use(gin.Recovery())
  94. // This will cause SSE not to work!!!
  95. //server.Use(gzip.Gzip(gzip.DefaultCompression))
  96. server.Use(middleware.RequestId())
  97. middleware.SetUpLogger(server)
  98. // Initialize session store
  99. store := cookie.NewStore([]byte(common.SessionSecret))
  100. server.Use(sessions.Sessions("session", store))
  101. router.SetRouter(server, buildFS, indexPage)
  102. var port = os.Getenv("PORT")
  103. if port == "" {
  104. port = strconv.Itoa(*common.Port)
  105. }
  106. err = server.Run(":" + port)
  107. if err != nil {
  108. common.FatalLog("failed to start HTTP server: " + err.Error())
  109. }
  110. }