Files
new-api/main.go

167 lines
4.1 KiB
Go
Raw Normal View History

2023-04-22 20:39:27 +08:00
package main
import (
"embed"
"fmt"
2023-11-23 21:40:17 +08:00
"log"
"net/http"
2023-04-22 21:14:09 +08:00
"one-api/common"
2024-07-18 17:26:21 +08:00
"one-api/constant"
"one-api/controller"
2023-04-22 21:14:09 +08:00
"one-api/middleware"
"one-api/model"
"one-api/router"
2024-02-29 16:21:25 +08:00
"one-api/service"
2023-04-22 20:39:27 +08:00
"os"
"strconv"
2023-11-23 21:40:17 +08:00
"github.com/bytedance/gopkg/util/gopool"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
2023-11-23 21:40:17 +08:00
_ "net/http/pprof"
2023-04-22 20:39:27 +08:00
)
2024-06-13 18:28:47 +08:00
//go:embed web/dist
2023-04-22 20:39:27 +08:00
var buildFS embed.FS
2024-06-13 18:28:47 +08:00
//go:embed web/dist/index.html
2023-04-22 20:39:27 +08:00
var indexPage []byte
func main() {
err := godotenv.Load(".env")
if err != nil {
common.SysLog("Support for .env file is disabled")
}
common.LoadEnv()
common.SetupLogger()
2023-12-21 23:11:52 +08:00
common.SysLog("New API " + common.Version + " started")
2023-04-22 20:39:27 +08:00
if os.Getenv("GIN_MODE") != "debug" {
gin.SetMode(gin.ReleaseMode)
}
if common.DebugEnabled {
common.SysLog("running in debug mode")
}
2023-04-22 20:39:27 +08:00
// Initialize SQL Database
err = model.InitDB()
2023-04-22 20:39:27 +08:00
if err != nil {
2023-06-22 10:59:01 +08:00
common.FatalLog("failed to initialize database: " + err.Error())
2024-08-13 10:28:35 +08:00
}
// Initialize SQL Database
err = model.InitLogDB()
if err != nil {
common.FatalLog("failed to initialize database: " + err.Error())
2023-04-22 20:39:27 +08:00
}
defer func() {
err := model.CloseDB()
if err != nil {
2023-06-22 10:59:01 +08:00
common.FatalLog("failed to close database: " + err.Error())
2023-04-22 20:39:27 +08:00
}
}()
// Initialize Redis
err = common.InitRedisClient()
if err != nil {
2023-06-22 10:59:01 +08:00
common.FatalLog("failed to initialize Redis: " + err.Error())
2023-04-22 20:39:27 +08:00
}
// Initialize constants
constant.InitEnv()
2023-04-22 20:39:27 +08:00
// Initialize options
model.InitOptionMap()
if common.RedisEnabled {
// for compatibility with old versions
common.MemoryCacheEnabled = true
}
if common.MemoryCacheEnabled {
common.SysLog("memory cache enabled")
common.SysError(fmt.Sprintf("sync frequency: %d seconds", common.SyncFrequency))
model.InitChannelCache()
}
if common.MemoryCacheEnabled {
go model.SyncOptions(common.SyncFrequency)
go model.SyncChannelCache(common.SyncFrequency)
}
2024-01-07 18:33:41 +08:00
// 数据看板
go model.UpdateQuotaData()
if os.Getenv("CHANNEL_UPDATE_FREQUENCY") != "" {
frequency, err := strconv.Atoi(os.Getenv("CHANNEL_UPDATE_FREQUENCY"))
if err != nil {
common.FatalLog("failed to parse CHANNEL_UPDATE_FREQUENCY: " + err.Error())
}
go controller.AutomaticallyUpdateChannels(frequency)
}
if os.Getenv("CHANNEL_TEST_FREQUENCY") != "" {
frequency, err := strconv.Atoi(os.Getenv("CHANNEL_TEST_FREQUENCY"))
if err != nil {
common.FatalLog("failed to parse CHANNEL_TEST_FREQUENCY: " + err.Error())
}
go controller.AutomaticallyTestChannels(frequency)
}
2024-07-18 17:26:21 +08:00
if common.IsMasterNode && constant.UpdateTask {
2024-07-19 01:07:37 +08:00
gopool.Go(func() {
2024-06-26 17:23:03 +08:00
controller.UpdateMidjourneyTaskBulk()
})
2024-07-19 01:07:37 +08:00
gopool.Go(func() {
2024-06-26 17:23:03 +08:00
controller.UpdateTaskBulk()
})
}
if os.Getenv("BATCH_UPDATE_ENABLED") == "true" {
common.BatchUpdateEnabled = true
common.SysLog("batch update enabled with interval " + strconv.Itoa(common.BatchUpdateInterval) + "s")
model.InitBatchUpdater()
}
2023-11-21 18:11:07 +08:00
if os.Getenv("ENABLE_PPROF") == "true" {
2023-11-23 21:40:17 +08:00
go func() {
log.Println(http.ListenAndServe("0.0.0.0:8005", nil))
}()
2023-11-21 18:11:07 +08:00
go common.Monitor()
common.SysLog("pprof enabled")
}
2024-02-29 16:21:25 +08:00
service.InitTokenEncoders()
2023-04-22 20:39:27 +08:00
// Initialize HTTP server
2023-09-17 15:39:46 +08:00
server := gin.New()
2024-01-07 22:25:03 +08:00
server.Use(gin.CustomRecovery(func(c *gin.Context, err any) {
common.SysError(fmt.Sprintf("panic detected: %v", err))
c.JSON(http.StatusInternalServerError, gin.H{
"error": gin.H{
"message": fmt.Sprintf("Panic detected, error: %v. Please submit a issue here: https://github.com/Calcium-Ion/new-api", err),
"type": "new_api_panic",
},
})
}))
// This will cause SSE not to work!!!
//server.Use(gzip.Gzip(gzip.DefaultCompression))
2023-09-17 15:39:46 +08:00
server.Use(middleware.RequestId())
middleware.SetUpLogger(server)
2023-04-22 20:39:27 +08:00
// Initialize session store
store := cookie.NewStore([]byte(common.SessionSecret))
store.Options(sessions.Options{
Path: "/",
MaxAge: 2592000, // 30 days
HttpOnly: true,
Secure: false,
SameSite: http.SameSiteStrictMode,
})
server.Use(sessions.Sessions("session", store))
2023-04-22 20:39:27 +08:00
router.SetRouter(server, buildFS, indexPage)
var port = os.Getenv("PORT")
if port == "" {
port = strconv.Itoa(*common.Port)
}
err = server.Run(":" + port)
if err != nil {
2023-06-22 10:59:01 +08:00
common.FatalLog("failed to start HTTP server: " + err.Error())
2023-04-22 20:39:27 +08:00
}
}