Skip to content

Instantly share code, notes, and snippets.

View cweinberger's full-sized avatar

Christian Weinberger cweinberger

View GitHub Profile
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)
import Vapor
final class BookAPIController {
// 1
struct Book: Content {
let id: Int
let title: String
let author: String
}
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
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?
    
@cweinberger
cweinberger / mysql-drop-all-tables.sh
Created June 6, 2018 12:44
drops all tables of a specific db
#!/bin/bash
#usage: mysql-drop-all-tables -d database -u dbuser -p dbpass
TEMP_FILE_PATH='./drop_all_tables.sql'
while getopts d:u:p: option
do
case "${option}"
in