main.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. "time"
  20. )
  21. func main() {
  22. // 开始调试模式
  23. gin.SetMode("debug")
  24. logger.InitLog()
  25. // 实例化配置
  26. initDB()
  27. err := cache.InitRedis()
  28. if err != nil {
  29. panic(err)
  30. }
  31. minio.InitMinio()
  32. controller.CalcTask()
  33. engine := gin.Default()
  34. router.InitRouter(engine)
  35. _ = engine.Run(config.Instance().Server.Address)
  36. }
  37. func initDB() {
  38. cfg := config.Instance()
  39. 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)
  40. logCfg := gormLogger.New(logger.Logger, gormLogger.Config{
  41. SlowThreshold: 200 * time.Millisecond,
  42. LogLevel: gormLogger.Info,
  43. Colorful: true,
  44. })
  45. db, err := gorm.Open(mysql.New(mysql.Config{
  46. DSN: dsn,
  47. }), &gorm.Config{Logger: logCfg})
  48. if err != nil {
  49. panic(err)
  50. } else {
  51. sqlDB, err := db.DB()
  52. if err != nil {
  53. panic(err)
  54. }
  55. if err := sqlDB.Ping(); err != nil {
  56. panic(err)
  57. }
  58. sqlDB.SetMaxOpenConns(32)
  59. sqlDB.SetMaxOpenConns(5)
  60. }
  61. device.InitDB(db)
  62. system.InitDB(db)
  63. multimedia.InitDB(db)
  64. data.InitDB(db)
  65. }