http.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package http
  2. import (
  3. "net/http"
  4. "go-common/app/service/main/coin/conf"
  5. "go-common/app/service/main/coin/service"
  6. "go-common/library/log"
  7. bm "go-common/library/net/http/blademaster"
  8. "go-common/library/net/http/blademaster/middleware/antispam"
  9. "go-common/library/net/http/blademaster/middleware/verify"
  10. )
  11. var (
  12. verifySrv *verify.Verify
  13. coinSvc *service.Service
  14. antispamM *antispam.Antispam
  15. )
  16. // Init init http linstening.
  17. func Init(c *conf.Config, s *service.Service) {
  18. coinSvc = s
  19. antispamM = antispam.New(c.Antispam)
  20. verifySrv = verify.New(c.Verify)
  21. // init outer router
  22. engine := bm.DefaultServer(c.BM)
  23. outerRouter(engine)
  24. if err := engine.Start(); err != nil {
  25. log.Error("engine.Start error(%v)", err)
  26. panic(err)
  27. }
  28. }
  29. // innerRouter init inner router.
  30. func outerRouter(r *bm.Engine) {
  31. r.Ping(ping)
  32. r.Register(register)
  33. cr := r.Group("/x/coin", verifySrv.Verify, Business)
  34. {
  35. cr.POST("/add", antispamM.ServeHTTP, addCoin)
  36. cr.POST("/settle", updateSettle)
  37. cr.GET("/v2/list", list)
  38. cr.GET("/today/exp", todayexp)
  39. }
  40. cr1 := r.Group("/x/internal/v1/coin", verifySrv.Verify, Business)
  41. {
  42. cr1.POST("/add", antispamM.ServeHTTP, internalAddCoin)
  43. cr1.GET("/list", list)
  44. cr1.GET("/coins", coins)
  45. cr1.GET("/item/coins", itemCoins)
  46. cr1.POST("/amend", amend)
  47. cr1.GET("/user/count", userCoins)
  48. cr1.GET("/user/log", coinLog)
  49. cr1.POST("/user/modify", modify)
  50. cr1.GET("/creation/counts", ccounts)
  51. }
  52. }
  53. func ping(c *bm.Context) {
  54. if err := coinSvc.Ping(c); err != nil {
  55. log.Error("ping error(%v)", err)
  56. c.AbortWithStatus(http.StatusServiceUnavailable)
  57. }
  58. }
  59. func register(c *bm.Context) {
  60. c.JSON(map[string]interface{}{}, nil)
  61. }
  62. // Business set business
  63. func Business(c *bm.Context) {
  64. business := c.Request.Form.Get("business")
  65. if business == "" {
  66. return
  67. }
  68. tp, err := coinSvc.CheckBusiness(business)
  69. if err != nil {
  70. c.JSON(nil, err)
  71. c.Abort()
  72. return
  73. }
  74. c.Set("business", tp)
  75. }