ckst commited on
Commit
faae925
·
verified ·
1 Parent(s): bad2282

Upload main.go

Browse files
Files changed (1) hide show
  1. main.go +153 -0
main.go ADDED
@@ -0,0 +1,153 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package main
2
+
3
+ import (
4
+ "embed"
5
+ "fmt"
6
+ "log"
7
+ "one-api/common"
8
+ "one-api/common/config"
9
+ "one-api/controller"
10
+ "one-api/middleware"
11
+ "one-api/model"
12
+ "one-api/relay/channel/openai"
13
+ "one-api/router"
14
+ "os"
15
+ "strconv"
16
+
17
+ "github.com/gin-contrib/sessions"
18
+ "github.com/gin-contrib/sessions/cookie"
19
+ "github.com/gin-gonic/gin"
20
+ "github.com/joho/godotenv"
21
+
22
+ "net/http"
23
+ _ "net/http/pprof"
24
+ )
25
+
26
+ //go:embed web-admin/build/*
27
+ var adminFS embed.FS
28
+
29
+ //go:embed web-user/build/*
30
+ var userFS embed.FS
31
+
32
+ //go:embed web-admin/build/index.html
33
+ var adminIndexPage []byte
34
+
35
+ //go:embed web-user/build/index.html
36
+ var userIndexPage []byte
37
+
38
+ func main() {
39
+ common.SetupLogger()
40
+ if err := godotenv.Load(); err != nil {
41
+ log.Println("Warning: .env file not found or error loading")
42
+ }
43
+
44
+ common.SysLog("Chat API " + common.Version + " started")
45
+ if os.Getenv("GIN_MODE") != "debug" {
46
+ gin.SetMode(gin.ReleaseMode)
47
+ }
48
+ if config.DebugEnabled {
49
+ common.SysLog("running in debug mode")
50
+ }
51
+ // Initialize SQL Database
52
+ // 初始化 SQL 数据库,并在结束时关闭它。
53
+ if err := model.InitDB(); err != nil { // 使用 := 声明和初始化 err
54
+ common.FatalLog("failed to initialize database: " + err.Error())
55
+ }
56
+ defer func() {
57
+ if err := model.CloseDB(); err != nil { // defer 中又一个新的作用域和局部变量 err
58
+ common.FatalLog("failed to close database: " + err.Error())
59
+ }
60
+ }()
61
+
62
+ // Initialize Redis
63
+ if err := common.InitRedisClient(); err != nil { // 再次使用 := 声明和初始化 err
64
+ common.FatalLog("failed to initialize Redis: " + err.Error())
65
+ }
66
+
67
+ // Initialize options
68
+ model.InitOptionMap()
69
+ if common.RedisEnabled {
70
+ // for compatibility with old versions
71
+ common.MemoryCacheEnabled = true
72
+ }
73
+ if common.MemoryCacheEnabled {
74
+ common.SysLog("memory cache enabled")
75
+ common.SysError(fmt.Sprintf("sync frequency: %d seconds", common.SyncFrequency))
76
+ model.InitChannelCache()
77
+ }
78
+ if common.MemoryCacheEnabled {
79
+ go model.SyncOptions(common.SyncFrequency)
80
+ go model.SyncChannelCache(common.SyncFrequency)
81
+ }
82
+
83
+ // 数据看板
84
+ go model.UpdateQuotaData()
85
+ // 额度有效期
86
+ go model.UpdateUserQuotaData()
87
+ //定时更新GCP AccessTokens
88
+ go model.StartScheduledRefreshAccessTokens()
89
+ if os.Getenv("CHANNEL_UPDATE_FREQUENCY") != "" {
90
+ frequency, err := strconv.Atoi(os.Getenv("CHANNEL_UPDATE_FREQUENCY"))
91
+ if err != nil {
92
+ common.FatalLog("failed to parse CHANNEL_UPDATE_FREQUENCY: " + err.Error())
93
+ }
94
+ go controller.AutomaticallyUpdateChannels(frequency)
95
+ }
96
+ if os.Getenv("CHANNEL_TEST_FREQUENCY") != "" {
97
+ frequency, err := strconv.Atoi(os.Getenv("CHANNEL_TEST_FREQUENCY"))
98
+ if err != nil {
99
+ common.FatalLog("failed to parse CHANNEL_TEST_FREQUENCY: " + err.Error())
100
+ }
101
+
102
+ go controller.AutomaticallyTestChannels(frequency)
103
+ }
104
+ // 启动禁用通道测试器
105
+ go controller.AutomaticallyTestDisabledChannels(60)
106
+ // 启动Midjourney任务更新器
107
+ go controller.UpdateMidjourneyTask()
108
+ // 启动额度提醒检查器
109
+ go controller.StartQuotaAlertChecker()
110
+ //go controller.UpdateMidjourneyTaskBulk()
111
+ if os.Getenv("BATCH_UPDATE_ENABLED") == "true" {
112
+ config.BatchUpdateEnabled = true
113
+ common.SysLog("batch update enabled with interval " + strconv.Itoa(common.BatchUpdateInterval) + "s")
114
+ model.InitBatchUpdater()
115
+ }
116
+
117
+ if os.Getenv("ENABLE_PPROF") == "true" {
118
+ go common.Monitor()
119
+ common.SysLog("pprof enabled")
120
+ }
121
+
122
+ openai.InitTokenEncoders()
123
+
124
+ // Initialize HTTP server
125
+ server := gin.New()
126
+ server.Use(gin.CustomRecovery(func(c *gin.Context, err any) {
127
+ common.SysError(fmt.Sprintf("panic detected: %v", err))
128
+ c.JSON(http.StatusInternalServerError, gin.H{
129
+ "error": gin.H{
130
+ "message": fmt.Sprintf("Panic detected, error: %v. Please submit a issue here: https://github.com/ai365vip/chat-api", err),
131
+ "type": "chat_api_panic",
132
+ },
133
+ })
134
+ }))
135
+ // This will cause SSE not to work!!!
136
+ //server.Use(gzip.Gzip(gzip.DefaultCompression))
137
+ server.Use(middleware.RequestId())
138
+ middleware.SetUpLogger(server)
139
+ // Initialize session store
140
+
141
+ store := cookie.NewStore([]byte(config.SessionSecret))
142
+ server.Use(sessions.Sessions("session", store))
143
+
144
+ router.SetRouter(server, adminFS, userFS, adminIndexPage, userIndexPage)
145
+ var port = os.Getenv("PORT")
146
+ if port == "" {
147
+ port = strconv.Itoa(*common.Port)
148
+ }
149
+ // 启动 HTTP 服务器。
150
+ if err := server.Run(":" + port); err != nil { // 再次使用 := 声明和初始化 err
151
+ common.FatalLog("failed to start HTTP server: " + err.Error())
152
+ }
153
+ }