Skip to content

Commit d3ddcb2

Browse files
author
Yilei He
committed
Supporting creating router by http method and path
1 parent 48f7b17 commit d3ddcb2

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
@@ -43,12 +43,12 @@ public final actor HTTPServer {
4343
self.handlers = []
4444
}
4545

46-
public func appendHandler(for route: String, handler: HTTPHandler) {
47-
handlers.append((HTTPRoute(route), handler))
46+
public func appendHandler(for route: HTTPRoute, handler: HTTPHandler) {
47+
handlers.append((route, handler))
4848
}
4949

50-
public func appendHandler(for route: String, closure: @escaping (HTTPRequest) async throws -> HTTPResponse) {
51-
handlers.append((HTTPRoute(route), ClosureHTTPHandler(closure)))
50+
public func appendHandler(for route: HTTPRoute, closure: @escaping (HTTPRequest) async throws -> HTTPResponse) {
51+
handlers.append((route, ClosureHTTPHandler(closure)))
5252
}
5353

5454
public func start() async throws {

0 commit comments

Comments
 (0)