http.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package http
  2. import (
  3. "net/http"
  4. "go-common/app/job/main/coupon/conf"
  5. "go-common/app/job/main/coupon/model"
  6. "go-common/app/job/main/coupon/service"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. )
  11. var (
  12. svc *service.Service
  13. )
  14. // Init init
  15. func Init(c *conf.Config, s *service.Service) {
  16. svc = s
  17. // init router
  18. engineInner := bm.DefaultServer(c.BM)
  19. innerRouter(engineInner)
  20. if err := engineInner.Start(); err != nil {
  21. log.Error("bm.DefaultServer error(%v)", err)
  22. panic(err)
  23. }
  24. }
  25. // innerRouter init inner router api path.
  26. func innerRouter(e *bm.Engine) {
  27. //init api
  28. e.GET("/monitor/ping", ping)
  29. e.GET("/deliver/success", cartoonDeliverSuccess)
  30. }
  31. // ping check server ok.
  32. func ping(c *bm.Context) {
  33. if err := svc.Ping(c); err != nil {
  34. log.Error("coupon http service ping error(%v)", err)
  35. c.AbortWithStatus(http.StatusServiceUnavailable)
  36. }
  37. }
  38. func cartoonDeliverSuccess(c *bm.Context) {
  39. var (
  40. err error
  41. o *model.CouponOrder
  42. )
  43. arg := new(struct {
  44. OrderNo string `form:"order_no"`
  45. State int8 `form:"state"`
  46. Ver int64 `form:"ver"`
  47. IsPaid int8 `form:"is_paid"`
  48. })
  49. if err = c.Bind(arg); err != nil {
  50. log.Error("add coupon page %+v", err)
  51. return
  52. }
  53. if o, err = svc.ByOrderNo(c, arg.OrderNo); err != nil {
  54. c.JSON(nil, err)
  55. return
  56. }
  57. if o == nil {
  58. c.JSON(nil, ecode.RequestErr)
  59. return
  60. }
  61. if err = svc.UpdateOrderState(c, o, arg.State, &model.CallBackRet{Ver: arg.Ver, IsPaid: arg.IsPaid}); err != nil {
  62. log.Error("svc.UpdateOrderState(%s) %+v", arg.OrderNo, err)
  63. c.JSON(nil, err)
  64. return
  65. }
  66. c.JSON(nil, nil)
  67. }