Skip to content

Instantly share code, notes, and snippets.


Christian Weinberger cweinberger

View GitHub Profile
View let-default-value.swift
+ less boilerplate
+ default initializer
- you can alter `limit`
struct MyRequestInput1 {
let searchTerm: String
var limit: Int = 10
View IntOrString-example.swift
import Foundation
enum IntOrString: Encodable {
case integer(Int)
case string(String)
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .integer(let val): try container.encode(val)
cweinberger / corsmiddleware.swift
Last active Jun 20, 2020
Vapor 4 TodoBackend - CORS Middleware
View corsmiddleware.swift
public func configure(_ app: Application) throws {
// Serves files from `Public/` directory
// app.middleware.use(FileMiddleware(publicDirectory:
app.middleware.use(CORSMiddleware()) // Add CORS with default configuration
// Configure SQLite database
app.databases.use(.sqlite(.file("db.sqlite")), as: .sqlite)
// Configure migrations
cweinberger /
Last active Jun 18, 2020
Change branch from `master` to `main`
# if you are using GitHub, you can use this tool to change all repos at once
# Rename dev to develop
github-default-branch --pat <token> --repo user/repo --old dev --new develop
# Rename all repos owned by an org
github-default-branch --pat <token> --org my-org-name
# Rename all repos owned by a user
cweinberger / EventLoopFutureQueue-example.swift
Last active Jan 19, 2021
Vapor 4 EventLoopFutureQueue
View EventLoopFutureQueue-example.swift
Assuming the route handler is attached to `http://localhost:8080/import-data` you can test the implementation with this request (cURL):
curl -X "POST" "http://localhost:8080/import-data" \
-H 'Content-Type: application/json; charset=utf-8' \
-d $'{ "values": ["first", "foo", "bar", "last"] }'
cweinberger / MySQL+Database.swift
Last active May 31, 2020
Vapor 3 with Swift 5.2
View MySQL+Database.swift
import FluentMySQL
Starting with Swift 5.2 the compiler is not able to resolve this automatically:
`migrations.add(model: Todo.self, database: .mysql)`
Instead of adding this to each model, I added these extensions.
View mysql with persistence
docker run -d \
--restart=always \
-p 33060:3306 \
-v ~/docker/volumes/mysql:/var/lib/mysql \
--name mysql-5.7 \
mysql:5.7 \
View EventLoopFuture+tryFlatMap.swift
import NIO
/// Author: vzsg (Discord) -
/// Source:
extension EventLoopFuture {
func tryFlatMap<NewValue>(
file: StaticString = #file,
line: UInt = #line,
_ callback: @escaping (Value) throws -> EventLoopFuture<NewValue>
cweinberger / 1-MySQLDataConvertibleEnum.swift
Last active Apr 13, 2020
Auto-conform your enums types that you want to store in MySQL to `MySQLDataConvertible` and `ReflectionDecodable`. Avoids a lot of boilerplate!
View 1-MySQLDataConvertibleEnum.swift
/* NOTE: check out 3-MySQLDataConvertibleEnum_Better_Version.swift. */
import Fluent
import FluentMySQL
import Vapor
protocol MySQLDataConvertibleEnum: MySQLDataConvertible, ReflectionDecodable, CaseIterable { }
extension MySQLDataConvertibleEnum where Self: RawRepresentable, Self.RawValue == String {
func convertToMySQLData() -> MySQLData {
View blog-vapor4-routing-routes-routecontroller3a.swift
func boot(routes: RoutesBuilder) throws {
routes.get("", use: getBooks)
routes.get(":bookID", use: getBook)