Skip to content

Instantly share code, notes, and snippets.

View vzsg's full-sized avatar

Zsolt Váradi vzsg

  • Budapest, Hungary
View GitHub Profile
@vzsg
vzsg / (1) Environment+EnvFile.swift
Last active September 19, 2019 08:47
Simple .env file injector for Vapor 3
import Foundation
import Service
#if os(Linux)
import Glibc
#else
import Darwin
#endif
extension Environment {
@vzsg
vzsg / 1_DispatchBox.swift
Last active October 1, 2019 13:42
Fetching remote configuration on app boot (Vapor 3)
import Dispatch
// Utility class for thread-safe access to an "app global" variable
final class DispatchBox<T>: Service {
private let queue = DispatchQueue(label: "DispatchBox_\(T.self)_Queue")
private var _value: T?
var value: T? {
get { return queue.sync { self._value }}
set { queue.async { self._value = newValue }}
import Foundation
#if os(Linux)
import Glibc
#else
import Darwin.C
#endif
final class SimpleRandom {
static func random(_ range: ClosedRange<Int32>) -> Int32 {
@vzsg
vzsg / Demo.swift
Last active July 3, 2018 20:47
UrbanAirship + Vapor 3 starting point - now with DSL for audiences
let client = try req.make(Client.self)
let appKey = "APPKEY" // TODO: get from env
let masterSecret = "MASTERSECRET" // TODO: get from env
let broadcast = UABroadcast(
audience: .or([
.channel(.ios, ["AAA", "BBB"]),
.channel(.android, ["CCC", "DDD", "EEE"]),
.tag("hello-tag", group: nil),
@vzsg
vzsg / index.leaf
Created June 25, 2018 21:04
Editable form example with Leaf 3
<!DOCTYPE html>
<html>
<head>
<title>No JS form example</title>
</head>
<body>
<form action="/form" method="post">
#for(s in parts) {
<div>
@vzsg
vzsg / ReactMiddleware.swift
Created June 5, 2018 08:03
React routing compatibility middleware for Vapor 3
// Vapor 3 port by @ajedwards
import Vapor
public final class ReactMiddleware: Middleware, ServiceType {
public static func makeService(for worker: Container) throws -> ReactMiddleware {
return try .init(defaultPath: worker.make(DirectoryConfig.self).workDir + "Public/index.html")
}
/// Default Path to index.html
///
@vzsg
vzsg / 1_SingleDriver.swift
Created March 4, 2018 20:57
Using a single database connection in Vapor 2
// Add this file to your App target
import Fluent
import Vapor
import Dispatch
final class SingleDriver: Fluent.Driver, ConfigInitializable {
private let log: LogProtocol
private var backendDriver: Fluent.Driver
private let queue = DispatchQueue(label: "database-queue", qos: .utility)
@vzsg
vzsg / 1_JSONPrint.swift
Last active February 17, 2018 12:21
Leaf tag for rendering a context variable as JSON
import Leaf
import JSON
final class JSONPrint: BasicTag {
let name: String = "json"
func run(arguments: ArgumentList) throws -> Node? {
guard let arg = arguments.first else {
return nil
}
@vzsg
vzsg / Post.swift
Last active January 4, 2018 10:00
Example of conforming a Vapor 2 Model to Codable
import Vapor
import FluentProvider
import HTTP
import Foundation
final class Post: Model, Codable {
let storage = Storage()
// MARK: Properties and database keys
@vzsg
vzsg / StructuredData+Codable.swift
Last active December 30, 2017 00:48
StructuredData + Codable = win?
import Foundation
import Vapor
private enum UniversalCodingKey: CodingKey {
case int(Int)
case string(String)
init?(intValue: Int) {
self = .int(intValue)
}