Skip to content

Instantly share code, notes, and snippets.

@paulofaria
Created January 14, 2016 19:39
Show Gist options
  • Save paulofaria/506b8106695d4736b2c5 to your computer and use it in GitHub Desktop.
Save paulofaria/506b8106695d4736b2c5 to your computer and use it in GitHub Desktop.
public protocol RouteType: ResponderType {
var methods: Set<Method> { get }
var path: String { get }
var responder: ResponderType { get }
}
public protocol RouteMatcherType: ResponderType {
var routes: [RouteType] { get set }
var fallback: ResponderType { get set }
func addRouteWithMethods(methods: Set<Method>, path: String, responder: ResponderType)
func match(request: Request) -> ResponderType?
}
extension RouteMatcherType {
public func respond(request: Request) throws -> Response {
let responder = self.match(request) ?? self.fallback
return try responder.respond(request)
}
}
public struct Router: ResponderType {
public let matcher: RouteMatcherType
public let middleware: [MiddlewareType]
public init(middleware: [MiddlewareType], matcher: RouteMatcherType) {
self.middleware = middleware
self.matcher = matcher
}
public func respond(request: Request) throws -> Response {
return try middleware.intercept(matcher).respond(request)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment