main.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "os"
  6. "os/signal"
  7. "syscall"
  8. "time"
  9. "go-common/app/service/main/coupon/conf"
  10. "go-common/app/service/main/coupon/http"
  11. rpc "go-common/app/service/main/coupon/rpc/server"
  12. grpc "go-common/app/service/main/coupon/server/grpc"
  13. "go-common/app/service/main/coupon/service"
  14. ecode "go-common/library/ecode/tip"
  15. "go-common/library/log"
  16. xrpc "go-common/library/net/rpc"
  17. )
  18. var (
  19. svc *service.Service
  20. rpcSvr *xrpc.Server
  21. )
  22. func main() {
  23. flag.Parse()
  24. if err := conf.Init(); err != nil {
  25. log.Error("conf.Init() error(%v)", err)
  26. panic(err)
  27. }
  28. // init log
  29. log.Init(conf.Conf.Log)
  30. defer log.Close()
  31. log.Info("coupon start")
  32. // ecode init
  33. ecode.Init(conf.Conf.Ecode)
  34. // service init
  35. svc = service.New(conf.Conf)
  36. // rpc init
  37. rpcSvr = rpc.New(conf.Conf, svc)
  38. // http init
  39. http.Init(conf.Conf, svc)
  40. // grpc
  41. ws := grpc.New(conf.Conf.WardenServer, svc)
  42. // init pprof conf.Conf.Perf
  43. // init signal
  44. c := make(chan os.Signal, 1)
  45. signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
  46. for {
  47. s := <-c
  48. log.Info("coupon get a signal %s", s.String())
  49. switch s {
  50. case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
  51. rpcSvr.Close()
  52. ws.Shutdown(context.Background())
  53. time.Sleep(time.Second * 1)
  54. log.Info("coupon exit")
  55. return
  56. case syscall.SIGHUP:
  57. default:
  58. return
  59. }
  60. }
  61. }