Skip to content

Instantly share code, notes, and snippets.

View tanner0101's full-sized avatar
💭
💧

Tanner tanner0101

💭
💧
View GitHub Profile
enum FujiMessage {
case credentials(username: String, password: String)
case didCreatePart(barcode: String)
case success(message: String)
case error(message: String)
}
protocol FujiConnection {
func send(message: FujiMessage)
}
@tanner0101
tanner0101 / vapor-4-fluent-authc.swift
Last active May 2, 2024 14:24
Example of authentication with Fluent in Vapor 4
import Fluent
import Vapor
func routes(_ app: Application) throws {
app.post("users") { req -> EventLoopFuture<User> in
try User.Create.validate(req)
let create = try req.content.decode(User.Create.self)
guard create.password == create.confirmPassword else {
throw Abort(.badRequest, reason: "Passwords did not match")
}
@tanner0101
tanner0101 / whats-new-vapor-4.md
Last active September 6, 2023 07:25
What's New in Vapor 4

What's New in Vapor 4

We've been working on the fourth major release of Vapor for almost a year now. The first alpha version was tagged last May, with the first beta following in October. During that time, the community has done amazing work helping to test, improve, and refine this release. Over 500 issues and pull requests have been closed so far!

Looking back at Vapor 3's pre-release timeline, 7 months passed between alpha.1 and the final release. If history repeats itself, we would reach 4.0.0 sometime in February 2020.

As we near the end of the active development phase, efforts are shifting toward a focus on documentation and polish. Since APIs have mostly settled down at this point, I'd like to take this opportunity to introduce you to some of the exciting changes coming in Vapor 4. Let's dive in.

New Dependency Injection API

protocol HookEvent { }
protocol HookEventHandler {
func handle(_ event: HookEvent)
}
protocol DiscordEventHandler: HookEventHandler {
func handle(_ event: DiscordEvent)
}
/// Aliquam quis dolor tristique, vulputate lectus id, maximus ipsum.
/// Fusce placerat metus sed luctus posuere. Phasellus ultrices iaculis diam,
/// sit amet volutpat urna elementum sed.
final class Clarity: Model {
/// Aliquam quis dolor tristique, vulputate lectus id, maximus ipsum.
/// Fusce placerat metus sed luctus posuere. Phasellus ultrices iaculis diam,
/// sit amet volutpat urna elementum sed.
@ID(key: "at", generatedBy: .user)
var id: Date?
@tanner0101
tanner0101 / .zshrc
Last active June 25, 2019 21:13
zsh profile
# echo current git branch
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ \1/'
}
# echo "*" if git status is unclean
parse_git_status() {
GIT_STATUS="$(git status 2>&1)"
if [[ $GIT_STATUS != *"clean"* ]] &&
[[ $GIT_STATUS != *"not a git repository"* ]];
@tanner0101
tanner0101 / streaming-download.swift
Last active May 15, 2019 19:36
Vapor 3 Streaming Download
import Vapor
public func routes(_ router: Router) throws {
router.get("streaming") { req -> Future<Response> in
// URL to test file that will be downloaded
// https://speed.hetzner.de/100MB.bin
// https://speed.hetzner.de/10GB.bin
let testFile = URL(string: "https://speed.hetzner.de/100MB.bin")!
// create an instance of our URL session delegate
==27371==
==27371== Process terminating with default action of signal 2 (SIGINT)
==27371== at 0x53659F3: futex_wait_cancelable (futex-internal.h:88)
==27371== by 0x53659F3: __pthread_cond_wait_common (pthread_cond_wait.c:502)
==27371== by 0x53659F3: pthread_cond_wait@@GLIBC_2.3.2 (pthread_cond_wait.c:655)
==27371== by 0x3FD1DC: $s21NIOConcurrencyHelpers13ConditionLockC4lock9whenValueyx_tF (in /home/tanner/dev/sandbox/vapor-memory-leak/.build/x86_64-unknown-linux/release/Run)
==27371== by 0x3A812C: $s3NIO15EventLoopFutureC4wait4file4linexs12StaticStringV_SutKF (in /home/tanner/dev/sandbox/vapor-memory-leak/.build/x86_64-unknown-linux/release/Run)
==27371== by 0x4FDD3C: $s5Vapor11ApplicationC3runyyKF (in /home/tanner/dev/sandbox/vapor-memory-leak/.build/x86_64-unknown-linux/release/Run)
==27371== by 0x463610: main (in /home/tanner/dev/sandbox/vapor-memory-leak/.build/x86_64-unknown-linux/release/Run)
==27371==
r.get("posts", ":postID") { req in
let postID = try req.parameters.get(":postID")
print(postID) // String
...
}
enum PathComponent {
case part(String)
case dynamic(name: String)
case anything
@tanner0101
tanner0101 / swift-4.1-macos.md
Created January 31, 2018 01:42
Swift 4.1 on macOS
  1. Download latest "Swift 4.1 Development" snapshot from Swift.org Snapshots.
  2. Open downloaded .pkg file to install.
  3. Xcode > Toolchains > Swift 4.1 Snapshot