This repo contains Swift Vapor preparation guidelines and content for an upcoming Revel hackathon
- Xcode 9 or later
- Swift 4.1 (We'll use Vapor 3. Vapor 4 is the latest version, it requires Swift 5.1)
- Postman
- Docker installed
- Vapor toolbox is a tool for creating, building, running Vapor projects.
- Generating Xcode projects
- Deploying projects with Vapor Cloud
eval "$(curl -sL check.vapor.sh)"
brew install vapor/tap/vapor
eval "$(curl -sL https://apt.vapor.sh)"
Create, Build and Start an app
vapor new VaporWorkshop
vapor build
vapor run
The template project is built and running on http://localhost:8080/hello
Stop the currently running app with Control-C
vapor xcode -y
router.get("hello", String.parameter) { req -> String in
let name = try req.parameters.next(String.self)
return "Hello, \(name)!"
}
//Accepting data
router.post(RevelThread.self, at: "thread") { req, data -> String in
return "Thread with title: \(data.title) posted."
}
//Returning json
router.post(RevelThread.self, at: "thread") { req, data -> RevelThread in
return data
}
Choose from:
- SQLite
- MySQL
- PostgreSQL
docker run --name postres -e POSTGRES_DB=vapor \-e POSTGRES_USER=vapor -e POSTGRES_PASSWORD=password \-p 5432:5432 -d postgres
Check status
docker ps
- Package.swift
vapor xcode -y
- Configure.swift
- RevelThread.swift
In Vapor a promise to deliver the result at some point of time is called Future. There are two main functions for dealing with unwrapping the result:
flatMap(to:) // use when promise closure returns a Future
map(to:) // use when promise closure returns a type other than Future
router.post("api", "threads") { req -> Future<RevelThread> in
return try req.content.decode(RevelThread.self)
.flatMap(to: RevelThread.self) { thread in
return thread.save(on: req)
}
}
// get all threads
router.get("api", "threads") { req -> Future<[RevelThread]> in
return RevelThread.query(on: req).all()
}
// get single with parameter
router.get("api", "threads", RevelThread.parameter) { req -> Future<RevelThread> in
return try req.parameters.next(RevelThread.self)
}
// update
router.put("api", "threads", RevelThread.parameter) { req -> Future<RevelThread> in
return try flatMap(to: RevelThread.self,
req.parameters.next(RevelThread.self),
req.content.decode(RevelThread.self)) { acronym, updatedAcronym in
acronym.title = updatedAcronym.title
return acronym.save(on: req)
}
}
// delete
router.delete("api", "threads", RevelThread.parameter) { req -> Future<HTTPStatus> in
return try req.parameters
.next(RevelThread.self)
.delete(on: req)
.transform(to: HTTPStatus.noContent)
}