http.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package http
  2. import (
  3. "net/http"
  4. "strconv"
  5. "go-common/app/job/main/coin/conf"
  6. "go-common/app/job/main/coin/service"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. )
  11. var coinSvr *service.Service
  12. // Init init http router.
  13. func Init(c *conf.Config, s *service.Service) {
  14. // init internal router
  15. coinSvr = s
  16. // init outer router
  17. engineOuter := bm.DefaultServer(c.BM)
  18. outerRouter(engineOuter)
  19. if err := engineOuter.Start(); err != nil {
  20. log.Error("bm.DefaultServer error(%v)", err)
  21. panic(err)
  22. }
  23. }
  24. func outerRouter(r *bm.Engine) {
  25. r.Ping(ping)
  26. r.Register(register)
  27. r.POST("/redo", redo)
  28. r.POST("/settle", settle)
  29. }
  30. // ping check server ok.
  31. func ping(c *bm.Context) {
  32. if err := coinSvr.Ping(c); err != nil {
  33. log.Error("ping error(%v)", err)
  34. c.AbortWithStatus(http.StatusServiceUnavailable)
  35. }
  36. }
  37. func redo(c *bm.Context) {
  38. idStr := c.Request.Form.Get("table_id")
  39. id, err := strconv.ParseInt(idStr, 10, 64)
  40. if err != nil {
  41. c.JSON(nil, ecode.RequestErr)
  42. return
  43. }
  44. c.JSON(nil, coinSvr.Redo(id))
  45. }
  46. func settle(c *bm.Context) {
  47. idStr := c.Request.Form.Get("table_id")
  48. id, err := strconv.ParseInt(idStr, 10, 64)
  49. if err != nil {
  50. c.JSON(nil, ecode.RequestErr)
  51. return
  52. }
  53. c.JSON(nil, coinSvr.Settle(id))
  54. }
  55. func register(c *bm.Context) {
  56. c.JSON(map[string]interface{}{}, nil)
  57. }