2023-04-22 20:39:27 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"embed"
|
2023-09-29 11:38:27 +08:00
|
|
|
"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"
|
2023-06-22 22:01:03 +08:00
|
|
|
"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"
|
2025-04-07 22:20:47 +08:00
|
|
|
"one-api/setting/operation_setting"
|
2023-04-22 20:39:27 +08:00
|
|
|
"os"
|
|
|
|
|
"strconv"
|
2023-11-23 21:40:17 +08:00
|
|
|
|
2024-09-24 11:39:02 +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
|
|
|
)
|
|
|
|
|
|
2025-06-07 23:05:01 +08:00
|
|
|
// go:embed web/dist
|
2023-04-22 20:39:27 +08:00
|
|
|
var buildFS embed.FS
|
|
|
|
|
|
2025-06-07 23:05:01 +08:00
|
|
|
// go:embed web/dist/index.html
|
2023-04-22 20:39:27 +08:00
|
|
|
var indexPage []byte
|
|
|
|
|
|
|
|
|
|
func main() {
|
2024-09-24 11:39:02 +08:00
|
|
|
err := godotenv.Load(".env")
|
|
|
|
|
if err != nil {
|
2025-04-11 16:23:54 +08:00
|
|
|
common.SysLog("Support for .env file is disabled: " + err.Error())
|
2024-09-24 11:39:02 +08:00
|
|
|
}
|
|
|
|
|
|
2024-12-30 17:24:19 +08:00
|
|
|
common.LoadEnv()
|
|
|
|
|
|
2023-09-17 16:35:30 +08:00
|
|
|
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)
|
|
|
|
|
}
|
2023-08-12 18:10:15 +08:00
|
|
|
if common.DebugEnabled {
|
|
|
|
|
common.SysLog("running in debug mode")
|
|
|
|
|
}
|
2023-04-22 20:39:27 +08:00
|
|
|
// Initialize SQL Database
|
2024-09-24 11:39:02 +08:00
|
|
|
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
|
|
|
}
|
2025-04-08 18:14:36 +08:00
|
|
|
|
|
|
|
|
model.CheckSetup()
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2025-04-10 00:07:34 +08:00
|
|
|
// Initialize model settings
|
2025-04-26 17:15:34 +08:00
|
|
|
operation_setting.InitRatioSettings()
|
2024-08-02 17:23:59 +08:00
|
|
|
// Initialize constants
|
|
|
|
|
constant.InitEnv()
|
2023-04-22 20:39:27 +08:00
|
|
|
// Initialize options
|
|
|
|
|
model.InitOptionMap()
|
2025-04-08 18:14:36 +08:00
|
|
|
|
2025-05-03 21:12:07 +08:00
|
|
|
service.InitTokenEncoders()
|
|
|
|
|
|
2023-06-20 19:09:49 +08:00
|
|
|
if common.RedisEnabled {
|
2023-09-29 11:38:27 +08:00
|
|
|
// 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))
|
2025-05-23 01:26:52 +08:00
|
|
|
|
|
|
|
|
// Add panic recovery and retry for InitChannelCache
|
|
|
|
|
func() {
|
|
|
|
|
defer func() {
|
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
|
common.SysError(fmt.Sprintf("InitChannelCache panic: %v, retrying once", r))
|
|
|
|
|
// Retry once
|
|
|
|
|
_, fixErr := model.FixAbility()
|
|
|
|
|
if fixErr != nil {
|
|
|
|
|
common.SysError(fmt.Sprintf("InitChannelCache failed: %s", fixErr.Error()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
model.InitChannelCache()
|
|
|
|
|
}()
|
|
|
|
|
|
2023-09-29 11:38:27 +08:00
|
|
|
go model.SyncOptions(common.SyncFrequency)
|
|
|
|
|
go model.SyncChannelCache(common.SyncFrequency)
|
2023-05-18 12:48:20 +08:00
|
|
|
}
|
2024-01-07 18:33:41 +08:00
|
|
|
|
|
|
|
|
// 数据看板
|
|
|
|
|
go model.UpdateQuotaData()
|
|
|
|
|
|
2023-06-22 22:01:03 +08:00
|
|
|
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()
|
|
|
|
|
})
|
|
|
|
|
}
|
2023-09-03 14:58:20 +08:00
|
|
|
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" {
|
2025-02-19 18:38:29 +08:00
|
|
|
gopool.Go(func() {
|
2023-11-23 21:40:17 +08:00
|
|
|
log.Println(http.ListenAndServe("0.0.0.0:8005", nil))
|
2025-02-19 18:38:29 +08:00
|
|
|
})
|
2023-11-21 18:11:07 +08:00
|
|
|
go common.Monitor()
|
|
|
|
|
common.SysLog("pprof enabled")
|
|
|
|
|
}
|
|
|
|
|
|
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",
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}))
|
2023-04-25 21:50:57 +08:00
|
|
|
// 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
|
2023-06-26 16:10:59 +08:00
|
|
|
store := cookie.NewStore([]byte(common.SessionSecret))
|
2025-02-11 15:45:24 +08:00
|
|
|
store.Options(sessions.Options{
|
2025-02-11 17:06:51 +08:00
|
|
|
Path: "/",
|
|
|
|
|
MaxAge: 2592000, // 30 days
|
|
|
|
|
HttpOnly: true,
|
|
|
|
|
Secure: false,
|
|
|
|
|
SameSite: http.SameSiteStrictMode,
|
2025-02-11 15:45:24 +08:00
|
|
|
})
|
2023-06-26 16:10:59 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|