|
| 1 | +package dotweb |
| 2 | + |
| 3 | +type ( |
| 4 | + Group interface { |
| 5 | + Use(m ...Middleware) Group |
| 6 | + Group(prefix string, m ...Middleware) Group |
| 7 | + DELETE(path string, h HttpHandle) RouterNode |
| 8 | + GET(path string, h HttpHandle) RouterNode |
| 9 | + HEAD(path string, h HttpHandle) RouterNode |
| 10 | + OPTIONS(path string, h HttpHandle) RouterNode |
| 11 | + PATCH(path string, h HttpHandle) RouterNode |
| 12 | + POST(path string, h HttpHandle) RouterNode |
| 13 | + PUT(path string, h HttpHandle) RouterNode |
| 14 | + } |
| 15 | + xGroup struct { |
| 16 | + prefix string |
| 17 | + middlewares []Middleware |
| 18 | + server *HttpServer |
| 19 | + } |
| 20 | +) |
| 21 | + |
| 22 | +func NewGroup(prefix string, server *HttpServer) Group { |
| 23 | + g := &xGroup{prefix: prefix, server: server} |
| 24 | + return g |
| 25 | +} |
| 26 | + |
| 27 | +// Use implements `Router#Use()` for sub-routes within the Group. |
| 28 | +func (g *xGroup) Use(m ...Middleware) Group { |
| 29 | + g.middlewares = append(g.middlewares, m...) |
| 30 | + return g |
| 31 | +} |
| 32 | + |
| 33 | +// DELETE implements `Router#DELETE()` for sub-routes within the Group. |
| 34 | +func (g *xGroup) DELETE(path string, h HttpHandle) RouterNode { |
| 35 | + return g.add(RouteMethod_DELETE, path, h) |
| 36 | +} |
| 37 | + |
| 38 | +// GET implements `Router#GET()` for sub-routes within the Group. |
| 39 | +func (g *xGroup) GET(path string, h HttpHandle) RouterNode { |
| 40 | + return g.add(RouteMethod_GET, path, h) |
| 41 | +} |
| 42 | + |
| 43 | +// HEAD implements `Router#HEAD()` for sub-routes within the Group. |
| 44 | +func (g *xGroup) HEAD(path string, h HttpHandle) RouterNode { |
| 45 | + return g.add(RouteMethod_HEAD, path, h) |
| 46 | +} |
| 47 | + |
| 48 | +// OPTIONS implements `Router#OPTIONS()` for sub-routes within the Group. |
| 49 | +func (g *xGroup) OPTIONS(path string, h HttpHandle) RouterNode { |
| 50 | + return g.add(RouteMethod_OPTIONS, path, h) |
| 51 | +} |
| 52 | + |
| 53 | +// PATCH implements `Router#PATCH()` for sub-routes within the Group. |
| 54 | +func (g *xGroup) PATCH(path string, h HttpHandle) RouterNode { |
| 55 | + return g.add(RouteMethod_PATCH, path, h) |
| 56 | +} |
| 57 | + |
| 58 | +// POST implements `Router#POST()` for sub-routes within the Group. |
| 59 | +func (g *xGroup) POST(path string, h HttpHandle) RouterNode { |
| 60 | + return g.add(RouteMethod_POST, path, h) |
| 61 | +} |
| 62 | + |
| 63 | +// PUT implements `Router#PUT()` for sub-routes within the Group. |
| 64 | +func (g *xGroup) PUT(path string, h HttpHandle) RouterNode { |
| 65 | + return g.add(RouteMethod_PUT, path, h) |
| 66 | +} |
| 67 | + |
| 68 | +// Group creates a new sub-group with prefix and optional sub-group-level middleware. |
| 69 | +func (g *xGroup) Group(prefix string, m ...Middleware) Group { |
| 70 | + return NewGroup(g.prefix+prefix, g.server).Use(g.middlewares...).Use(m...) |
| 71 | +} |
| 72 | + |
| 73 | +func (g *xGroup) add(method, path string, handler HttpHandle) RouterNode { |
| 74 | + return g.server.Router().RegisterRoute(method, g.prefix+path, handler).Use(g.middlewares...) |
| 75 | +} |
0 commit comments