Skip to content

Instantly share code, notes, and snippets.

View cweinberger's full-sized avatar

Christian Weinberger cweinberger

View GitHub Profile
func boot(routes: RoutesBuilder) throws {
routes.get("", use: getBooks)
routes.get(":bookID", use: getBook)
}
func getBook(req: Request) throws -> Book {
guard
// 1
let bookIDString = req.parameters.get("bookID"),
// 2
let bookID = Int(bookIDString),
// 3
let book = Self.books.first(where: { $0.id == bookID })
else {
// 4
// 1
let apiRoutes = app.grouped("api", "v1")
// 2
try apiRoutes.grouped("books").register(collection: bookAPIController)
// 1
let bookAPIController = BookAPIController()
// 2
try app.register(collection: bookAPIController)
func routes(_ app: Application) throws {
// 1
// a GET request to /hello1
app.get("hello1") { req -> String in
return "hello1"
}
// 2
// a GET request to /hello2
import Vapor
final class BookAPIController {
// 1
struct Book: Content {
let id: Int
let title: String
let author: String
}
import Vapor
func routes(_ app: Application) throws {
}
@cweinberger
cweinberger / vapor-custom-arguments.md
Last active December 12, 2019 10:03
Custom arguments when running Vapor

How to add (and read) custom arguments when launching a vapor app.

In app.swift I have added a struct AppOptions to keep track of custom options and added another parameter options to the app(_:) function:

import Vapor

public struct AppOptions {
    public var foo: String?