Skip to content

Instantly share code, notes, and snippets.

@5t111111
Created May 14, 2018 03:09
Show Gist options
  • Save 5t111111/53c3408574d394740b2671c6168b7e3a to your computer and use it in GitHub Desktop.
Save 5t111111/53c3408574d394740b2671c6168b7e3a to your computer and use it in GitHub Desktop.
Vapor 3.0 middleware
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
}
}
}
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