http.go 760 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package http
  2. import (
  3. "net/http"
  4. "go-common/app/job/main/dm/conf"
  5. "go-common/app/job/main/dm/service"
  6. "go-common/library/log"
  7. bm "go-common/library/net/http/blademaster"
  8. )
  9. var (
  10. dmSvc *service.Service
  11. )
  12. // Init new dm job service
  13. func Init(c *conf.Config, s *service.Service) {
  14. dmSvc = s
  15. engine := bm.DefaultServer(c.HTTPServer)
  16. initRouter(engine)
  17. // run http server
  18. if err := engine.Start(); err != nil {
  19. log.Error("bm.DefaultServer error(%v)", err)
  20. panic(err)
  21. }
  22. }
  23. // initRouter init router.
  24. func initRouter(e *bm.Engine) {
  25. e.Ping(ping)
  26. }
  27. // ping check server ok.
  28. func ping(c *bm.Context) {
  29. if err := dmSvc.Ping(c); err != nil {
  30. log.Error("dm-job service ping error(%v)", err)
  31. c.AbortWithStatus(http.StatusServiceUnavailable)
  32. }
  33. }