/
RoutesBuilder+Group.swift
55 lines (49 loc) 路 1.82 KB
/
RoutesBuilder+Group.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
extension RoutesBuilder {
// MARK: Path
/// Creates a new `Router` that will automatically prepend the supplied path components.
///
/// let users = router.grouped("user")
/// // Adding "user/auth/" route to router.
/// users.get("auth") { ... }
/// // adding "user/profile/" route to router
/// users.get("profile") { ... }
///
/// - parameters:
/// - path: Group path components separated by commas.
/// - returns: Newly created `Router` wrapped in the path.
public func grouped(_ path: PathComponent...) -> RoutesBuilder {
return HTTPRoutesGroup(root: self, path: path)
}
/// Creates a new `Router` that will automatically prepend the supplied path components.
///
/// router.group("user") { users in
/// // Adding "user/auth/" route to router.
/// users.get("auth") { ... }
/// // adding "user/profile/" route to router
/// users.get("profile") { ... }
/// }
///
/// - parameters:
/// - path: Group path components separated by commas.
/// - configure: Closure to configure the newly created `Router`.
public func group(_ path: PathComponent..., configure: (RoutesBuilder) throws -> ()) rethrows {
try configure(HTTPRoutesGroup(root: self, path: path))
}
}
/// Groups routes
private final class HTTPRoutesGroup: RoutesBuilder {
/// Router to cascade to.
let root: RoutesBuilder
/// Additional components.
let path: [PathComponent]
/// Creates a new `PathGroup`.
init(root: RoutesBuilder, path: [PathComponent]) {
self.root = root
self.path = path
}
/// See `HTTPRoutesBuilder`.
func add(_ route: Route) {
route.path = self.path + route.path
self.root.add(route)
}
}