route_register.go 2.4 KB

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