main.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package main
  4. import (
  5. "context"
  6. "flag"
  7. "fmt"
  8. "gopkg.in/ini.v1"
  9. "log"
  10. "net"
  11. "os"
  12. "os/signal"
  13. "property-management/impl/v1/user"
  14. "property-management/pb"
  15. "syscall"
  16. "time"
  17. "property-management/impl"
  18. "property-management/parser"
  19. "git.getensh.com/common/gopkgs/database"
  20. "git.getensh.com/common/gopkgs/tasker"
  21. "google.golang.org/grpc"
  22. )
  23. var (
  24. configFile = flag.String("config", "conf/common.yaml", "config file location")
  25. appConf = flag.String("appconf", "conf/app.conf", "app conf file location")
  26. version = flag.Bool("version", false, "print the version")
  27. GitCommit = "library-import"
  28. Version = "library-import"
  29. )
  30. func showVersion() {
  31. fmt.Println("Version: ", Version)
  32. fmt.Println("GitCommit:", GitCommit)
  33. }
  34. func prepare(filename string) {
  35. // 加载配置
  36. err := parser.LoadConfig(filename)
  37. if err != nil {
  38. fmt.Printf("get conf failed, err: %+v\n\n", err)
  39. os.Exit(1)
  40. }
  41. // 注册处理函数
  42. // parser.Register(parser.MysqlHandler, parser.RedisHandler, parser.LoggerHandler)
  43. parser.Register(
  44. parser.MysqlHandler,
  45. parser.LoggerHandler,
  46. parser.RedisHandler,
  47. )
  48. // 执行注册的处理函数
  49. parser.Handle()
  50. }
  51. func run() {
  52. // 开始监听
  53. serveAddr := fmt.Sprintf("%s:%d", parser.Conf.Rpc.Management.ServiceIp, parser.Conf.Rpc.Management.ServicePort)
  54. log.Printf("Listening and serving TCP on %s\n", serveAddr)
  55. lis, err := net.Listen("tcp", serveAddr)
  56. if err != nil {
  57. os.Exit(1)
  58. }
  59. s := grpc.NewServer()
  60. impl.Register(s)
  61. cfg, err := ini.Load(*appConf)
  62. if err != nil {
  63. fmt.Printf("Fail to read file: %v\n\n", err)
  64. os.Exit(1)
  65. }
  66. serviceIp := cfg.Section("").Key("service_ip").String()
  67. if len(parser.Conf.EtcdAddrs) > 0 {
  68. parser.EctdRegister(parser.Conf.EtcdAddrs, parser.Conf.Rpc.Management.ServiceName,
  69. fmt.Sprintf("%s:%v", serviceIp, parser.Conf.Rpc.Management.ServicePort),
  70. parser.Conf.Rpc.Prefix)
  71. }
  72. // 建立rpc客户端
  73. conns := pb.SetupClients()
  74. for _, conn := range conns {
  75. defer conn.Close()
  76. }
  77. user.InitUser()
  78. go func() {
  79. if err := s.Serve(lis); err != nil {
  80. log.Fatalf("HTTP server listen failed. err: %s\n", err.Error())
  81. }
  82. }()
  83. // 优雅关闭服务器
  84. sigChan := make(chan os.Signal, 1)
  85. // 捕获信号
  86. signal.Notify(sigChan, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
  87. sigValue := <-sigChan
  88. log.Printf("Got a signal:%v", sigValue)
  89. // 让tasker安全退出
  90. tasker.SignalNotify(sigValue)
  91. // 不管什么行为,都等待5秒退出
  92. log.Println("Start to shutdown server...")
  93. _, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  94. defer cancel()
  95. s.Stop()
  96. log.Println("Shutdown server finished.")
  97. }
  98. func main() {
  99. defer func() {
  100. database.Close()
  101. }()
  102. flag.Parse()
  103. if *version {
  104. showVersion()
  105. os.Exit(1)
  106. }
  107. prepare(*configFile)
  108. run()
  109. return
  110. }