main.go 1018 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package main
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "github.com/jinzhu/gorm"
  5. device "iot_manager_service/app/device/dao"
  6. system "iot_manager_service/app/system/dao"
  7. "iot_manager_service/config"
  8. _ "iot_manager_service/config"
  9. "iot_manager_service/router"
  10. "iot_manager_service/util"
  11. "net/url"
  12. )
  13. func main() {
  14. // 开始调试模式
  15. gin.SetMode("debug")
  16. // 实例化配置
  17. initDB()
  18. err := util.InitRedis()
  19. if err != nil {
  20. panic(err)
  21. }
  22. engine := gin.Default()
  23. router.InitRouter(engine)
  24. _ = engine.Run(config.Instance().Server.Address)
  25. }
  26. func initDB() {
  27. cfg := config.Instance()
  28. dsn := cfg.Database.User + ":" + cfg.Database.Password + "@tcp(" + cfg.Database.Host + ":" + cfg.Database.Port + ")/" + cfg.Database.Name + "?charset=utf8&parseTime=True" + "&loc=" + url.QueryEscape(cfg.Database.Timezone)
  29. db, err := gorm.Open("mysql", dsn)
  30. if err != nil {
  31. panic(err)
  32. } else {
  33. db.DB().SetMaxOpenConns(32)
  34. db.DB().SetMaxIdleConns(5)
  35. db.LogMode(false)
  36. }
  37. device.InitDB(db)
  38. system.InitDB(db)
  39. }