forked from MartialBE/one-hub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
93 lines (76 loc) · 1.99 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
package main
import (
"embed"
"fmt"
"one-api/common"
"one-api/common/config"
"one-api/common/requester"
"one-api/common/telegram"
"one-api/controller"
"one-api/middleware"
"one-api/model"
"one-api/relay/util"
"one-api/router"
"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() {
config.InitConf()
common.SetupLogger()
common.SysLog("One API " + common.Version + " started")
// Initialize SQL Database
model.SetupDB()
defer model.CloseDB()
// Initialize Redis
common.InitRedisClient()
// Initialize options
model.InitOptionMap()
util.NewPricing()
initMemoryCache()
initSync()
common.InitTokenEncoders()
requester.InitHttpClient()
// Initialize Telegram bot
telegram.InitTelegramBot()
initHttpServer()
}
func initMemoryCache() {
if viper.GetBool("MEMORY_CACHE_ENABLED") {
common.MemoryCacheEnabled = true
}
if !common.MemoryCacheEnabled {
return
}
syncFrequency := viper.GetInt("SYNC_FREQUENCY")
model.TokenCacheSeconds = syncFrequency
common.SysLog("memory cache enabled")
common.SysError(fmt.Sprintf("sync frequency: %d seconds", syncFrequency))
go model.SyncOptions(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(common.SessionSecret))
server.Use(sessions.Sessions("session", store))
router.SetRouter(server, buildFS, indexPage)
port := viper.GetString("PORT")
err := server.Run(":" + port)
if err != nil {
common.FatalLog("failed to start HTTP server: " + err.Error())
}
}