main.go 2.4 KB

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