models.go 752 B

1234567891011121314151617181920212223242526272829303132
  1. package models
  2. import (
  3. "fmt"
  4. "time"
  5. "tmr-watch/conf/setting"
  6. "tmr-watch/http/handle/restful"
  7. "github.com/xormplus/xorm"
  8. )
  9. var Engine *xorm.Engine
  10. func Setup() {
  11. // restful 接口的 数据库初始化
  12. sqlDNS := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=True&loc=Local",
  13. setting.DatabaseSetting.User,
  14. setting.DatabaseSetting.Password,
  15. setting.DatabaseSetting.Host,
  16. setting.DatabaseSetting.Name,
  17. )
  18. if err := restful.SQLInit(setting.DatabaseSetting.Type, sqlDNS, 1000, 10, setting.DatabaseSetting.ShowXormlog); err != nil {
  19. panic(err)
  20. }
  21. Engine = restful.Engine
  22. Engine.SetConnMaxLifetime(time.Second * 30)
  23. Engine.ShowSQL(setting.DatabaseSetting.ShowXormlog)
  24. if err := Engine.Ping(); err != nil {
  25. panic(err)
  26. }
  27. }