12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- package db
- import (
- "fmt"
- "github.com/astaxie/beego/orm"
- _ "github.com/go-sql-driver/mysql"
- _ "github.com/mattn/go-sqlite3"
- "nginx-ui/server/config"
- "nginx-ui/server/models"
- "nginx-ui/server/utils"
- "os"
- "time"
- )
- func Init() {
- dir := config.Config.DBDir
- if !utils.IsExist(dir) {
- os.MkdirAll(dir, 0777)
- }
- if config.Config.DbType == "sqlite" {
- orm.RegisterDriver("sqlite3", orm.DRSqlite)
- orm.RegisterDataBase("default", "sqlite3", dir+"/sqlite.db")
- } else {
- db := config.Config.DbConfig
- orm.RegisterDriver("mysql", orm.DRMySQL)
- url := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8", db.User, db.Pass, db.Host, db.Port, db.Name)
- orm.RegisterDataBase("default", "mysql", url)
- }
- orm.SetMaxIdleConns("default", 50)
- orm.SetMaxOpenConns("default", 200)
- //设置数据库时区
- orm.DefaultTimeLoc = time.Local
- orm.RegisterModel(new(models.Nginx))
- orm.RegisterModel(new(models.ServerHost))
- orm.RegisterModel(new(models.NginxCerts))
- orm.RegisterModel(new(models.User))
- orm.RegisterModel(new(models.LdapServer))
- orm.RegisterModel(new(models.LdapUser))
- orm.RunSyncdb("default", false, true)
- orm.Debug = true
- }
|