Created
May 14, 2018 03:09
-
-
Save 5t111111/53c3408574d394740b2671c6168b7e3a to your computer and use it in GitHub Desktop.
Vapor 3.0 middleware
This file contains hidden or 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
import Vapor | |
public final class MyMiddleware: Middleware { | |
public init() { | |
} | |
public func respond(to req: Request, chainingTo next: Responder) throws -> Future<Response> { | |
return try next.respond(to: req).map { res in | |
if req.http.headers.contains(name: "X-NYA-N") { | |
res.http.headers.add(name: "X-NYA-N", value: "RESPONSE") | |
} | |
return res | |
} | |
} | |
} |
This file contains hidden or 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
import Vapor | |
public func routes(_ router: Router) throws { | |
let myMiddlewaredRoutes = router.grouped(MyMiddleware()) | |
let homeController = HomeController() | |
myMiddlewaredRoutes.get(use: homeController.index) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment