ai-gateway/model/main.go

167 lines
4.0 KiB
Go
Raw Normal View History

2023-04-22 12:39:27 +00:00
package model
import (
"fmt"
2024-01-28 11:38:58 +00:00
"github.com/songquanpeng/one-api/common"
"github.com/songquanpeng/one-api/common/config"
"github.com/songquanpeng/one-api/common/env"
2024-01-28 11:38:58 +00:00
"github.com/songquanpeng/one-api/common/helper"
"github.com/songquanpeng/one-api/common/logger"
"github.com/songquanpeng/one-api/common/random"
2023-04-22 12:39:27 +00:00
"gorm.io/driver/mysql"
2023-08-12 11:20:12 +00:00
"gorm.io/driver/postgres"
2023-04-22 12:39:27 +00:00
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"os"
2023-08-12 11:20:12 +00:00
"strings"
"time"
2023-04-22 12:39:27 +00:00
)
var DB *gorm.DB
var LOG_DB *gorm.DB
2023-04-22 12:39:27 +00:00
func CreateRootAccountIfNeed() error {
2023-04-22 12:39:27 +00:00
var user User
2024-01-14 11:21:03 +00:00
//if user.Status != util.UserStatusEnabled {
2023-04-22 12:39:27 +00:00
if err := DB.First(&user).Error; err != nil {
logger.SysLog("no user exists, creating a root user for you: username is root, password is 123456")
2023-04-22 12:39:27 +00:00
hashedPassword, err := common.Password2Hash("123456")
if err != nil {
return err
}
rootUser := User{
Username: "root",
Password: hashedPassword,
2024-04-05 18:03:59 +00:00
Role: RoleRootUser,
Status: UserStatusEnabled,
2023-04-22 12:39:27 +00:00
DisplayName: "Root User",
AccessToken: random.GetUUID(),
Quota: 500000000000000,
2023-04-22 12:39:27 +00:00
}
DB.Create(&rootUser)
if config.InitialRootToken != "" {
logger.SysLog("creating initial root token as requested")
token := Token{
Id: 1,
UserId: rootUser.Id,
Key: config.InitialRootToken,
2024-04-05 18:03:59 +00:00
Status: TokenStatusEnabled,
Name: "Initial Root Token",
CreatedTime: helper.GetTimestamp(),
AccessedTime: helper.GetTimestamp(),
ExpiredTime: -1,
RemainQuota: 500000000000000,
UnlimitedQuota: true,
}
DB.Create(&token)
}
2023-04-22 12:39:27 +00:00
}
return nil
}
func chooseDB(envName string) (*gorm.DB, error) {
if os.Getenv(envName) != "" {
dsn := os.Getenv(envName)
2023-08-12 11:20:12 +00:00
if strings.HasPrefix(dsn, "postgres://") {
// Use PostgreSQL
logger.SysLog("using PostgreSQL as database")
common.UsingPostgreSQL = true
2023-08-12 11:20:12 +00:00
return gorm.Open(postgres.New(postgres.Config{
DSN: dsn,
PreferSimpleProtocol: true, // disables implicit prepared statement usage
}), &gorm.Config{
PrepareStmt: true, // precompile SQL
})
}
2023-04-22 12:39:27 +00:00
// Use MySQL
logger.SysLog("using MySQL as database")
2024-03-10 18:24:58 +00:00
common.UsingMySQL = true
2023-08-12 11:20:12 +00:00
return gorm.Open(mysql.Open(dsn), &gorm.Config{
2023-04-22 12:39:27 +00:00
PrepareStmt: true, // precompile SQL
})
}
2023-08-12 11:20:12 +00:00
// Use SQLite
logger.SysLog("SQL_DSN not set, using SQLite as database")
2023-08-12 11:20:12 +00:00
common.UsingSQLite = true
config := fmt.Sprintf("?_busy_timeout=%d", common.SQLiteBusyTimeout)
return gorm.Open(sqlite.Open(common.SQLitePath+config), &gorm.Config{
2023-08-12 11:20:12 +00:00
PrepareStmt: true, // precompile SQL
})
}
func InitDB(envName string) (db *gorm.DB, err error) {
db, err = chooseDB(envName)
2023-04-22 12:39:27 +00:00
if err == nil {
if config.DebugSQLEnabled {
db = db.Debug()
}
sqlDB, err := db.DB()
if err != nil {
return nil, err
}
sqlDB.SetMaxIdleConns(env.Int("SQL_MAX_IDLE_CONNS", 100))
sqlDB.SetMaxOpenConns(env.Int("SQL_MAX_OPEN_CONNS", 1000))
sqlDB.SetConnMaxLifetime(time.Second * time.Duration(env.Int("SQL_MAX_LIFETIME", 60)))
if !config.IsMasterNode {
return db, err
}
2024-03-10 18:24:58 +00:00
if common.UsingMySQL {
_, _ = sqlDB.Exec("DROP INDEX idx_channels_key ON channels;") // TODO: delete this line when most users have upgraded
}
logger.SysLog("database migration started")
err = db.AutoMigrate(&Channel{})
2023-04-22 12:39:27 +00:00
if err != nil {
return nil, err
2023-04-22 12:39:27 +00:00
}
2023-04-23 04:43:10 +00:00
err = db.AutoMigrate(&Token{})
if err != nil {
return nil, err
2023-04-23 04:43:10 +00:00
}
2023-04-22 12:39:27 +00:00
err = db.AutoMigrate(&User{})
if err != nil {
return nil, err
2023-04-22 12:39:27 +00:00
}
err = db.AutoMigrate(&Option{})
if err != nil {
return nil, err
2023-04-22 12:39:27 +00:00
}
err = db.AutoMigrate(&Redemption{})
if err != nil {
return nil, err
}
err = db.AutoMigrate(&Ability{})
if err != nil {
return nil, err
}
err = db.AutoMigrate(&Log{})
if err != nil {
return nil, err
}
logger.SysLog("database migrated")
return db, err
2023-04-22 12:39:27 +00:00
} else {
logger.FatalLog(err)
2023-04-22 12:39:27 +00:00
}
return db, err
2023-04-22 12:39:27 +00:00
}
func closeDB(db *gorm.DB) error {
sqlDB, err := db.DB()
2023-04-22 12:39:27 +00:00
if err != nil {
return err
}
err = sqlDB.Close()
return err
}
func CloseDB() error {
if LOG_DB != DB {
err := closeDB(LOG_DB)
if err != nil {
return err
}
}
return closeDB(DB)
}