main.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "os"
  6. "os/signal"
  7. "syscall"
  8. "time"
  9. "go-common/app/service/bbq/push/conf"
  10. "go-common/app/service/bbq/push/server/grpc"
  11. "go-common/app/service/bbq/push/server/http"
  12. "go-common/app/service/bbq/push/service"
  13. ecode "go-common/library/ecode/tip"
  14. "go-common/library/log"
  15. "go-common/library/net/trace"
  16. )
  17. func main() {
  18. flag.Parse()
  19. if err := conf.Init(); err != nil {
  20. panic(err)
  21. }
  22. log.Init(conf.Conf.Log)
  23. defer log.Close()
  24. log.Info("start")
  25. trace.Init(conf.Conf.Tracer)
  26. defer trace.Close()
  27. ecode.Init(conf.Conf.Ecode)
  28. serv := service.New(conf.Conf)
  29. http.Init(conf.Conf, serv)
  30. gsrv, err := grpc.New(conf.Conf.RPCConf, serv)
  31. if err != nil {
  32. log.Error("grpc server start error: %s", err)
  33. return
  34. }
  35. c := make(chan os.Signal, 1)
  36. signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
  37. ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
  38. defer cancel()
  39. for {
  40. s := <-c
  41. log.Info("get a signal %s", s.String())
  42. switch s {
  43. case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
  44. gsrv.Shutdown(ctx)
  45. log.Info("exit")
  46. return
  47. case syscall.SIGHUP:
  48. default:
  49. return
  50. }
  51. }
  52. }