http.go 842 B

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