支持sqlite
This commit is contained in:
@@ -9,46 +9,86 @@ package models
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/astaxie/beego/orm"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
var StartTime int64
|
||||
|
||||
func Init(startTime int64) {
|
||||
StartTime = startTime
|
||||
dbhost := beego.AppConfig.String("db.host")
|
||||
dbport := beego.AppConfig.String("db.port")
|
||||
dbuser := beego.AppConfig.String("db.user")
|
||||
dbpassword := beego.AppConfig.String("db.password")
|
||||
dbname := beego.AppConfig.String("db.name")
|
||||
timezone := beego.AppConfig.String("db.timezone")
|
||||
if dbport == "" {
|
||||
dbport = "3306"
|
||||
dbType := beego.AppConfig.String("db.type")
|
||||
if dbType == "" {
|
||||
dbType = "mysql"
|
||||
}
|
||||
dsn := dbuser + ":" + dbpassword + "@tcp(" + dbhost + ":" + dbport + ")/" + dbname + "?charset=utf8"
|
||||
if timezone != "" {
|
||||
dsn = dsn + "&loc=" + url.QueryEscape(timezone)
|
||||
}
|
||||
orm.RegisterDataBase("default", "mysql", dsn)
|
||||
orm.RegisterModel(
|
||||
new(Admin),
|
||||
new(Auth),
|
||||
new(Role),
|
||||
new(RoleAuth),
|
||||
new(ServerGroup),
|
||||
new(TaskServer),
|
||||
new(Ban),
|
||||
new(Group),
|
||||
new(Task),
|
||||
new(TaskLog),
|
||||
new(NotifyTpl),
|
||||
)
|
||||
|
||||
if beego.AppConfig.String("runmode") == "dev" {
|
||||
orm.Debug = true
|
||||
if dbType == "sqlite" {
|
||||
dbpath := beego.AppConfig.String("db.path")
|
||||
if dbpath == "" {
|
||||
dbpath = "./data/ppgo_job.db"
|
||||
}
|
||||
|
||||
dir := filepath.Dir(dbpath)
|
||||
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
dsn := dbpath
|
||||
orm.RegisterDataBase("default", "sqlite3", dsn)
|
||||
orm.RegisterModel(
|
||||
new(Admin),
|
||||
new(Auth),
|
||||
new(Role),
|
||||
new(RoleAuth),
|
||||
new(ServerGroup),
|
||||
new(TaskServer),
|
||||
new(Ban),
|
||||
new(Group),
|
||||
new(Task),
|
||||
new(TaskLog),
|
||||
new(NotifyTpl),
|
||||
)
|
||||
|
||||
if beego.AppConfig.String("runmode") == "dev" {
|
||||
orm.Debug = true
|
||||
}
|
||||
} else {
|
||||
dbhost := beego.AppConfig.String("db.host")
|
||||
dbport := beego.AppConfig.String("db.port")
|
||||
dbuser := beego.AppConfig.String("db.user")
|
||||
dbpassword := beego.AppConfig.String("db.password")
|
||||
dbname := beego.AppConfig.String("db.name")
|
||||
timezone := beego.AppConfig.String("db.timezone")
|
||||
if dbport == "" {
|
||||
dbport = "3306"
|
||||
}
|
||||
dsn := dbuser + ":" + dbpassword + "@tcp(" + dbhost + ":" + dbport + ")/" + dbname + "?charset=utf8"
|
||||
if timezone != "" {
|
||||
dsn = dsn + "&loc=" + url.QueryEscape(timezone)
|
||||
}
|
||||
orm.RegisterDataBase("default", "mysql", dsn)
|
||||
orm.RegisterModel(
|
||||
new(Admin),
|
||||
new(Auth),
|
||||
new(Role),
|
||||
new(RoleAuth),
|
||||
new(ServerGroup),
|
||||
new(TaskServer),
|
||||
new(Ban),
|
||||
new(Group),
|
||||
new(Task),
|
||||
new(TaskLog),
|
||||
new(NotifyTpl),
|
||||
)
|
||||
|
||||
if beego.AppConfig.String("runmode") == "dev" {
|
||||
orm.Debug = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user