main.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package main
  2. import (
  3. "github.com/gin-gonic/gin"
  4. _ "github.com/go-sql-driver/mysql"
  5. "github.com/jinzhu/gorm"
  6. device "iot_manager_service/app/device/dao"
  7. multimedia "iot_manager_service/app/multimedia/dao"
  8. system "iot_manager_service/app/system/dao"
  9. "iot_manager_service/config"
  10. _ "iot_manager_service/config"
  11. "iot_manager_service/router"
  12. "iot_manager_service/util/cache"
  13. "iot_manager_service/util/logger"
  14. "net/url"
  15. )
  16. func main() {
  17. // 开始调试模式
  18. gin.SetMode("debug")
  19. logger.InitLog()
  20. // 实例化配置
  21. initDB()
  22. err := cache.InitRedis()
  23. if err != nil {
  24. panic(err)
  25. }
  26. engine := gin.Default()
  27. router.InitRouter(engine)
  28. _ = engine.Run(config.Instance().Server.Address)
  29. }
  30. func initDB() {
  31. cfg := config.Instance()
  32. 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)
  33. db, err := gorm.Open("mysql", dsn)
  34. if err != nil {
  35. panic(err)
  36. } else {
  37. db.DB().SetMaxOpenConns(32)
  38. db.DB().SetMaxIdleConns(5)
  39. db.LogMode(false)
  40. db.SetLogger(logger.Logger)
  41. }
  42. device.InitDB(db)
  43. system.InitDB(db)
  44. multimedia.InitDB(db)
  45. }