123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- 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.UserRole))
- orm.RegisterModel(new(models.LdapServer))
- orm.RegisterModel(new(models.LdapUser))
- orm.RegisterModel(new(models.LdapOrganize))
- orm.RegisterModel(new(models.Setting))
- orm.RegisterModel(new(models.SettingRoute))
- orm.RunSyncdb("default", false, true)
- orm.Debug = true
- }
|