main.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package main
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "gorm.io/driver/mysql"
  5. "gorm.io/gorm"
  6. gormLogger "gorm.io/gorm/logger"
  7. "iot_manager_service/app/data/controller"
  8. data "iot_manager_service/app/data/dao"
  9. device "iot_manager_service/app/device/dao"
  10. multimedia "iot_manager_service/app/multimedia/dao"
  11. record "iot_manager_service/app/record/dao"
  12. system "iot_manager_service/app/system/dao"
  13. "iot_manager_service/config"
  14. _ "iot_manager_service/config"
  15. "iot_manager_service/router"
  16. "iot_manager_service/util/cache"
  17. "iot_manager_service/util/logger"
  18. "iot_manager_service/util/minio"
  19. "net/url"
  20. "os"
  21. "time"
  22. )
  23. func main() {
  24. // 开始调试模式
  25. gin.SetMode("debug")
  26. logger.InitLog()
  27. // 实例化配置
  28. initDB()
  29. err := cache.InitRedis()
  30. if err != nil {
  31. panic(err)
  32. }
  33. minio.InitMinio()
  34. controller.CalcTask()
  35. engine := gin.Default()
  36. router.InitRouter(engine)
  37. _ = engine.Run(config.Instance().Server.Address)
  38. }
  39. func initDB() {
  40. cfg := config.Instance()
  41. 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)
  42. logCfg := gormLogger.New(logger.Logger, gormLogger.Config{
  43. SlowThreshold: 200 * time.Millisecond,
  44. LogLevel: gormLogger.Info,
  45. Colorful: true,
  46. })
  47. // 如果是本地开发环境 则 打印gorm 查询日志到控制台
  48. if os.Getenv("DEV_ENV") == "1" {
  49. logCfg = gormLogger.Default
  50. }
  51. //logCfg = gormLogger.Default
  52. db, err := gorm.Open(mysql.New(mysql.Config{
  53. DSN: dsn,
  54. }), &gorm.Config{Logger: logCfg})
  55. if err != nil {
  56. panic(err)
  57. } else {
  58. sqlDB, err := db.DB()
  59. if err != nil {
  60. panic(err)
  61. }
  62. if err := sqlDB.Ping(); err != nil {
  63. panic(err)
  64. }
  65. sqlDB.SetMaxOpenConns(32)
  66. sqlDB.SetMaxOpenConns(5)
  67. }
  68. device.InitDB(db)
  69. system.InitDB(db)
  70. multimedia.InitDB(db)
  71. data.InitDB(db)
  72. record.InitDB(db)
  73. }