main.go 2.5 KB

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