-
-
Notifications
You must be signed in to change notification settings - Fork 151
/
main.go
120 lines (101 loc) · 2.63 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
package main
import (
"embed"
"fmt"
"one-api/cli"
"one-api/common"
"one-api/common/config"
"one-api/common/logger"
"one-api/common/notify"
"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/relay_util"
"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()
logger.SetupLogger()
logger.SysLog("One API " + config.Version + " started")
// Initialize SQL Database
model.SetupDB()
defer model.CloseDB()
// Initialize Redis
common.InitRedisClient()
// Initialize options
model.InitOptionMap()
relay_util.NewPricing()
initMemoryCache()
initSync()
common.InitTokenEncoders()
requester.InitHttpClient()
// Initialize Telegram bot
telegram.InitTelegramBot()
controller.InitMidjourneyTask()
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)
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()
relay_util.PricingInstance.Init()
}
}