sql.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. package initial
  2. import (
  3. "github.com/astaxie/beego"
  4. "net/url"
  5. "github.com/astaxie/beego/orm"
  6. )
  7. func InitSql() {
  8. dbhost := beego.AppConfig.String("db.host")
  9. dbport := beego.AppConfig.String("db.port")
  10. dbuser := beego.AppConfig.String("db.user")
  11. dbpassword := beego.AppConfig.String("db.password")
  12. dbname := beego.AppConfig.String("db.name")
  13. timezone := beego.AppConfig.String("db.timezone")
  14. if dbport == "" {
  15. dbport = "3306"
  16. }
  17. dsn := dbuser + ":" + dbpassword + "@tcp(" + dbhost + ":" + dbport + ")/" + dbname + "?charset=utf8"
  18. // fmt.Println(dsn)
  19. if timezone != "" {
  20. dsn = dsn + "&loc=" + url.QueryEscape(timezone)
  21. }
  22. orm.RegisterDataBase("default", "mysql", dsn)
  23. //orm.RegisterModel(new(backgroundm.Auth), new(backgroundm.Role), new(backgroundm.RoleAuth), new(backgroundm.Admin),
  24. // new(backgroundm.Group), new(backgroundm.Env), new(backgroundm.Code), new(backgroundm.ApiSource), new(backgroundm.ApiDetail),
  25. // new(backgroundm.ApiPublic),
  26. // new(backgroundm.Template),new(commonm.PropertyNotice),
  27. // new(frontm.User),
  28. //)
  29. if beego.AppConfig.String("runmode") == "dev" {
  30. orm.Debug = true
  31. }
  32. }