main.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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/cache"
  12. "iot_manager_service/util/logger"
  13. "net/url"
  14. )
  15. func main() {
  16. // 开始调试模式
  17. gin.SetMode("debug")
  18. logger.InitLog()
  19. // 实例化配置
  20. initDB()
  21. err := cache.InitRedis()
  22. if err != nil {
  23. panic(err)
  24. }
  25. engine := gin.Default()
  26. router.InitRouter(engine)
  27. _ = engine.Run(config.Instance().Server.Address)
  28. }
  29. func initDB() {
  30. cfg := config.Instance()
  31. 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)
  32. db, err := gorm.Open("mysql", dsn)
  33. if err != nil {
  34. panic(err)
  35. } else {
  36. db.DB().SetMaxOpenConns(32)
  37. db.DB().SetMaxIdleConns(5)
  38. db.LogMode(false)
  39. db.SetLogger(logger.Logger)
  40. }
  41. device.InitDB(db)
  42. system.InitDB(db)
  43. }