main.go 2.8 KB

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