http.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package http
  2. import (
  3. "net/http"
  4. "go-common/app/service/main/spy/conf"
  5. "go-common/app/service/main/spy/service"
  6. "go-common/library/log"
  7. bm "go-common/library/net/http/blademaster"
  8. "go-common/library/net/http/blademaster/middleware/verify"
  9. )
  10. var (
  11. spySvc *service.Service
  12. verifyN *verify.Verify
  13. )
  14. // Init init http server.
  15. func Init(c *conf.Config, s *service.Service) {
  16. spySvc = s
  17. verifyN = verify.New(nil)
  18. // init inner router
  19. engineInner := bm.DefaultServer(c.BM)
  20. innerRouter(engineInner)
  21. // init local server
  22. if err := engineInner.Start(); err != nil {
  23. log.Error("xhttp.Serve error(%v)", err)
  24. panic(err)
  25. }
  26. }
  27. // innerRouter init inner router.
  28. func innerRouter(e *bm.Engine) {
  29. // health check
  30. e.Ping(ping)
  31. e.Register(register)
  32. //new defined api lists
  33. group := e.Group("/x/internal/v1/spy", verifyN.Verify)
  34. {
  35. group.GET("/info", info)
  36. group.GET("/purge", purgeUser)
  37. group.POST("/purge2", purgeUser2)
  38. group.GET("/stat", stat)
  39. }
  40. }
  41. // ping check server ok.
  42. func ping(c *bm.Context) {
  43. if spySvc.Ping(c) != nil {
  44. log.Error("spy-service service ping error")
  45. c.AbortWithStatus(http.StatusServiceUnavailable)
  46. }
  47. }
  48. // register check server ok.
  49. func register(c *bm.Context) {
  50. c.JSON(map[string]interface{}{}, nil)
  51. }