Skip to content

Commit be2e0ce

Browse files
committed
CompositeHTTPHandler -> RouteHTTPHandler
1 parent 8d8eab8 commit be2e0ce

File tree

3 files changed

+54
-17
lines changed

3 files changed

+54
-17
lines changed

Sources/HTTPServer.swift

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public final actor HTTPServer {
3737
private let timeout: TimeInterval
3838
private var socket: AsyncSocket?
3939
private let logger: HTTPLogging?
40-
private var handlers: CompositeHTTPHandler
40+
private var handlers: RouteHTTPHandler
4141

4242
public init(port: UInt16,
4343
timeout: TimeInterval = 15,
@@ -59,12 +59,12 @@ public final actor HTTPServer {
5959
handler: ClosureHTTPHandler(handler))
6060
}
6161

62-
public func appendHandler(for route: HTTPRoute, handler: HTTPHandler) {
63-
handlers.appendHandler(for: route, handler: handler)
62+
public func appendRoute(_ route: HTTPRoute, to handler: HTTPHandler) {
63+
handlers.appendRoute(route, to: handler)
6464
}
6565

66-
public func appendHandler(for route: HTTPRoute, closure: @Sendable @escaping (HTTPRequest) async throws -> HTTPResponse) {
67-
handlers.appendHandler(for: route, closure: closure)
66+
public func appendRoute(_ route: HTTPRoute, handler: @Sendable @escaping (HTTPRequest) async throws -> HTTPResponse) {
67+
handlers.appendRoute(route, handler: handler)
6868
}
6969

7070
public func start() async throws {
@@ -150,10 +150,10 @@ public final actor HTTPServer {
150150
}
151151
}
152152

153-
private static func makeCompositeHandler(root: HTTPHandler?) -> CompositeHTTPHandler {
154-
var composite = CompositeHTTPHandler()
153+
private static func makeCompositeHandler(root: HTTPHandler?) -> RouteHTTPHandler {
154+
var composite = RouteHTTPHandler()
155155
if let handler = root {
156-
composite.appendHandler(for: "*", handler: handler)
156+
composite.appendRoute("*", to: handler)
157157
}
158158
return composite
159159
}
@@ -183,3 +183,18 @@ private extension HTTPConnection {
183183
"<\(hostname)>"
184184
}
185185
}
186+
187+
188+
public extension HTTPServer {
189+
190+
@available(*, deprecated, renamed: "appendRoute(_:to:)")
191+
func appendHandler(for route: HTTPRoute, handler: HTTPHandler) {
192+
appendRoute(route, to: handler)
193+
194+
}
195+
196+
@available(*, deprecated, renamed: "appendRoute(_:to:)")
197+
func appendHandler(for route: HTTPRoute, closure: @Sendable @escaping (HTTPRequest) async throws -> HTTPResponse) {
198+
appendRoute(route, handler: closure)
199+
}
200+
}

Sources/Handlers/CompositeHTTPHandler.swift renamed to Sources/Handlers/RouteHTTPHandler.swift

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,19 @@
2929
// SOFTWARE.
3030
//
3131

32-
public struct CompositeHTTPHandler: HTTPHandler, Sendable {
32+
public struct RouteHTTPHandler: HTTPHandler, Sendable {
3333

3434
private var handlers: [(route: HTTPRoute, handler: HTTPHandler)] = []
3535

3636
public init() { }
3737

38-
public mutating func appendHandler(for route: HTTPRoute, handler: HTTPHandler) {
38+
public mutating func appendRoute(_ route: HTTPRoute, to handler: HTTPHandler) {
3939
handlers.append((route, handler))
4040
}
4141

42-
public mutating func appendHandler(for route: HTTPRoute,
43-
closure: @Sendable @escaping (HTTPRequest) async throws -> HTTPResponse) {
44-
handlers.append((route, ClosureHTTPHandler(closure)))
42+
public mutating func appendRoute(_ route: HTTPRoute,
43+
handler: @Sendable @escaping (HTTPRequest) async throws -> HTTPResponse) {
44+
handlers.append((route, ClosureHTTPHandler(handler)))
4545
}
4646

4747
public func handleRequest(_ request: HTTPRequest) async throws -> HTTPResponse {
@@ -57,3 +57,25 @@ public struct CompositeHTTPHandler: HTTPHandler, Sendable {
5757
throw HTTPUnhandledError()
5858
}
5959
}
60+
61+
@available(*, deprecated, renamed: "RouteHTTPHandler")
62+
public typealias CompositeHTTPHandler = RouteHTTPHandler
63+
64+
65+
public extension RouteHTTPHandler {
66+
67+
@available(*, deprecated, renamed: "RouteHTTPHandler")
68+
69+
70+
@available(*, deprecated, renamed: "appendRoute(_:to:)")
71+
mutating func appendHandler(for route: HTTPRoute, handler: HTTPHandler) {
72+
appendRoute(route, to: handler)
73+
74+
}
75+
76+
@available(*, deprecated, renamed: "appendRoute(_:to:)")
77+
mutating func appendHandler(for route: HTTPRoute,
78+
closure: @Sendable @escaping (HTTPRequest) async throws -> HTTPResponse) {
79+
appendRoute(route, handler: closure)
80+
}
81+
}

Tests/HTTPServerTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ final class HTTPServerTests: XCTestCase {
4141
func testRequests_AreMatchedToHandlers_ViaRoute() async throws {
4242
let server = HTTPServer(port: 8008)
4343

44-
await server.appendHandler(for: "/accepted") { _ in
44+
await server.appendRoute("/accepted") { _ in
4545
HTTPResponse.make(statusCode: .accepted)
4646
}
47-
await server.appendHandler(for: "/gone") { _ in
47+
await server.appendRoute("/gone") { _ in
4848
HTTPResponse.make(statusCode: .gone)
4949
}
5050

@@ -116,7 +116,7 @@ final class HTTPServerTests: XCTestCase {
116116

117117
func testServer_ReturnsFile_WhenFileHandlerIsMatched() async throws {
118118
let server = HTTPServer(port: 8009)
119-
await server.appendHandler(for: "*", handler: .file(named: "fish.json", in: .module))
119+
await server.appendRoute("*", to: .file(named: "fish.json", in: .module))
120120
let task = Task { try await server.start() }
121121

122122
let request = URLRequest(url: URL(string: "http://localhost:8009")!)
@@ -132,7 +132,7 @@ final class HTTPServerTests: XCTestCase {
132132
#if canImport(Darwin)
133133
func testServer_Returns500_WhenHandlerTimesout() async throws {
134134
let server = HTTPServer(port: 8008, timeout: 0.1)
135-
await server.appendHandler(for: "*") { _ in
135+
await server.appendRoute("*") { _ in
136136
try await Task.sleep(nanoseconds: 5_000_000_000)
137137
return .make(statusCode: .ok)
138138
}

0 commit comments

Comments
 (0)