main.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package main
  4. import (
  5. "dust-monitor/handler/v1"
  6. "dust-monitor/parser"
  7. "dust-monitor/pb"
  8. "dust-monitor/tcp_packet"
  9. "dust-monitor/timetask"
  10. "flag"
  11. "fmt"
  12. "net"
  13. "net/http"
  14. "os"
  15. "log"
  16. "github.com/gin-gonic/gin"
  17. swaggerFiles "github.com/swaggo/files"
  18. ginSwagger "github.com/swaggo/gin-swagger"
  19. "dust-monitor/route"
  20. )
  21. var (
  22. configFile = flag.String("config", "conf/common.yaml", "config file location")
  23. version = flag.Bool("version", false, "print the version")
  24. GitCommit = "library-import"
  25. Version = "library-import"
  26. )
  27. func showVersion() {
  28. fmt.Println("Version: ", Version)
  29. fmt.Println("GitCommit:", GitCommit)
  30. }
  31. func prepare(filename string) {
  32. // 加载配置
  33. err := parser.LoadConfig(filename)
  34. if err != nil {
  35. fmt.Printf("get conf failed, err: %+v\n\n", err)
  36. os.Exit(1)
  37. }
  38. // 注册处理函数
  39. // parser.Register(parser.MysqlHandler, parser.RedisHandler, parser.LoggerHandler)
  40. //parser.Register(parser.LoggerHandler, parser.MysqlHandler, parser.RedisHandler)
  41. parser.Register(parser.LoggerHandler, parser.MysqlHandler, parser.InfluxdbHandler)
  42. // 执行注册的处理函数
  43. parser.Handle()
  44. v1.InitAreaCodeMap()
  45. // 建立rpc客户端
  46. conns := pb.SetupClients()
  47. for _, conn := range conns {
  48. defer conn.Close()
  49. }
  50. timetask.Init()
  51. }
  52. func runDeviceTcp(ip string, port int) {
  53. service := fmt.Sprintf("%s:%d", ip, port)
  54. tcpAddr, err := net.ResolveTCPAddr("tcp4", service)
  55. if err != nil {
  56. fmt.Println("resolve tcp addr err:", err)
  57. os.Exit(1)
  58. }
  59. listener, err := net.ListenTCP("tcp", tcpAddr)
  60. if err != nil {
  61. fmt.Println("listen tcp err:", err)
  62. os.Exit(2)
  63. }
  64. for {
  65. fmt.Printf("listen:%v\n", listener.Addr().String())
  66. conn, err := listener.Accept()
  67. if err != nil {
  68. continue
  69. }
  70. go tcp_packet.HandleSocketClient(conn)
  71. }
  72. }
  73. func runHttp() {
  74. // 新建engine
  75. engine := gin.New()
  76. handlers := gin.HandlersChain{}
  77. // 根据env_mode设置gin的运行模式
  78. switch parser.Conf.RunMode {
  79. case "test":
  80. gin.SetMode(gin.TestMode)
  81. case "prod":
  82. gin.SetMode(gin.ReleaseMode)
  83. case "dev":
  84. fallthrough
  85. default:
  86. gin.SetMode(gin.DebugMode)
  87. // 根据配置是否生成swagger
  88. engine.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
  89. // 输出打印日志
  90. handlers = append(handlers, gin.Logger())
  91. }
  92. // 处理panic
  93. handlers = append(handlers, gin.Recovery())
  94. engine.Use(handlers...)
  95. // 设定路由
  96. route.SetupRoute(engine)
  97. // 建立rpc客户端
  98. conns := pb.SetupClients()
  99. for _, conn := range conns {
  100. defer conn.Close()
  101. }
  102. // 创建监听服务
  103. server := &http.Server{
  104. Addr: fmt.Sprintf("%s:%d", parser.Conf.DustMonitor.GatewayIp,
  105. parser.Conf.DustMonitor.GatewayPort),
  106. Handler: engine,
  107. }
  108. // 开始监听
  109. log.Printf("Listening and serving HTTP on %s\n", server.Addr)
  110. go func() {
  111. if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
  112. log.Fatalf("HTTP server listen failed. err: %s\n", err.Error())
  113. }
  114. }()
  115. }
  116. func main() {
  117. flag.Parse()
  118. if *version {
  119. showVersion()
  120. os.Exit(1)
  121. }
  122. prepare(*configFile)
  123. runHttp()
  124. runDeviceTcp(parser.Conf.DustMonitor.ServiceIp, parser.Conf.DustMonitor.ServicePort)
  125. return
  126. }