http.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package http
  2. import (
  3. "net/http"
  4. "go-common/app/interface/main/captcha/conf"
  5. "go-common/app/interface/main/captcha/service"
  6. "go-common/library/log"
  7. bm "go-common/library/net/http/blademaster"
  8. "go-common/library/net/http/blademaster/middleware/rate"
  9. verifyx "go-common/library/net/http/blademaster/middleware/verify"
  10. )
  11. var (
  12. svr *service.Service
  13. verifySvc *verifyx.Verify
  14. )
  15. // Init captcha http init.
  16. func Init(c *conf.Config, s *service.Service) (err error) {
  17. svr = s
  18. verifySvc = verifyx.New(c.Verify)
  19. rateLimit := rate.New(c.Rate)
  20. engineOuter := bm.DefaultServer(c.BM.Outer)
  21. engineOuter.Use(rateLimit)
  22. outerRouter(engineOuter)
  23. interRouter(engineOuter)
  24. if err := engineOuter.Start(); err != nil {
  25. log.Error("bm.DefaultServer error(%v)", err)
  26. panic(err)
  27. }
  28. return
  29. }
  30. func outerRouter(e *bm.Engine) {
  31. e.Ping(ping)
  32. group := e.Group("/x/v1/captcha")
  33. group.GET("/get", get)
  34. }
  35. func interRouter(e *bm.Engine) {
  36. group := e.Group("/x/internal/v1/captcha")
  37. group.GET("/token", verifySvc.Verify, token)
  38. group.POST("/verify", verifySvc.Verify, verify)
  39. }
  40. func ping(c *bm.Context) {
  41. if svr.Ping(c) != nil {
  42. log.Error("captcha service ping error")
  43. c.AbortWithStatus(http.StatusServiceUnavailable)
  44. }
  45. }