http.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package http
  2. import (
  3. "go-common/app/interface/main/web-show/conf"
  4. "go-common/app/interface/main/web-show/service/job"
  5. "go-common/app/interface/main/web-show/service/operation"
  6. res "go-common/app/interface/main/web-show/service/resource"
  7. "go-common/library/log"
  8. bm "go-common/library/net/http/blademaster"
  9. "go-common/library/net/http/blademaster/middleware/auth"
  10. "go-common/library/net/http/blademaster/middleware/cache"
  11. "go-common/library/net/http/blademaster/middleware/cache/store"
  12. "go-common/library/net/http/blademaster/middleware/verify"
  13. )
  14. var (
  15. jobSvc *job.Service
  16. opSvc *operation.Service
  17. vfySvr *verify.Verify
  18. authSvr *auth.Auth
  19. resSvc *res.Service
  20. // cache components
  21. cacheSvr *cache.Cache
  22. deg *cache.Degrader
  23. )
  24. // Init int http service
  25. func Init(c *conf.Config) {
  26. initService(c)
  27. // init external router
  28. cacheSvr = cache.New(store.NewMemcache(c.DegradeConfig.Memcache))
  29. deg = cache.NewDegrader(c.DegradeConfig.Expire)
  30. engineOut := bm.DefaultServer(c.BM.Outer)
  31. outerRouter(engineOut)
  32. // init Outer serve
  33. if err := engineOut.Start(); err != nil {
  34. log.Error("engineOut.Start error(%v)", err)
  35. panic(err)
  36. }
  37. engineLocal := bm.DefaultServer(c.BM.Local)
  38. localRouter(engineLocal)
  39. // init Outlocaler serve
  40. if err := engineLocal.Start(); err != nil {
  41. log.Error("engineLocal.Start error(%v)", err)
  42. panic(err)
  43. }
  44. }
  45. // initService init service
  46. func initService(c *conf.Config) {
  47. authSvr = auth.New(c.Auth)
  48. vfySvr = verify.New(c.Verify)
  49. jobSvc = job.New(c)
  50. opSvc = operation.New(c)
  51. resSvc = res.New(c)
  52. }
  53. //CloseService close all service
  54. func CloseService() {
  55. jobSvc.Close()
  56. opSvc.Close()
  57. }
  58. // outerRouter init outer router api path.
  59. func outerRouter(e *bm.Engine) {
  60. // init api
  61. e.Ping(ping)
  62. group := e.Group("/x/web-show", bm.CORS())
  63. {
  64. group.GET("/join", join)
  65. group.GET("/notice", notice)
  66. group.GET("/promote", promote)
  67. group.GET("/res/loc", authSvr.Guest, cacheSvr.Cache(deg.Args("id", "pf"), nil), resource)
  68. group.GET("/res/locs", authSvr.Guest, cacheSvr.Cache(deg.Args("ids", "pf"), nil), resources)
  69. group.GET("/ad/video", authSvr.Guest, advideo)
  70. group.GET("/archive/relation", relation)
  71. group.GET("/urls", vfySvr.Verify, urlMonitor)
  72. }
  73. e.GET("/x/ad/video", authSvr.Guest, advideo)
  74. }
  75. // innerRouter init local router api path.
  76. func localRouter(e *bm.Engine) {
  77. group := e.Group("/x/web-show", bm.CORS())
  78. {
  79. group.GET("/monitor/ping", ping)
  80. group.GET("/version", version)
  81. group.GET("/gray", grayRate)
  82. }
  83. }