/
RoutesBuilder+Method.swift
180 lines (163 loc) · 5.43 KB
/
RoutesBuilder+Method.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/// Determines how an incoming HTTP request's body is collected.
public enum HTTPBodyStreamStrategy {
/// The HTTP request's body will be collected into memory before the route handler is
/// called. The max size will determine how much data can be collected. The default is
/// 1,000,000 bytes, or 1 megabyte.
///
/// See `collect(maxSize:)` to set a lower max body size.
public static var collect: HTTPBodyStreamStrategy {
return .collect(maxSize: 1_000_000)
}
/// The HTTP request's body will not be collected first before the route handler is called
/// and will arrive in zero or more chunks.
case stream
/// The HTTP request's body will be collected into memory before the route handler is
/// called.
///
/// If a `maxSize` is supplied, the request body size in bytes will be limited. Requests
/// exceeding that size will result in an error.
case collect(maxSize: Int?)
}
extension RoutesBuilder {
@discardableResult
public func get<Response>(
_ path: PathComponent...,
use closure: @escaping (Request) throws -> Response
) -> Route
where Response: ResponseEncodable
{
return self.on(.GET, path, use: closure)
}
@discardableResult
public func get<Response>(
_ path: [PathComponent],
use closure: @escaping (Request) throws -> Response
) -> Route
where Response: ResponseEncodable
{
return self.on(.GET, path, use: closure)
}
@discardableResult
public func post<Response>(
_ path: PathComponent...,
use closure: @escaping (Request) throws -> Response
) -> Route
where Response: ResponseEncodable
{
return self.on(.POST, path, use: closure)
}
@discardableResult
public func post<Response>(
_ path: [PathComponent],
use closure: @escaping (Request) throws -> Response
) -> Route
where Response: ResponseEncodable
{
return self.on(.POST, path, use: closure)
}
@discardableResult
public func patch<Response>(
_ path: PathComponent...,
use closure: @escaping (Request) throws -> Response
) -> Route
where Response: ResponseEncodable
{
return self.on(.PATCH, path, use: closure)
}
@discardableResult
public func patch<Response>(
_ path: [PathComponent],
use closure: @escaping (Request) throws -> Response
) -> Route
where Response: ResponseEncodable
{
return self.on(.PATCH, path, use: closure)
}
@discardableResult
public func put<Response>(
_ path: PathComponent...,
use closure: @escaping (Request) throws -> Response
) -> Route
where Response: ResponseEncodable
{
return self.on(.PUT, path, use: closure)
}
@discardableResult
public func put<Response>(
_ path: [PathComponent],
use closure: @escaping (Request) throws -> Response
) -> Route
where Response: ResponseEncodable
{
return self.on(.PUT, path, use: closure)
}
@discardableResult
public func delete<Response>(
_ path: PathComponent...,
use closure: @escaping (Request) throws -> Response
) -> Route
where Response: ResponseEncodable
{
return self.on(.DELETE, path, use: closure)
}
@discardableResult
public func delete<Response>(
_ path: [PathComponent],
use closure: @escaping (Request) throws -> Response
) -> Route
where Response: ResponseEncodable
{
return self.on(.DELETE, path, use: closure)
}
@discardableResult
public func on<Response>(
_ method: HTTPMethod,
_ path: PathComponent...,
body: HTTPBodyStreamStrategy? = nil,
use closure: @escaping (Request) throws -> Response
) -> Route
where Response: ResponseEncodable
{
return self.on(method, path, body: body, use: { request in
return try closure(request)
})
}
@discardableResult
public func on<Response>(
_ method: HTTPMethod,
_ path: [PathComponent],
body: HTTPBodyStreamStrategy? = nil,
use closure: @escaping (Request) throws -> Response
) -> Route
where Response: ResponseEncodable
{
let streamStrategy = body ?? .collect(maxSize: self.defaultMaxBodySize)
let responder = BasicResponder { request in
switch streamStrategy {
case let .collect(maxSize):
let collected: EventLoopFuture<Void>
if let data = request.body.data {
collected = request.eventLoop.tryFuture {
if let max = maxSize, data.readableBytes > max { throw Abort(.payloadTooLarge) }
}
} else {
collected = request.body.collect(max: maxSize).transform(to: ())
}
return collected.flatMapThrowing {
return try closure(request)
}.encodeResponse(for: request)
case .stream:
return try closure(request).encodeResponse(for: request)
}
}
let route = Route(
method: method,
path: path,
responder: responder,
requestType: Request.self,
responseType: Response.self
)
self.add(route)
return route
}
}