http.go 894 B

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