main.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. "context"
  6. "flag"
  7. "fmt"
  8. "log"
  9. "net"
  10. "os"
  11. "os/signal"
  12. "smart-government-management/impl/v1/user"
  13. "smart-government-management/utils"
  14. "syscall"
  15. "time"
  16. "smart-government-management/impl"
  17. "smart-government-management/parser"
  18. "github.com/jaryhe/gopkgs/database"
  19. "github.com/jaryhe/gopkgs/tasker"
  20. "smart-government-management/pb"
  21. "google.golang.org/grpc"
  22. )
  23. var (
  24. configFile = flag.String("config", "/etc/smart/common.yaml", "config file location")
  25. version = flag.Bool("version", false, "print the version")
  26. GitCommit = "library-import"
  27. Version = "library-import"
  28. // 重置开发者账号
  29. rd = flag.String("rd", "", " ")
  30. // 重置管理员账号
  31. ra = flag.String("ra", "", " ")
  32. )
  33. func showVersion() {
  34. fmt.Println("Version: ", Version)
  35. fmt.Println("GitCommit:", GitCommit)
  36. }
  37. func prepare(filename string) {
  38. // 加载配置
  39. err := parser.LoadConfig(filename)
  40. if err != nil {
  41. fmt.Printf("get conf failed, err: %+v\n\n", err)
  42. os.Exit(1)
  43. }
  44. // 注册处理函数
  45. // parser.Register(parser.MysqlHandler, parser.RedisHandler, parser.LoggerHandler)
  46. parser.Register(parser.LoggerHandler, parser.RedisHandler, parser.MysqlHandler, parser.InfluxdbHandler)
  47. // 执行注册的处理函数
  48. parser.Handle()
  49. }
  50. func run() {
  51. // 开始监听
  52. serveAddr := fmt.Sprintf("%s:%d", parser.Conf.Rpc.SmartGovernmentManagement.ServiceIp, parser.Conf.Rpc.SmartGovernmentManagement.ServicePort)
  53. log.Printf("Listening and serving TCP on %s\n", serveAddr)
  54. lis, err := net.Listen("tcp", serveAddr)
  55. if err != nil {
  56. os.Exit(1)
  57. }
  58. s := grpc.NewServer()
  59. impl.Register(s)
  60. go func() {
  61. if err := s.Serve(lis); err != nil {
  62. log.Fatalf("HTTP server listen failed. err: %s\n", err.Error())
  63. }
  64. }()
  65. conns := pb.SetupClients()
  66. for _, conn := range conns {
  67. defer conn.Close()
  68. }
  69. // 优雅关闭服务器
  70. sigChan := make(chan os.Signal, 1)
  71. // 捕获信号
  72. signal.Notify(sigChan, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
  73. sigValue := <-sigChan
  74. log.Printf("Got a signal:%v", sigValue)
  75. // 让tasker安全退出
  76. tasker.SignalNotify(sigValue)
  77. // 不管什么行为,都等待5秒退出
  78. log.Println("Start to shutdown server...")
  79. _, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  80. defer cancel()
  81. s.Stop()
  82. log.Println("Shutdown server finished.")
  83. }
  84. func main() {
  85. defer func() {
  86. database.Close()
  87. }()
  88. flag.Parse()
  89. if *version {
  90. showVersion()
  91. os.Exit(1)
  92. }
  93. prepare(*configFile)
  94. if *rd != "" {
  95. if utils.VerifyCredential(*rd) {
  96. user.ResetDevelop()
  97. }
  98. os.Exit(1)
  99. }
  100. if *ra != "" {
  101. if utils.VerifyCredential(*rd) {
  102. user.ResetAdmin()
  103. }
  104. os.Exit(1)
  105. }
  106. //go utils.Free()
  107. if err := user.InitDefaultUser(); err != nil {
  108. panic("初始化失败:"+err.Error())
  109. }
  110. run()
  111. return
  112. }