main.go 956 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package main
  2. import (
  3. "flag"
  4. "math/rand"
  5. "os"
  6. "os/signal"
  7. "syscall"
  8. "time"
  9. "go-common/app/infra/discovery/conf"
  10. "go-common/app/infra/discovery/http"
  11. "go-common/app/infra/discovery/service"
  12. "go-common/library/log"
  13. )
  14. func main() {
  15. flag.Parse()
  16. if err := conf.Init(); err != nil {
  17. log.Error("conf.Init() error(%v)", err)
  18. panic(err)
  19. }
  20. // init log
  21. log.Init(conf.Conf.Log)
  22. defer log.Close()
  23. log.Info("discovery start")
  24. // service init
  25. rand.Seed(time.Now().UnixNano())
  26. svc, cancel := service.New(conf.Conf)
  27. http.Init(conf.Conf, svc)
  28. // init signal
  29. c := make(chan os.Signal, 1)
  30. signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
  31. for {
  32. s := <-c
  33. log.Info("discovery get a signal %s", s.String())
  34. switch s {
  35. case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
  36. cancel()
  37. time.Sleep(time.Second)
  38. log.Info("discovery exit")
  39. return
  40. case syscall.SIGHUP:
  41. default:
  42. return
  43. }
  44. }
  45. }