main.go 2.7 KB

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