http.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package http
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "go-common/app/service/bbq/video/conf"
  6. "go-common/app/service/bbq/video/service"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. )
  11. var (
  12. srv *service.Service
  13. )
  14. // Init init
  15. func Init(c *conf.Config, s *service.Service) {
  16. srv = s
  17. engine := bm.DefaultServer(c.BM.Server)
  18. route(engine)
  19. if err := engine.Start(); err != nil {
  20. log.Error("bm Start error(%v)", err)
  21. panic(err)
  22. }
  23. }
  24. func route(e *bm.Engine) {
  25. e.Ping(ping)
  26. e.Register(register)
  27. g := e.Group("/bbq/internal")
  28. {
  29. g.POST("/sv/trans/back", bvcTransBack)
  30. g.GET("/sv/trans/commit", bvcTransCommit)
  31. g.POST("/sv/stat", videoStat)
  32. g.GET("/create/id", createID)
  33. g.GET("/sv/create/id", createID)
  34. // 增加视频播放量数据
  35. g.POST("/sv/views/add", videoViewsAdd)
  36. g.POST("/sv/limits/modify", limitsModify)
  37. g.POST("/sv/play", svPlays)
  38. }
  39. }
  40. func ping(c *bm.Context) {
  41. if err := srv.Ping(c); err != nil {
  42. log.Error("ping error(%v)", err)
  43. c.AbortWithStatus(http.StatusServiceUnavailable)
  44. }
  45. }
  46. func register(c *bm.Context) {
  47. c.JSON(map[string]interface{}{}, nil)
  48. }
  49. // bindJson 解析application/json请求body
  50. func bindJSON(c *bm.Context, obj interface{}) error {
  51. decoder := json.NewDecoder(c.Request.Body)
  52. err := decoder.Decode(obj)
  53. if err != nil {
  54. log.Warn("参数解析失败 %v", err)
  55. err = ecode.ReqParamErr
  56. return err
  57. }
  58. return nil
  59. }