http.go 796 B

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