Skip to content

Commit 1a7b00a

Browse files
authored
Merge pull request #3 from lionhylra/yilei/improve-router
Supporting creating router by http method and path
2 parents 1e03f82 + d3ddcb2 commit 1a7b00a

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

Sources/HTTPRoute.swift

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,16 @@ public struct HTTPRoute {
3838

3939
public init(_ string: String) {
4040
let comps = Self.components(for: string)
41-
self.method = Component(comps.method)
42-
self.path = comps.path
41+
self.init(method: comps.method, path: comps.path)
42+
}
43+
44+
public init(method: HTTPMethod, path: String) {
45+
self.init(method: method.rawValue, path: path)
46+
}
47+
48+
init(method: String, path: String) {
49+
self.method = Component(method)
50+
self.path = path
4351
.split(separator: "/", omittingEmptySubsequences: true)
4452
.map { Component(String($0)) }
4553
}
@@ -119,3 +127,10 @@ public extension HTTPRoute {
119127
}
120128

121129
}
130+
131+
extension HTTPRoute: ExpressibleByStringLiteral {
132+
133+
public init(stringLiteral value: String) {
134+
self.init(value)
135+
}
136+
}

Sources/HTTPServer.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ public final actor HTTPServer {
4545
self.handlers = []
4646
}
4747

48-
public func appendHandler(for route: String, handler: HTTPHandler) {
49-
handlers.append((HTTPRoute(route), handler))
48+
public func appendHandler(for route: HTTPRoute, handler: HTTPHandler) {
49+
handlers.append((route, handler))
5050
}
5151

52-
public func appendHandler(for route: String, closure: @escaping (HTTPRequest) async throws -> HTTPResponse) {
53-
handlers.append((HTTPRoute(route), ClosureHTTPHandler(closure)))
52+
public func appendHandler(for route: HTTPRoute, closure: @escaping (HTTPRequest) async throws -> HTTPResponse) {
53+
handlers.append((route, ClosureHTTPHandler(closure)))
5454
}
5555

5656
public func start() async throws {

0 commit comments

Comments
 (0)