http.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package http
  2. import (
  3. "net/http"
  4. "strconv"
  5. "go-common/app/interface/main/push-archive/conf"
  6. "go-common/app/interface/main/push-archive/model"
  7. "go-common/app/interface/main/push-archive/service"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/http/blademaster/middleware/antispam"
  11. "go-common/library/net/http/blademaster/middleware/auth"
  12. "go-common/library/net/http/blademaster/middleware/verify"
  13. )
  14. var (
  15. pushSrv *service.Service
  16. authSrv *auth.Auth
  17. veriSrv *verify.Verify
  18. anti *antispam.Antispam
  19. )
  20. // Init init http.
  21. func Init(c *conf.Config, srv *service.Service) {
  22. pushSrv = srv
  23. eng := bm.DefaultServer(c.Bm)
  24. authSrv = auth.New(c.Auth)
  25. veriSrv = verify.New(c.Verify)
  26. anti = antispam.New(c.Anti)
  27. addRoutes(eng)
  28. if err := eng.Start(); err != nil {
  29. log.Error("eng.Start error(%v)", err)
  30. panic(err)
  31. }
  32. }
  33. func addRoutes(e *bm.Engine) {
  34. e.Ping(ping)
  35. // TODO delete, for test
  36. e.GET("/x/push-archive/test", func(ctx *bm.Context) {
  37. aidStr := ctx.Request.Form.Get("aid")
  38. aid, _ := strconv.ParseInt(aidStr, 10, 64)
  39. midStr := ctx.Request.Form.Get("mid")
  40. mid, _ := strconv.ParseInt(midStr, 10, 64)
  41. pushSrv.Test(&model.Archive{
  42. ID: aid,
  43. Mid: mid,
  44. })
  45. }) // TODO delete, for test
  46. set := e.Group("/x/push-archive/setting", veriSrv.Verify, authSrv.UserMobile)
  47. {
  48. set.GET("/get", anti.ServeHTTP, setting)
  49. set.POST("/set", setSetting)
  50. }
  51. }
  52. func ping(c *bm.Context) {
  53. if err := pushSrv.Ping(c); err != nil {
  54. log.Error("push-archive ping error(%v)", err)
  55. c.AbortWithStatus(http.StatusServiceUnavailable)
  56. }
  57. }