main.go 1.1 KB

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