main.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. operation "iot_manager_service/app/operation/dao"
  12. record "iot_manager_service/app/record/dao"
  13. system "iot_manager_service/app/system/dao"
  14. warn "iot_manager_service/app/warn/dao"
  15. "iot_manager_service/config"
  16. _ "iot_manager_service/config"
  17. "iot_manager_service/router"
  18. "iot_manager_service/util/cache"
  19. "iot_manager_service/util/logger"
  20. "iot_manager_service/util/minio"
  21. "net/url"
  22. "os"
  23. "time"
  24. )
  25. func main() {
  26. // 只在本地开调试模式
  27. if isDevEnv() {
  28. gin.SetMode(gin.DebugMode)
  29. } else {
  30. gin.SetMode(gin.ReleaseMode)
  31. }
  32. logger.InitLog()
  33. // 实例化配置
  34. initDB()
  35. err := cache.InitRedis()
  36. if err != nil {
  37. panic(err)
  38. }
  39. minio.InitMinio()
  40. controller.CalcTask()
  41. engine := gin.Default()
  42. router.InitRouter(engine)
  43. if isDevEnv() {
  44. println(gin.DebugMode + "当前为开发环境,会产生较多调试日志")
  45. } else {
  46. println(gin.ReleaseMode)
  47. }
  48. _ = engine.Run(config.Instance().Server.Address)
  49. }
  50. func initDB() {
  51. cfg := config.Instance()
  52. 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)
  53. logCfg := gormLogger.New(logger.Logger, gormLogger.Config{
  54. SlowThreshold: 200 * time.Millisecond,
  55. LogLevel: gormLogger.Info,
  56. Colorful: true,
  57. })
  58. // 如果是本地开发环境 则 打印gorm 查询日志到控制台
  59. if isDevEnv() {
  60. logCfg = gormLogger.Default
  61. }
  62. //logCfg = gormLogger.Default
  63. db, err := gorm.Open(mysql.New(mysql.Config{
  64. DSN: dsn,
  65. }), &gorm.Config{Logger: logCfg})
  66. if err != nil {
  67. panic(err)
  68. } else {
  69. sqlDB, err := db.DB()
  70. if err != nil {
  71. panic(err)
  72. }
  73. if err := sqlDB.Ping(); err != nil {
  74. panic(err)
  75. }
  76. sqlDB.SetMaxOpenConns(32)
  77. sqlDB.SetMaxOpenConns(5)
  78. sqlDB.SetConnMaxLifetime(time.Hour * 4) //括号里面是超时时间,要小于数据库的超时时间 返回 invalid connection的问题
  79. }
  80. device.InitDB(db)
  81. system.InitDB(db)
  82. multimedia.InitDB(db)
  83. data.InitDB(db)
  84. record.InitDB(db)
  85. warn.InitDB(db)
  86. operation.InitDB(db)
  87. }
  88. // isDevEnv 是否开发环境 本地开发需要添加环境变量
  89. //
  90. // [windows] CMD下执行 setx "DEV_ENV" "1"
  91. // [linux] export DEV_ENV="1"
  92. func isDevEnv() bool {
  93. //return true
  94. if os.Getenv("DEV_ENV") == "1" {
  95. return true
  96. }
  97. return false
  98. }