http.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package http
  2. import (
  3. "net/http"
  4. "go-common/app/service/main/broadcast/service"
  5. "go-common/library/conf/paladin"
  6. "go-common/library/log"
  7. bm "go-common/library/net/http/blademaster"
  8. "go-common/library/net/http/blademaster/middleware/verify"
  9. )
  10. var (
  11. c paladin.Map
  12. srv *service.Service
  13. verifySvr *verify.Verify
  14. )
  15. // Init init.
  16. func Init(s *service.Service) {
  17. var hc struct {
  18. Server *bm.ServerConfig
  19. }
  20. if err := paladin.Get("http.toml").UnmarshalTOML(&hc); err != nil {
  21. if err != paladin.ErrNotExist {
  22. panic(err)
  23. }
  24. }
  25. if err := paladin.Watch("application.toml", &c); err != nil {
  26. panic(err)
  27. }
  28. srv = s
  29. verifySvr = verify.New(nil)
  30. engine := bm.DefaultServer(hc.Server)
  31. outerRouter(engine)
  32. interRouter(engine)
  33. if err := engine.Start(); err != nil {
  34. log.Error("xhttp.Serve2 error(%v)", err)
  35. panic(err)
  36. }
  37. }
  38. func outerRouter(e *bm.Engine) {
  39. g := e.Group("/x/broadcast")
  40. {
  41. g.POST("/conn/connect", connect)
  42. g.POST("/conn/disconnect", disconnect)
  43. g.POST("/conn/heartbeat", heartbeat)
  44. g.POST("/online/renew", renewOnline)
  45. }
  46. }
  47. func interRouter(e *bm.Engine) {
  48. e.Ping(ping)
  49. e.Register(register)
  50. g := e.Group("/x/internal/broadcast")
  51. {
  52. g.POST("/push/keys", verifySvr.Verify, pushKeys)
  53. g.POST("/push/mids", verifySvr.Verify, pushMids)
  54. g.POST("/push/room", verifySvr.Verify, pushRoom)
  55. g.POST("/push/all", verifySvr.Verify, pushAll)
  56. g.GET("/online/top", onlineTop)
  57. g.GET("/online/room", onlineRoom)
  58. g.GET("/online/total", onlineTotal)
  59. g.GET("/server/list", serverList)
  60. g.GET("/server/infos", serverInfos)
  61. g.GET("/server/weight", serverWeight)
  62. }
  63. }
  64. func ping(c *bm.Context) {
  65. if err := srv.Ping(c); err != nil {
  66. log.Error("broadcast-service ping error(%v)", err)
  67. c.AbortWithStatus(http.StatusServiceUnavailable)
  68. }
  69. }
  70. func register(c *bm.Context) {
  71. c.JSON(map[string]interface{}{}, nil)
  72. }