db.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package db
  2. import (
  3. "fmt"
  4. "github.com/astaxie/beego/orm"
  5. _ "github.com/go-sql-driver/mysql"
  6. _ "github.com/mattn/go-sqlite3"
  7. "nginx-ui/server/config"
  8. "nginx-ui/server/models"
  9. "nginx-ui/server/utils"
  10. "os"
  11. "time"
  12. )
  13. func Init() {
  14. dir := config.Config.DBDir
  15. if !utils.IsExist(dir) {
  16. os.MkdirAll(dir, 0777)
  17. }
  18. if config.Config.DbType == "sqlite" {
  19. orm.RegisterDriver("sqlite3", orm.DRSqlite)
  20. orm.RegisterDataBase("default", "sqlite3", dir+"/sqlite.db")
  21. } else {
  22. db := config.Config.DbConfig
  23. orm.RegisterDriver("mysql", orm.DRMySQL)
  24. url := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8", db.User, db.Pass, db.Host, db.Port, db.Name)
  25. orm.RegisterDataBase("default", "mysql", url)
  26. }
  27. orm.SetMaxIdleConns("default", 50)
  28. orm.SetMaxOpenConns("default", 200)
  29. //设置数据库时区
  30. orm.DefaultTimeLoc = time.Local
  31. orm.RegisterModel(new(models.Nginx))
  32. orm.RegisterModel(new(models.ServerHost))
  33. orm.RegisterModel(new(models.NginxCerts))
  34. orm.RegisterModel(new(models.User))
  35. orm.RegisterModel(new(models.LdapServer))
  36. orm.RegisterModel(new(models.LdapUser))
  37. orm.RegisterModel(new(models.LdapOrganize))
  38. orm.RunSyncdb("default", false, true)
  39. orm.Debug = true
  40. }