main.go 1.8 KB

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