main.go 1.2 KB

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