1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-01-19 16:29:17 +08:00

32 lines
742 B
Go
Raw Normal View History

2022-08-16 23:30:23 +08:00
package db
2022-11-16 18:27:22 +08:00
import (
"fmt"
"os"
2022-11-16 18:27:22 +08:00
"github.com/1Panel-dev/1Panel/backend/global"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
2022-08-16 23:30:23 +08:00
func Init() {
if _, err := os.Stat(global.CONF.System.DbPath); err != nil {
if err := os.MkdirAll(global.CONF.System.DbPath, os.ModePerm); err != nil {
panic(fmt.Errorf("init db dir falied, err: %v", err))
}
}
fullPath := global.CONF.System.DbPath + "/" + global.CONF.System.DbFile
if _, err := os.Stat(fullPath); err != nil {
if _, err := os.Create(fullPath); err != nil {
panic(fmt.Errorf("init db file falied, err: %v", err))
}
}
db, err := gorm.Open(sqlite.Open(fullPath), &gorm.Config{
DisableForeignKeyConstraintWhenMigrating: true,
})
2022-11-16 18:27:22 +08:00
if err != nil {
panic(err)
2022-08-16 23:30:23 +08:00
}
2022-11-16 18:27:22 +08:00
global.DB = db
2022-08-16 23:30:23 +08:00
}