-
-
Notifications
You must be signed in to change notification settings - Fork 257
/
main.go
145 lines (121 loc) · 3.14 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"embed"
"fmt"
"one-api/cli"
"one-api/common"
"one-api/common/cache"
"one-api/common/config"
"one-api/common/logger"
"one-api/common/notify"
"one-api/common/oidc"
"one-api/common/redis"
"one-api/common/requester"
"one-api/common/storage"
"one-api/common/telegram"
"one-api/controller"
"one-api/cron"
"one-api/middleware"
"one-api/model"
"one-api/relay/task"
"one-api/router"
"time"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
)
//go:embed web/build
var buildFS embed.FS
//go:embed web/build/index.html
var indexPage []byte
func main() {
cli.InitCli()
config.InitConf()
if viper.GetString("log_level") == "debug" {
config.Debug = true
}
logger.SetupLogger()
logger.SysLog("One Hub " + config.Version + " started")
// Initialize user token
err := common.InitUserToken()
if err != nil {
logger.FatalLog("failed to initialize user token: " + err.Error())
}
// Initialize SQL Database
model.SetupDB()
defer model.CloseDB()
// Initialize Redis
redis.InitRedisClient()
cache.InitCacheManager()
// Initialize options
model.InitOptionMap()
// Initialize oidc
oidc.InitOIDCConfig()
model.NewPricing()
model.HandleOldTokenMaxId()
initMemoryCache()
initSync()
common.InitTokenEncoders()
requester.InitHttpClient()
// Initialize Telegram bot
telegram.InitTelegramBot()
controller.InitMidjourneyTask()
task.InitTask()
notify.InitNotifier()
cron.InitCron()
storage.InitStorage()
initHttpServer()
}
func initMemoryCache() {
if viper.GetBool("memory_cache_enabled") {
config.MemoryCacheEnabled = true
}
if !config.MemoryCacheEnabled {
return
}
syncFrequency := viper.GetInt("sync_frequency")
model.TokenCacheSeconds = syncFrequency
logger.SysLog("memory cache enabled")
logger.SysError(fmt.Sprintf("sync frequency: %d seconds", syncFrequency))
go model.SyncOptions(syncFrequency)
go SyncChannelCache(syncFrequency)
}
func initSync() {
// go controller.AutomaticallyUpdateChannels(viper.GetInt("channel.update_frequency"))
go controller.AutomaticallyTestChannels(viper.GetInt("channel.test_frequency"))
}
func initHttpServer() {
if viper.GetString("gin_mode") != "debug" {
gin.SetMode(gin.ReleaseMode)
}
server := gin.New()
server.Use(gin.Recovery())
server.Use(middleware.RequestId())
middleware.SetUpLogger(server)
trustedHeader := viper.GetString("trusted_header")
if trustedHeader != "" {
server.TrustedPlatform = trustedHeader
}
store := cookie.NewStore([]byte(config.SessionSecret))
server.Use(sessions.Sessions("session", store))
router.SetRouter(server, buildFS, indexPage)
port := viper.GetString("port")
err := server.Run(":" + port)
if err != nil {
logger.FatalLog("failed to start HTTP server: " + err.Error())
}
}
func SyncChannelCache(frequency int) {
// 只有 从 服务器端获取数据的时候才会用到
if config.IsMasterNode {
logger.SysLog("master node does't synchronize the channel")
return
}
for {
time.Sleep(time.Duration(frequency) * time.Second)
logger.SysLog("syncing channels from database")
model.ChannelGroup.Load()
model.PricingInstance.Init()
}
}