db.go 720 B

123456789101112131415161718192021222324252627282930313233
  1. package db
  2. import (
  3. "github.com/astaxie/beego/orm"
  4. _ "github.com/mattn/go-sqlite3"
  5. "nginx-ui/server/config"
  6. "nginx-ui/server/models"
  7. "nginx-ui/server/utils"
  8. "os"
  9. "time"
  10. )
  11. func Init() {
  12. dir := config.Config.DBDir
  13. if !utils.IsExist(dir) {
  14. os.MkdirAll(dir, 0777)
  15. }
  16. orm.RegisterDriver("sqlite3", orm.DRSqlite)
  17. orm.RegisterDataBase("default", "sqlite3", dir+"/sqlite.db")
  18. orm.SetMaxIdleConns("default", 50)
  19. orm.SetMaxOpenConns("default", 200)
  20. //设置数据库时区
  21. orm.DefaultTimeLoc = time.Local
  22. orm.RegisterModel(new(models.Nginx))
  23. orm.RegisterModel(new(models.ServerHost))
  24. orm.RegisterModel(new(models.NginxCerts))
  25. orm.RegisterModel(new(models.User))
  26. orm.RunSyncdb("default", false, true)
  27. }