http.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package http
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "strconv"
  8. "go-common/app/service/main/antispam/conf"
  9. "go-common/app/service/main/antispam/service"
  10. "go-common/library/log"
  11. bm "go-common/library/net/http/blademaster"
  12. "go-common/library/net/http/blademaster/middleware/auth"
  13. "go-common/library/net/http/blademaster/middleware/verify"
  14. )
  15. var (
  16. // Svr .
  17. Svr service.Service
  18. verifySvc *verify.Verify
  19. authSvc *auth.Auth
  20. )
  21. // Init .
  22. func Init(c *conf.Config, s service.Service) {
  23. Svr = s
  24. verifySvc = verify.New(c.Verify)
  25. authSvc = auth.New(c.Auth)
  26. engine := bm.DefaultServer(c.BM)
  27. interRouter(engine)
  28. if err := engine.Start(); err != nil {
  29. log.Error("engine.Start() error(%v)", err)
  30. panic(err)
  31. }
  32. }
  33. func interRouter(e *bm.Engine) {
  34. e.GET("/monitor/ping", ping)
  35. e.GET("/register", register)
  36. e.GET("/x/internal/antispam/filter", authSvc.Guest, Filter)
  37. regexps := e.Group("/x/internal/antispam/regexps")
  38. regexps.GET("", verifySvc.Verify, GetRegexps)
  39. regexps.GET("/one", verifySvc.Verify, GetRegexp)
  40. regexps.POST("/add", verifySvc.Verify, AddRegexp)
  41. regexps.POST("/edit", verifySvc.Verify, EditRegexp)
  42. regexps.POST("/del", verifySvc.Verify, DeleteRegexp)
  43. regexps.POST("/recover", verifySvc.Verify, RecoverRegexp)
  44. rules := e.Group("/x/internal/antispam/rules")
  45. rules.GET("", verifySvc.Verify, GetRules)
  46. rules.GET("/one", verifySvc.Verify, GetRule)
  47. rules.POST("/add", verifySvc.Verify, AddRule)
  48. keywords := e.Group("/x/internal/antispam/keywords")
  49. keywords.GET("", verifySvc.Verify, GetKeywords)
  50. keywords.GET("/senders", verifySvc.Verify, GetKeywordSenders)
  51. keywords.GET("/one", verifySvc.Verify, GetKeyword)
  52. keywords.POST("/dels", verifySvc.Verify, DeleteKeywords)
  53. keywords.POST("/action", verifySvc.Verify, UpdateKeyword)
  54. }
  55. func ping(c *bm.Context) {
  56. if err := Svr.Ping(c); err != nil {
  57. log.Error("antispam service ping error(%v)", err)
  58. c.AbortWithStatus(http.StatusServiceUnavailable)
  59. }
  60. }
  61. func register(c *bm.Context) {
  62. c.JSON(struct{}{}, nil)
  63. }
  64. func getAdminIDAndArea(params url.Values) (adminID int64, area string, err error) {
  65. adminID, err = getAdminID(params)
  66. if err != nil {
  67. return 0, "", err
  68. }
  69. area, err = parseArea(params)
  70. if err != nil {
  71. return 0, "", err
  72. }
  73. return adminID, area, nil
  74. }
  75. func parseArea(params url.Values) (string, error) {
  76. area := params.Get(ProtocolArea)
  77. if _, ok := conf.Areas[area]; !ok {
  78. err := fmt.Errorf("invalid area(%s)", area)
  79. log.Error("%v", err)
  80. return "", err
  81. }
  82. return area, nil
  83. }
  84. func getAdminID(params url.Values) (int64, error) {
  85. adminIDStr := params.Get(ProtocolAdminID)
  86. if adminIDStr == "" {
  87. err := errors.New("empty admin id")
  88. log.Error("%v", err)
  89. return 0, err
  90. }
  91. adminID, err := strconv.ParseInt(adminIDStr, 10, 64)
  92. if err != nil {
  93. log.Error("%v", err)
  94. return 0, err
  95. }
  96. return adminID, nil
  97. }
  98. func errResp(c *bm.Context, code interface{}, err error) {
  99. c.JSONMap(map[string]interface{}{
  100. ProtocolData: code,
  101. ProtocolMessage: err.Error(),
  102. }, nil)
  103. }