Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Route specific middleware #66

Merged
merged 1 commit into from
Feb 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,18 @@ class MyMiddleware: Middleware {
app.middleware.append(MyMiddleware)
```

Middleware can also be applied to a specific set of routes by using the `app.middleware(_: handler:)` method.

```swift
app.get("welcome") { ... }

app.middleware([AuthMiddleware]) {
app.get("user") { ... }
}
```

In this example the AuthMiddleware will be applied to the `user` route but not the `welcome` route.

## Providers

[Providers](https://github.com/qutheory/vapor/wiki/Provider) and [Drivers](https://github.com/qutheory/vapor/wiki/Driver) allow almost any component of Vapor to be extended or replaced.
Expand Down
40 changes: 20 additions & 20 deletions Sources/Application+Route.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ extension Application {
return try handler(request).response()
}

// //Apply any scoped middlewares
// for middleware in Route.scopedMiddleware {
// handler = middleware.handle(handler)
// }
//Apply any scoped middlewares
for middleware in Route.scopedMiddleware {
handler = middleware.handle(handler)
}

//Store the route for registering with Router later
let route = Route(method: method, path: path, handler: handler)
Expand All @@ -125,23 +125,23 @@ extension Application {
self.routes.append(route)
}

// /**
// Applies the middleware to the routes defined
// inside the closure. This method can be nested within
// itself safely.
// */
// public func middleware(middleware: Middleware.Type, handler: () -> ()) {
// self.middleware([middleware], handler: handler)
// }
/**
Applies the middleware to the routes defined
inside the closure. This method can be nested within
itself safely.
*/
public final func middleware(middleware: Middleware.Type, handler: () -> ()) {
self.middleware([middleware], handler: handler)
}

// public func middleware(middleware: [Middleware.Type], handler: () -> ()) {
// let original = Route.scopedMiddleware
// Route.scopedMiddleware += middleware
//
// handler()
//
// Route.scopedMiddleware = original
// }
public final func middleware(middleware: [Middleware.Type], handler: () -> ()) {
let original = Route.scopedMiddleware
Route.scopedMiddleware += middleware

handler()

Route.scopedMiddleware = original
}

public final func host(host: String, handler: () -> Void) {
Route.scopedHost = host
Expand Down