Last active
January 7, 2016 15:58
-
-
Save paulofaria/c330e028af8c56f21a4e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol MiddlewareType { | |
func intercept(request: Request, nextResponder: ResponderType) throws -> Response | |
} | |
protocol RequestMiddlewareType: MiddlewareType { | |
func intercept(request: Request) throws -> Request | |
} | |
extension RequestMiddlewareType { | |
func intercept(request: Request, nextResponder: ResponderType) throws -> Response { | |
let interceptedRequest = try self.intercept(request) | |
return try nextResponder.respond(request) | |
} | |
} | |
protocol ResponseMiddlewareType: MiddlewareType { | |
func intercept(response: Response) throws -> Response | |
} | |
extension ResponseMiddlewareType { | |
func intercept(request: Request, nextResponder: ResponderType) throws -> Response { | |
let response = try nextResponder.respond(request) | |
return try self.intercept(response) | |
} | |
} | |
protocol FailureRecoveryMiddlewareType: MiddlewareType { | |
func intercept(error: ErrorType) -> Response | |
} | |
extension FailureRecoveryMiddlewareType { | |
func intercept(request: Request, nextResponder: ResponderType) throws -> Response { | |
do { | |
return try nextResponder.respond(request) | |
} catch { | |
return self.intercept(error) | |
} | |
} | |
} | |
enum EarlyReturnMiddlewareResult { | |
case PassForward(Request) | |
case EarlyReturn(Response) | |
} | |
protocol EarlyReturnMiddlewareType: MiddlewareType { | |
func intercept(request: Request) throws -> EarlyReturnMiddlewareResult | |
} | |
extension EarlyReturnMiddlewareType { | |
func intercept(request: Request, nextResponder: ResponderType) throws -> Response { | |
switch try self.intercept(request) { | |
case .PassForward(let request): | |
return try nextResponder.respond(request) | |
case .EarlyReturn(let response): | |
return response | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
route.get("/", middleware: logger, errorHandler, authenticator, contentNegotiator, templateRender, responder: responder)