http.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package http
  2. import (
  3. "net/http"
  4. "go-common/app/admin/main/space/conf"
  5. "go-common/app/admin/main/space/service"
  6. "go-common/library/log"
  7. bm "go-common/library/net/http/blademaster"
  8. "go-common/library/net/http/blademaster/middleware/permit"
  9. )
  10. var (
  11. spcSvc *service.Service
  12. //idfSvc *identify.Identify
  13. permitSvc *permit.Permit
  14. )
  15. // Init init http sever instance.
  16. func Init(c *conf.Config, s *service.Service) {
  17. spcSvc = s
  18. permitSvc = permit.New(c.Permit)
  19. engine := bm.DefaultServer(c.BM)
  20. authRouter(engine)
  21. // init internal server
  22. if err := engine.Start(); err != nil {
  23. log.Error("engine.Start error(%v)", err)
  24. panic(err)
  25. }
  26. }
  27. func authRouter(e *bm.Engine) {
  28. e.Ping(ping)
  29. group := e.Group("/x/admin/space")
  30. {
  31. noticeGroup := group.Group("/notice", permitSvc.Permit("SPACE_NOTICE"))
  32. {
  33. noticeGroup.GET("", notice)
  34. noticeGroup.POST("/up", noticeUp)
  35. }
  36. group.GET("/relation", relation)
  37. blacklist := group.Group("/blacklist", permitSvc.Permit("SPACE_BLACKLIST"))
  38. {
  39. blacklist.GET("", blacklistIndex)
  40. blacklist.POST("add", blacklistAdd)
  41. blacklist.POST("update", blacklistUp)
  42. }
  43. }
  44. }
  45. func ping(c *bm.Context) {
  46. if err := spcSvc.Ping(c); err != nil {
  47. log.Error("space-admin ping error")
  48. c.AbortWithStatus(http.StatusServiceUnavailable)
  49. }
  50. }