main.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "os"
  7. "os/signal"
  8. "syscall"
  9. "time"
  10. "go-common/app/service/live/xanchor/conf"
  11. "go-common/app/service/live/xanchor/server/grpc"
  12. "go-common/app/service/live/xanchor/server/http"
  13. "go-common/app/service/live/xanchor/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. panic(err)
  22. }
  23. log.Init(conf.Conf.Log)
  24. defer log.Close()
  25. log.Info("xanchor-service start")
  26. trace.Init(conf.Conf.Tracer)
  27. defer trace.Close()
  28. ecode.Init(conf.Conf.Ecode)
  29. svc := service.New(conf.Conf)
  30. http.Init(conf.Conf)
  31. // start grpc server
  32. svr, err := grpc.New(svc)
  33. if err != nil {
  34. panic(fmt.Sprintf("start xanchor grpc server fail! %s", err))
  35. }
  36. c := make(chan os.Signal, 1)
  37. signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
  38. for {
  39. s := <-c
  40. log.Info("get a signal %s", s.String())
  41. switch s {
  42. case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
  43. svc.Close()
  44. if svr != nil {
  45. svr.Shutdown(context.Background())
  46. }
  47. log.Info("xanchor-service exit")
  48. time.Sleep(time.Second)
  49. return
  50. case syscall.SIGHUP:
  51. default:
  52. return
  53. }
  54. }
  55. }