http.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package http
  2. import (
  3. "net/http"
  4. "go-common/app/service/main/riot-search/conf"
  5. "go-common/app/service/main/riot-search/model"
  6. "go-common/app/service/main/riot-search/service"
  7. "go-common/library/log"
  8. bm "go-common/library/net/http/blademaster"
  9. "go-common/library/net/http/blademaster/middleware/verify"
  10. )
  11. var (
  12. srv *service.Service
  13. vfy *verify.Verify
  14. )
  15. // Init init
  16. func Init(c *conf.Config) {
  17. srv = service.New(c)
  18. vfy = verify.New(c.Verify)
  19. engine := bm.DefaultServer(c.BM)
  20. router(engine)
  21. if err := engine.Start(); err != nil {
  22. log.Error("xhttp.Serve error(%v)", err)
  23. panic(err)
  24. }
  25. }
  26. func router(e *bm.Engine) {
  27. e.Ping(ping)
  28. e.Register(register)
  29. g := e.Group("/x/internal/riot-search")
  30. {
  31. g.POST("/arc/ids", vfy.Verify, searchIDOnly)
  32. g.POST("/arc/contents", vfy.Verify, search)
  33. // debug api
  34. g.GET("/arc/has", has)
  35. }
  36. }
  37. func ping(c *bm.Context) {
  38. if err := srv.Ping(c); err != nil {
  39. log.Error("ping error(%v)", err)
  40. c.AbortWithStatus(http.StatusServiceUnavailable)
  41. }
  42. }
  43. func register(c *bm.Context) {
  44. c.JSON(map[string]interface{}{}, nil)
  45. }
  46. // @params RiotSearchReq
  47. // @router post /x/riot-search/arc/aids
  48. // @response IDsResp
  49. func searchIDOnly(c *bm.Context) {
  50. req := new(model.RiotSearchReq)
  51. if err := c.Bind(req); err != nil {
  52. log.Error("request param(%v) error", req)
  53. return
  54. }
  55. c.JSON(srv.SearchIDOnly(c, req), nil)
  56. }
  57. // @params RiotSearchReq
  58. // @router post /x/riot-search/arc/contents
  59. // @response DocumentsResp
  60. func search(c *bm.Context) {
  61. req := new(model.RiotSearchReq)
  62. if err := c.Bind(req); err != nil {
  63. log.Error("request param(%v) error", req)
  64. return
  65. }
  66. c.JSON(srv.Search(c, req), nil)
  67. }
  68. func has(c *bm.Context) {
  69. req := new(struct {
  70. ID uint64 `form:"id" validate:"min=0"`
  71. })
  72. if err := c.Bind(req); err != nil {
  73. return
  74. }
  75. c.JSON(srv.Has(c, req.ID), nil)
  76. }