Micro & pluggable web framework for Go
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.5KB

  1. // Copyright 2015 The Tango Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package tango
  5. type groupRouter struct {
  6. methods interface{}
  7. url string
  8. c interface{}
  9. handlers []Handler
  10. }
  11. // Group defines a route group
  12. type Group struct {
  13. routers []groupRouter
  14. handlers []Handler
  15. }
  16. // NewGroup creates a route group
  17. func NewGroup() *Group {
  18. return &Group{
  19. routers: make([]groupRouter, 0),
  20. handlers: make([]Handler, 0),
  21. }
  22. }
  23. // Use set the middlewares to apply to this group's routes
  24. func (g *Group) Use(handlers ...Handler) {
  25. g.handlers = append(g.handlers, handlers...)
  26. }
  27. // Get addes a GET route to this group
  28. func (g *Group) Get(url string, c interface{}, middlewares ...Handler) {
  29. g.Route([]string{"GET", "HEAD:Get"}, url, c, middlewares...)
  30. }
  31. // Post addes a POST route to this group
  32. func (g *Group) Post(url string, c interface{}, middlewares ...Handler) {
  33. g.Route([]string{"POST"}, url, c, middlewares...)
  34. }
  35. // Head addes a HEAD route to this group
  36. func (g *Group) Head(url string, c interface{}, middlewares ...Handler) {
  37. g.Route([]string{"HEAD"}, url, c, middlewares...)
  38. }
  39. // Options addes a OPTIONS route to this group
  40. func (g *Group) Options(url string, c interface{}, middlewares ...Handler) {
  41. g.Route([]string{"OPTIONS"}, url, c, middlewares...)
  42. }
  43. // Trace addes a TRACE route to this group
  44. func (g *Group) Trace(url string, c interface{}, middlewares ...Handler) {
  45. g.Route([]string{"TRACE"}, url, c, middlewares...)
  46. }
  47. // Patch addes a PATCH route to this group
  48. func (g *Group) Patch(url string, c interface{}, middlewares ...Handler) {
  49. g.Route([]string{"PATCH"}, url, c, middlewares...)
  50. }
  51. // Delete addes a DELETE route to this group
  52. func (g *Group) Delete(url string, c interface{}, middlewares ...Handler) {
  53. g.Route([]string{"DELETE"}, url, c, middlewares...)
  54. }
  55. // Put addes a PUT route to this group
  56. func (g *Group) Put(url string, c interface{}, middlewares ...Handler) {
  57. g.Route([]string{"PUT"}, url, c, middlewares...)
  58. }
  59. // Any addes the default mehtods route to this group
  60. func (g *Group) Any(url string, c interface{}, middlewares ...Handler) {
  61. g.Route(SupportMethods, url, c, middlewares...)
  62. g.Route([]string{"HEAD:Get"}, url, c, middlewares...)
  63. }
  64. // Route defines a customerize route to this group
  65. func (g *Group) Route(methods interface{}, url string, c interface{}, middlewares ...Handler) {
  66. g.routers = append(g.routers, groupRouter{methods, url, c, middlewares})
  67. }
  68. // Group defines group's child group
  69. func (g *Group) Group(p string, o interface{}, handlers ...Handler) {
  70. gr := getGroup(o)
  71. if len(handlers) > 0 {
  72. gr.handlers = append(handlers, gr.handlers...)
  73. }
  74. for _, gchild := range gr.routers {
  75. g.Route(gchild.methods, joinRoute(p, gchild.url), gchild.c, append(gr.handlers, gchild.handlers...)...)
  76. }
  77. }
  78. func getGroup(o interface{}) *Group {
  79. var g *Group
  80. var gf func(*Group)
  81. var ok bool
  82. if g, ok = o.(*Group); ok {
  83. } else if gf, ok = o.(func(*Group)); ok {
  84. g = NewGroup()
  85. gf(g)
  86. } else {
  87. panic("not allowed group parameter")
  88. }
  89. return g
  90. }
  91. func joinRoute(p, url string) string {
  92. if len(p) == 0 || p == "/" {
  93. return url
  94. }
  95. return p + url
  96. }
  97. func (t *Tango) addGroup(p string, g *Group) {
  98. for _, r := range g.routers {
  99. t.Route(r.methods, joinRoute(p, r.url), r.c, append(g.handlers, r.handlers...)...)
  100. }
  101. }
  102. // Group adds routines groups
  103. func (t *Tango) Group(p string, o interface{}, handlers ...Handler) {
  104. g := getGroup(o)
  105. g.handlers = append(handlers, g.handlers...)
  106. t.addGroup(p, g)
  107. }