http.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package http
  2. import (
  3. "go-common/app/admin/main/block/conf"
  4. "go-common/app/admin/main/block/model"
  5. "go-common/app/admin/main/block/service"
  6. "go-common/library/ecode"
  7. "go-common/library/log"
  8. bm "go-common/library/net/http/blademaster"
  9. "go-common/library/net/http/blademaster/middleware/permit"
  10. "github.com/pkg/errors"
  11. )
  12. var (
  13. authSvc *permit.Permit
  14. svc *service.Service
  15. )
  16. // Init http server
  17. func Init() {
  18. initService()
  19. engine := bm.DefaultServer(conf.Conf.BM)
  20. innerRouter(engine)
  21. if err := engine.Start(); err != nil {
  22. log.Error("engine.Start() error(%v)", err)
  23. panic(err)
  24. }
  25. }
  26. // initService init biz services
  27. func initService() {
  28. authSvc = permit.New(conf.Conf.Auth)
  29. svc = service.New()
  30. }
  31. func innerRouter(e *bm.Engine) {
  32. e.GET("/monitor/ping", func(c *bm.Context) {})
  33. cb := e.Group("/x/admin/block", authSvc.Permit(conf.Conf.Perms.Perm["search"]))
  34. {
  35. cb.POST("/search", blockSearch)
  36. cb.GET("/history", blockHistory)
  37. }
  38. cb = e.Group("/x/admin/block", authSvc.Permit(conf.Conf.Perms.Perm["block"]))
  39. {
  40. cb.POST("", batchBlock)
  41. }
  42. cb = e.Group("/x/admin/block", authSvc.Permit(conf.Conf.Perms.Perm["remove"]))
  43. {
  44. cb.POST("/remove", batchRemove)
  45. }
  46. }
  47. func bind(c *bm.Context, v model.ParamValidator) (err error) {
  48. if err = c.Bind(v); err != nil {
  49. err = errors.WithStack(err)
  50. return
  51. }
  52. if !v.Validate() {
  53. err = ecode.RequestErr
  54. c.JSON(nil, ecode.RequestErr)
  55. return
  56. }
  57. return
  58. }