http.go 781 B

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