mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2024-12-21 21:27:49 +08:00
60 lines
1.3 KiB
Go
60 lines
1.3 KiB
Go
package db
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"path"
|
|
"time"
|
|
|
|
"github.com/1Panel-dev/1Panel/core/global"
|
|
"github.com/glebarez/sqlite"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/logger"
|
|
)
|
|
|
|
func Init() {
|
|
dbPath := path.Join(global.CONF.System.BaseDir, "1panel/db")
|
|
if _, err := os.Stat(dbPath); err != nil {
|
|
if err := os.MkdirAll(dbPath, os.ModePerm); err != nil {
|
|
panic(fmt.Errorf("init db dir failed, err: %v", err))
|
|
}
|
|
}
|
|
fullPath := path.Join(dbPath, global.CONF.System.DbCoreFile)
|
|
if _, err := os.Stat(fullPath); err != nil {
|
|
f, err := os.Create(fullPath)
|
|
if err != nil {
|
|
panic(fmt.Errorf("init db file failed, err: %v", err))
|
|
}
|
|
_ = f.Close()
|
|
}
|
|
|
|
newLogger := logger.New(
|
|
log.New(os.Stdout, "\r\n", log.LstdFlags),
|
|
logger.Config{
|
|
SlowThreshold: time.Second,
|
|
LogLevel: logger.Silent,
|
|
IgnoreRecordNotFoundError: true,
|
|
Colorful: false,
|
|
},
|
|
)
|
|
|
|
db, err := gorm.Open(sqlite.Open(fullPath), &gorm.Config{
|
|
DisableForeignKeyConstraintWhenMigrating: true,
|
|
Logger: newLogger,
|
|
})
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
sqlDB, dbError := db.DB()
|
|
if dbError != nil {
|
|
panic(dbError)
|
|
}
|
|
sqlDB.SetConnMaxIdleTime(10)
|
|
sqlDB.SetMaxOpenConns(100)
|
|
sqlDB.SetConnMaxLifetime(time.Hour)
|
|
|
|
global.DB = db
|
|
global.LOG.Info("init db successfully")
|
|
}
|