http.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package http
  2. import (
  3. "go-common/app/interface/bbq/wechat/internal/conf"
  4. "go-common/app/interface/bbq/wechat/internal/model"
  5. "go-common/app/interface/bbq/wechat/internal/service"
  6. "go-common/library/log"
  7. bm "go-common/library/net/http/blademaster"
  8. "net/http"
  9. "github.com/pkg/errors"
  10. )
  11. var (
  12. svc *service.Service
  13. )
  14. // Init init
  15. func Init(c *conf.Config, s *service.Service) {
  16. svc = service.New(c)
  17. engine := bm.DefaultServer(c.BM)
  18. route(engine)
  19. if err := engine.Start(); err != nil {
  20. log.Error("bm Start error(%v)", err)
  21. panic(err)
  22. }
  23. }
  24. func ping(c *bm.Context) {
  25. if err := svc.Ping(c); err != nil {
  26. log.Error("ping error(%v)", err)
  27. c.AbortWithStatus(http.StatusServiceUnavailable)
  28. }
  29. }
  30. func register(c *bm.Context) {
  31. c.JSON(map[string]interface{}{}, nil)
  32. }
  33. func route(e *bm.Engine) {
  34. e.Ping(ping)
  35. e.Register(register)
  36. g := e.Group("/bbq/wechat")
  37. {
  38. g.GET("/token", getToken)
  39. }
  40. }
  41. func getToken(c *bm.Context) {
  42. arg := new(model.TokenReq)
  43. if err := c.Bind(arg); err != nil {
  44. errors.Wrap(err, "参数验证失败")
  45. return
  46. }
  47. url := c.Request.Referer()
  48. c.JSON(svc.TokenGet(c, arg, url))
  49. }