route_register.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package api
  2. import (
  3. "net/http"
  4. "github.com/grafana/grafana/pkg/middleware"
  5. macaron "gopkg.in/macaron.v1"
  6. )
  7. type Router interface {
  8. Handle(method, pattern string, handlers []macaron.Handler) *macaron.Route
  9. }
  10. type RouteRegister interface {
  11. Get(string, ...macaron.Handler)
  12. Post(string, ...macaron.Handler)
  13. Delete(string, ...macaron.Handler)
  14. Put(string, ...macaron.Handler)
  15. Patch(string, ...macaron.Handler)
  16. Any(string, ...macaron.Handler)
  17. Group(string, func(RouteRegister), ...macaron.Handler)
  18. Register(Router) *macaron.Router
  19. }
  20. func newRouteRegister() RouteRegister {
  21. return &routeRegister{
  22. prefix: "",
  23. routes: []route{},
  24. subfixHandlers: []macaron.Handler{},
  25. }
  26. }
  27. type route struct {
  28. method string
  29. pattern string
  30. handlers []macaron.Handler
  31. }
  32. type routeRegister struct {
  33. prefix string
  34. subfixHandlers []macaron.Handler
  35. routes []route
  36. groups []*routeRegister
  37. }
  38. func (rr *routeRegister) Group(pattern string, fn func(rr RouteRegister), handlers ...macaron.Handler) {
  39. group := &routeRegister{
  40. prefix: rr.prefix + pattern,
  41. subfixHandlers: append(rr.subfixHandlers, handlers...),
  42. routes: []route{},
  43. }
  44. fn(group)
  45. rr.groups = append(rr.groups, group)
  46. }
  47. func (rr *routeRegister) Register(router Router) *macaron.Router {
  48. for _, r := range rr.routes {
  49. router.Handle(r.method, r.pattern, r.handlers)
  50. }
  51. for _, g := range rr.groups {
  52. g.Register(router)
  53. }
  54. return &macaron.Router{}
  55. }
  56. func (rr *routeRegister) route(pattern, method string, handlers ...macaron.Handler) {
  57. //inject tracing
  58. h := append(rr.subfixHandlers, handlers...)
  59. h = append([]macaron.Handler{middleware.RequestMetrics(pattern)}, h...)
  60. rr.routes = append(rr.routes, route{
  61. method: method,
  62. pattern: rr.prefix + pattern,
  63. handlers: h,
  64. })
  65. }
  66. func (rr *routeRegister) Get(pattern string, handlers ...macaron.Handler) {
  67. rr.route(pattern, http.MethodGet, handlers...)
  68. }
  69. func (rr *routeRegister) Post(pattern string, handlers ...macaron.Handler) {
  70. rr.route(pattern, http.MethodPost, handlers...)
  71. }
  72. func (rr *routeRegister) Delete(pattern string, handlers ...macaron.Handler) {
  73. rr.route(pattern, http.MethodDelete, handlers...)
  74. }
  75. func (rr *routeRegister) Put(pattern string, handlers ...macaron.Handler) {
  76. rr.route(pattern, http.MethodPut, handlers...)
  77. }
  78. func (rr *routeRegister) Patch(pattern string, handlers ...macaron.Handler) {
  79. rr.route(pattern, http.MethodPatch, handlers...)
  80. }
  81. func (rr *routeRegister) Any(pattern string, handlers ...macaron.Handler) {
  82. rr.route(pattern, "*", handlers...)
  83. }