main.go 1.4 KB

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