models.go 853 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package models
  2. import (
  3. "fmt"
  4. "log"
  5. "time"
  6. "../pkg/setting"
  7. "../routers/restful"
  8. "github.com/astaxie/beego/logs"
  9. "github.com/xormplus/xorm"
  10. )
  11. var Engine *xorm.Engine
  12. func Setup() {
  13. var err error
  14. // restful 接口的 数据库初始化
  15. if err := restful.SQLInit("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=True&loc=Local",
  16. setting.DatabaseSetting.User,
  17. setting.DatabaseSetting.Password,
  18. setting.DatabaseSetting.Host,
  19. setting.DatabaseSetting.Name), 1000, 10, setting.DatabaseSetting.ShowXormlog); err != nil {
  20. log.Fatal(err.Error())
  21. fmt.Println("dbs 数据库初始化失败 ")
  22. }
  23. Engine = restful.Engine
  24. if err != nil {
  25. log.Fatalf("models.Setup err: %v", err)
  26. }
  27. Engine.SetConnMaxLifetime(time.Second * 30)
  28. Engine.ShowSQL(true)
  29. if err = Engine.Ping(); err != nil {
  30. logs.Error(err)
  31. panic(err)
  32. }
  33. }