Skip to content

Commit

Permalink
Merge branch 'release/1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
bbriatte committed Jun 3, 2017
2 parents 1a57a4d + 8856560 commit ff1b26a
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 154 deletions.
29 changes: 15 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import PackageDescription
let package = Package(
name: "XXX",
dependencies: [
.Package(url: "https://github.com/Digipolitan/perfect-middleware-swift.git", majorVersion: 1, minor: 0)
.Package(url: "https://github.com/Digipolitan/perfect-middleware-swift.git", majorVersion: 1)
]
)
```
Expand All @@ -31,7 +31,7 @@ let server = HTTPServer()

let router = RouterMiddleware()

router.get(path: "/") { (context) in
router.get(path: "/").bind { (context) in
context.response.setBody(string: "It Works !").completed()
context.next()
}
Expand All @@ -51,18 +51,18 @@ do {
Passing data between middlewares, you can provide 2 or more middleware for the same route and shared data across each middleware using the context

```swift
router.get(path: "/") { (context) in
router.get(path: "/")
.bind { (context) in
context["name"] = "Steve"
context.next()
}

router.get(path: "/") { (context) in
}
.bind { (context) in
guard let name = context["name"] as? String else {
return
}
context.response.setBody(string: "hello mr. \(name)!").completed()
context.next()
}
}
```

It's possible to create and register Middleware subsclasses instead of closures
Expand All @@ -80,15 +80,15 @@ class RandomMiddleware: Middleware {
Register Middleware as follow :

```swift
router.post(path: "/random", middleware: RandomMiddleware())

router.post(path: "/random") { (context) in
router.post(path: "/random")
.bind(RandomMiddleware())
.bind { (context) in
guard let rand = context["rand"] as? UInt32 else {
return
}
context.response.setBody(string: "the result \(rand)").completed()
context.next()
}
}
```

## Advanced
Expand Down Expand Up @@ -140,12 +140,13 @@ let router = RouterMiddleware()

let childRouter = RouterMiddleware()

childRouter.get(path: "/name") { (context) in
childRouter.get(path: "/name")
.bind { (context) in
context.response.setBody(string: "My name is").completed()
context.next()
}
}

router.use(path: "/user", router: childRouter)
router.use(path: "/user", child: childRouter)

server.use(router: router)
```
Expand Down
39 changes: 39 additions & 0 deletions Sources/CompositeMiddlewareBinder.swift
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))
}
}
8 changes: 0 additions & 8 deletions Sources/Middleware.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
//
// Middleware.swift
// PerfectMiddleware
//
// Created by Benoit BRIATTE on 17/04/2017.
//
//

import PerfectHTTP

/**
Expand Down
33 changes: 33 additions & 0 deletions Sources/MiddlewareBinder.swift
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
}
10 changes: 1 addition & 9 deletions Sources/MiddlewareIterator.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
//
// MiddlewareIterator.swift
// PerfectMiddleware
//
// Created by Benoit BRIATTE on 20/04/2017.
//
//

import PerfectHTTP

/**
Expand All @@ -14,7 +6,7 @@ import PerfectHTTP
* @author Benoit BRIATTE http://www.digipolitan.com
* @copyright 2017 Digipolitan. All rights reserved.
*/
class MiddlewareIterator: RouteContext {
internal class MiddlewareIterator: RouteContext {

public let request: HTTPRequest
public let response: HTTPResponse
Expand Down
34 changes: 34 additions & 0 deletions Sources/MiddlewareRegistry.swift
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
}
}
10 changes: 1 addition & 9 deletions Sources/MiddlewareWrapper.swift
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
//
// MiddlewareWrapper.swift
// PerfectMiddleware
//
// Created by Benoit BRIATTE on 17/04/2017.
//
//

import PerfectHTTP

/**
* MiddlewareWrapper that wrap a middleware handler into a middleware subclass
* @author Benoit BRIATTE http://www.digipolitan.com
* @copyright 2017 Digipolitan. All rights reserved.
*/
class MiddlewareWrapper: Middleware {
internal class MiddlewareWrapper: Middleware {

private let handler: MiddlewareHandler

Expand Down
8 changes: 0 additions & 8 deletions Sources/RouteContext.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
//
// RouteContext.swift
// PerfectMiddleware
//
// Created by Benoit BRIATTE on 17/04/2017.
//
//

import PerfectHTTP

/**
Expand Down
35 changes: 35 additions & 0 deletions Sources/RouteMiddlewareBinder.swift
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))
}
}
Loading

0 comments on commit ff1b26a

Please sign in to comment.