-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
205 additions
and
154 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// | ||
// Created by Benoit BRIATTE on 03/06/2017. | ||
// | ||
|
||
/** | ||
* Composite MiddlewareBinder back an array of MiddlewareBinder | ||
* This class is used when the users want to bind the same middleware to 2 or more HTTPMethod in the same time | ||
* @author Benoit BRIATTE http://www.digipolitan.com | ||
* @copyright 2017 Digipolitan. All rights reserved. | ||
*/ | ||
internal class CompositeMiddlewareBinder: MiddlewareBinder { | ||
|
||
public let children: [MiddlewareBinder] | ||
|
||
public init(children: [MiddlewareBinder]) { | ||
self.children = children | ||
} | ||
|
||
@discardableResult | ||
public func bind(_ middleware: Middleware) -> Self { | ||
self.children.forEach { child in | ||
child.bind(middleware) | ||
} | ||
return self | ||
} | ||
|
||
@discardableResult | ||
public func bind(_ middlewares: [Middleware]) -> Self { | ||
self.children.forEach { child in | ||
child.bind(middlewares) | ||
} | ||
return self | ||
} | ||
|
||
@discardableResult | ||
public func bind(_ handler: @escaping MiddlewareHandler) -> Self { | ||
return self.bind(MiddlewareWrapper(handler: handler)) | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import PerfectHTTP | ||
|
||
/** | ||
* MiddlewareBinder protocol, is used to bind middlewares or handlers inside a route with a method chaining pattern | ||
* @author Benoit BRIATTE http://www.digipolitan.com | ||
* @copyright 2017 Digipolitan. All rights reserved. | ||
*/ | ||
public protocol MiddlewareBinder { | ||
|
||
/** | ||
* Register a middleware | ||
* @param middleware The middleware | ||
* @return MiddlewareBinder | ||
*/ | ||
@discardableResult | ||
func bind(_ middleware: Middleware) -> Self | ||
|
||
/** | ||
* Register an array of middleware | ||
* @param middlewares The array | ||
* @return MiddlewareBinder | ||
*/ | ||
@discardableResult | ||
func bind(_ middlewares: [Middleware]) -> Self | ||
|
||
/** | ||
* Register a closure | ||
* @param handler The handler | ||
* @return MiddlewareBinder | ||
*/ | ||
@discardableResult | ||
func bind(_ handler: @escaping MiddlewareHandler) -> Self | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import PerfectHTTP | ||
|
||
/** | ||
* Registry of RouteMiddlewareBinder, back all the route binding of the RouterMiddleware | ||
* @author Benoit BRIATTE http://www.digipolitan.com | ||
* @copyright 2017 Digipolitan. All rights reserved. | ||
*/ | ||
internal class RouteMiddlewareRegistry { | ||
|
||
public private(set) var binders: [HTTPMethod: [String: RouteMiddlewareBinder]] | ||
|
||
public init() { | ||
self.binders = [HTTPMethod: [String: RouteMiddlewareBinder]]() | ||
} | ||
|
||
/** | ||
* Find or create a RouteMiddlewareBinder | ||
* @param method The HTTPMethod | ||
* @param path The route path | ||
* @return Old RouteMiddlewareBinder with the combination of HTTPMethod / path otherwise a new one | ||
*/ | ||
public func findOrCreate(method: HTTPMethod, path: String) -> RouteMiddlewareBinder { | ||
let sanitizePath = RouterMiddleware.sanitize(path: path) | ||
if self.binders[method] == nil { | ||
self.binders[method] = [String: RouteMiddlewareBinder]() | ||
} | ||
if let binder = self.binders[method]![sanitizePath] { | ||
return binder | ||
} | ||
let binder = RouteMiddlewareBinder() | ||
self.binders[method]![sanitizePath] = binder | ||
return binder | ||
} | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// | ||
// Created by Benoit BRIATTE on 03/06/2017. | ||
// | ||
|
||
import Foundation | ||
|
||
/** | ||
* Default implementation of the MiddlewareBinder | ||
* Allows the user to register middleware and handler inside an array | ||
*/ | ||
internal class RouteMiddlewareBinder: MiddlewareBinder { | ||
|
||
public var middlewares: [Middleware] | ||
|
||
public init() { | ||
self.middlewares = [] | ||
} | ||
|
||
@discardableResult | ||
public func bind(_ middleware: Middleware) -> Self { | ||
self.middlewares.append(middleware) | ||
return self | ||
} | ||
|
||
@discardableResult | ||
public func bind(_ middlewares: [Middleware]) -> Self { | ||
self.middlewares.append(contentsOf: middlewares) | ||
return self | ||
} | ||
|
||
@discardableResult | ||
public func bind(_ handler: @escaping MiddlewareHandler) -> Self { | ||
return self.bind(MiddlewareWrapper(handler: handler)) | ||
} | ||
} |
Oops, something went wrong.