main.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package main
  2. import (
  3. "github.com/gin-gonic/gin"
  4. _ "github.com/go-sql-driver/mysql"
  5. "github.com/jinzhu/gorm"
  6. device "iot_manager_service/app/device/dao"
  7. system "iot_manager_service/app/system/dao"
  8. "iot_manager_service/config"
  9. _ "iot_manager_service/config"
  10. "iot_manager_service/router"
  11. "iot_manager_service/util"
  12. "net/url"
  13. )
  14. func main() {
  15. // 开始调试模式
  16. gin.SetMode("debug")
  17. // 实例化配置
  18. initDB()
  19. err := util.InitRedis()
  20. if err != nil {
  21. panic(err)
  22. }
  23. engine := gin.Default()
  24. router.InitRouter(engine)
  25. _ = engine.Run(config.Instance().Server.Address)
  26. }
  27. func initDB() {
  28. cfg := config.Instance()
  29. 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)
  30. db, err := gorm.Open("mysql", dsn)
  31. if err != nil {
  32. panic(err)
  33. } else {
  34. db.DB().SetMaxOpenConns(32)
  35. db.DB().SetMaxIdleConns(5)
  36. db.LogMode(false)
  37. }
  38. device.InitDB(db)
  39. system.InitDB(db)
  40. }