Skip to content

Commit

Permalink
Merge pull request #3 from lionhylra/yilei/improve-router
Browse files Browse the repository at this point in the history
Supporting creating router by http method and path
  • Loading branch information
swhitty authored Feb 15, 2022
2 parents 1e03f82 + d3ddcb2 commit 1a7b00a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
19 changes: 17 additions & 2 deletions Sources/HTTPRoute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,16 @@ public struct HTTPRoute {

public init(_ string: String) {
let comps = Self.components(for: string)
self.method = Component(comps.method)
self.path = comps.path
self.init(method: comps.method, path: comps.path)
}

public init(method: HTTPMethod, path: String) {
self.init(method: method.rawValue, path: path)
}

init(method: String, path: String) {
self.method = Component(method)
self.path = path
.split(separator: "/", omittingEmptySubsequences: true)
.map { Component(String($0)) }
}
Expand Down Expand Up @@ -119,3 +127,10 @@ public extension HTTPRoute {
}

}

extension HTTPRoute: ExpressibleByStringLiteral {

public init(stringLiteral value: String) {
self.init(value)
}
}
8 changes: 4 additions & 4 deletions Sources/HTTPServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public final actor HTTPServer {
self.handlers = []
}

public func appendHandler(for route: String, handler: HTTPHandler) {
handlers.append((HTTPRoute(route), handler))
public func appendHandler(for route: HTTPRoute, handler: HTTPHandler) {
handlers.append((route, handler))
}

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

public func start() async throws {
Expand Down

0 comments on commit 1a7b00a

Please sign in to comment.