Skip to content

Instantly share code, notes, and snippets.

@29satnam
Created February 5, 2019 17:09
Show Gist options
  • Save 29satnam/d18ab59fc7f894f5c39aa10221c55795 to your computer and use it in GitHub Desktop.
Save 29satnam/d18ab59fc7f894f5c39aa10221c55795 to your computer and use it in GitHub Desktop.
Vapor very basic route handeling
import Vapor
/// Register your application's routes here.
public func routes(_ router: Router) throws {
/* // Example of configuring a controller
let todoController = TodoController()
router.get("todos", use: todoController.index)
router.post("todos", use: todoController.create)
router.delete("todos", Todo.parameter, use: todoController.delete) */
//Simple GET
router.get { req in
return "It works!"
}
// GET
router.get(String.parameter) { req in
return "Hello \(try req.parameters.next(String.self))"
}
// POST
router.post(InfoData.self, at: "info") { req, data -> InfoResponse in
return InfoResponse(request: data)
}
}
// Post encoder
struct InfoData: Content {
let name: String
}
// Response encoder
struct InfoResponse: Content {
let request: InfoData
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment