Skip to content

Instantly share code, notes, and snippets.

Avatar
👨‍💻
coding

Christian Weinberger cweinberger

👨‍💻
coding
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
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.directory.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
cweinberger / change-branches-github.sh
Last active Jun 18, 2020
Change branch from `master` to `main`
View change-branches-github.sh
# if you are using GitHub, you can use this tool to change all repos at once
# https://github.com/mheap/github-default-branch
# 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
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"] }'
```
Prints:
```
@cweinberger
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)`
See: https://forums.swift.org/t/vapor-3-swift-5-2-regression/34764
Instead of adding this to each model, I added these extensions.
View mysql with persistence
docker run -d \
--restart=always \
-p 33060:3306 \
-e MYSQL_ROOT_PASSWORD=root \
-v ~/docker/volumes/mysql:/var/lib/mysql \
--name mysql-5.7 \
mysql:5.7 \
--sql-mode="NO_ENGINE_SUBSTITUTION"
View EventLoopFuture+tryFlatMap.swift
import NIO
/// Author: vzsg (Discord) - https://twitter.com/vzsg_dev
/// Source: https://discordapp.com/channels/431917998102675485/684159753189982218/684537099378098272
extension EventLoopFuture {
func tryFlatMap<NewValue>(
file: StaticString = #file,
line: UInt = #line,
_ callback: @escaping (Value) throws -> EventLoopFuture<NewValue>
@cweinberger
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)
}