barometer: update DMA's vendoring packages
[barometer.git] / src / dma / vendor / github.com / labstack / echo / group.go
1 package echo
2
3 import (
4         "net/http"
5         "path"
6 )
7
8 type (
9         // Group is a set of sub-routes for a specified route. It can be used for inner
10         // routes that share a common middleware or functionality that should be separate
11         // from the parent echo instance while still inheriting from it.
12         Group struct {
13                 prefix     string
14                 middleware []MiddlewareFunc
15                 echo       *Echo
16         }
17 )
18
19 // Use implements `Echo#Use()` for sub-routes within the Group.
20 func (g *Group) Use(middleware ...MiddlewareFunc) {
21         g.middleware = append(g.middleware, middleware...)
22         // Allow all requests to reach the group as they might get dropped if router
23         // doesn't find a match, making none of the group middleware process.
24         for _, p := range []string{"", "/*"} {
25                 g.echo.Any(path.Clean(g.prefix+p), func(c Context) error {
26                         return NotFoundHandler(c)
27                 }, g.middleware...)
28         }
29 }
30
31 // CONNECT implements `Echo#CONNECT()` for sub-routes within the Group.
32 func (g *Group) CONNECT(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
33         return g.Add(http.MethodConnect, path, h, m...)
34 }
35
36 // DELETE implements `Echo#DELETE()` for sub-routes within the Group.
37 func (g *Group) DELETE(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
38         return g.Add(http.MethodDelete, path, h, m...)
39 }
40
41 // GET implements `Echo#GET()` for sub-routes within the Group.
42 func (g *Group) GET(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
43         return g.Add(http.MethodGet, path, h, m...)
44 }
45
46 // HEAD implements `Echo#HEAD()` for sub-routes within the Group.
47 func (g *Group) HEAD(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
48         return g.Add(http.MethodHead, path, h, m...)
49 }
50
51 // OPTIONS implements `Echo#OPTIONS()` for sub-routes within the Group.
52 func (g *Group) OPTIONS(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
53         return g.Add(http.MethodOptions, path, h, m...)
54 }
55
56 // PATCH implements `Echo#PATCH()` for sub-routes within the Group.
57 func (g *Group) PATCH(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
58         return g.Add(http.MethodPatch, path, h, m...)
59 }
60
61 // POST implements `Echo#POST()` for sub-routes within the Group.
62 func (g *Group) POST(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
63         return g.Add(http.MethodPost, path, h, m...)
64 }
65
66 // PUT implements `Echo#PUT()` for sub-routes within the Group.
67 func (g *Group) PUT(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
68         return g.Add(http.MethodPut, path, h, m...)
69 }
70
71 // TRACE implements `Echo#TRACE()` for sub-routes within the Group.
72 func (g *Group) TRACE(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
73         return g.Add(http.MethodTrace, path, h, m...)
74 }
75
76 // Any implements `Echo#Any()` for sub-routes within the Group.
77 func (g *Group) Any(path string, handler HandlerFunc, middleware ...MiddlewareFunc) []*Route {
78         routes := make([]*Route, len(methods))
79         for i, m := range methods {
80                 routes[i] = g.Add(m, path, handler, middleware...)
81         }
82         return routes
83 }
84
85 // Match implements `Echo#Match()` for sub-routes within the Group.
86 func (g *Group) Match(methods []string, path string, handler HandlerFunc, middleware ...MiddlewareFunc) []*Route {
87         routes := make([]*Route, len(methods))
88         for i, m := range methods {
89                 routes[i] = g.Add(m, path, handler, middleware...)
90         }
91         return routes
92 }
93
94 // Group creates a new sub-group with prefix and optional sub-group-level middleware.
95 func (g *Group) Group(prefix string, middleware ...MiddlewareFunc) *Group {
96         m := make([]MiddlewareFunc, 0, len(g.middleware)+len(middleware))
97         m = append(m, g.middleware...)
98         m = append(m, middleware...)
99         return g.echo.Group(g.prefix+prefix, m...)
100 }
101
102 // Static implements `Echo#Static()` for sub-routes within the Group.
103 func (g *Group) Static(prefix, root string) {
104         static(g, prefix, root)
105 }
106
107 // File implements `Echo#File()` for sub-routes within the Group.
108 func (g *Group) File(path, file string) {
109         g.echo.File(g.prefix+path, file)
110 }
111
112 // Add implements `Echo#Add()` for sub-routes within the Group.
113 func (g *Group) Add(method, path string, handler HandlerFunc, middleware ...MiddlewareFunc) *Route {
114         // Combine into a new slice to avoid accidentally passing the same slice for
115         // multiple routes, which would lead to later add() calls overwriting the
116         // middleware from earlier calls.
117         m := make([]MiddlewareFunc, 0, len(g.middleware)+len(middleware))
118         m = append(m, g.middleware...)
119         m = append(m, middleware...)
120         return g.echo.Add(method, g.prefix+path, handler, m...)
121 }