2022-08-16 15:30:23 +00:00
|
|
|
package db
|
|
|
|
|
2022-11-16 10:27:22 +00:00
|
|
|
import (
|
2023-01-29 08:38:34 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2022-11-16 10:27:22 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
|
|
|
"gorm.io/driver/sqlite"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
2022-08-16 15:30:23 +00:00
|
|
|
|
|
|
|
func Init() {
|
2023-01-29 08:38:34 +00:00
|
|
|
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{})
|
2022-11-16 10:27:22 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2022-08-16 15:30:23 +00:00
|
|
|
}
|
2022-11-16 10:27:22 +00:00
|
|
|
global.DB = db
|
2022-08-16 15:30:23 +00:00
|
|
|
}
|