Skip to content

Instantly share code, notes, and snippets.

@gtranchedone
Last active January 28, 2017 03:32
Show Gist options
  • Save gtranchedone/1b9b58c0ca4dbe3538b2707df5959d80 to your computer and use it in GitHub Desktop.
Save gtranchedone/1b9b58c0ca4dbe3538b2707df5959d80 to your computer and use it in GitHub Desktop.
Vapor Classes Templates

Classes that I use as templates for Server Side Swift using Vapor.

import Vapor
import HTTP
extension Vapor.KeyAccessible where Key == HeaderKey, Value == String {
var contentType: String? {
get {
return self["Content-Type"]
}
set {
self["Content-Type"] = newValue
}
}
var authorization: String? {
get {
return self["Authorization"]
}
set {
self["Authorization"] = newValue
}
}
}
public enum ContentType: String {
case html = "text/html"
case json = "application/json"
}
open class ApplicationController {
private var resourcefulName: String {
let className = String(describing: type(of: self))
return className.replacingOccurrences(of: "Controller", with: "").lowercased()
}
public func respond(to request: Request, with response: [ContentType : ResponseRepresentable]) -> ResponseRepresentable {
let contentTypeValue = request.headers.contentType ?? ContentType.html.rawValue
let contentType = ContentType(rawValue: contentTypeValue) ?? ContentType.html
return response[contentType] ?? Response(status: .notFound)
}
public func render(_ path: String, _ context: NodeRepresentable? = nil) throws -> View {
return try drop.view.make("\(resourcefulName)/\(path)", context ?? Node.null)
}
public func render(_ path: String, _ context: [String : NodeRepresentable]?) throws -> View {
return try render(path, context?.makeNode())
}
}
extension Resource {
public convenience init(
index: Multiple? = nil,
show: Item? = nil,
create: Multiple? = nil,
replace: Item? = nil,
update: Item? = nil,
destroy: Item? = nil,
clear: Multiple? = nil,
aboutItem: Item? = nil,
aboutMultiple: Multiple? = nil) {
self.init(
index: index,
store: create,
show: show,
replace: replace,
modify: update,
destroy: destroy,
clear: clear,
aboutItem: aboutItem,
aboutMultiple: aboutMultiple
)
}
}
import Vapor
import HTTP
final class _CLASS_NAME_: ApplicationController, ResourceRepresentable {
/*
func index(request: Request) throws -> ResponseRepresentable {
return respond(to: request, with: [.json: try _RESOURCE_NAME_.all().makeNode().converted(to: JSON.self),
.html: try render("index")])
}
*/
/*
public func new(request: Request) throws -> ResponseRepresentable {
return respond(to: request, with: [.html: try render("new")])
}
*/
/*
func create(request: Request) throws -> ResponseRepresentable {
var _VAR_NAME_ = try request._VAR_NAME_()
try _VAR_NAME_.save()
return _VAR_NAME_
}
*/
/*
func show(request: Request, _VAR_NAME_: _RESOURCE_NAME_) throws -> ResponseRepresentable {
return _VAR_NAME_
}
*/
/*
public func edit(request: Request) throws -> ResponseRepresentable {
return respond(to: request, with: [.html: try render("edit")])
}
*/
/*
func delete(request: Request, _VAR_NAME_: _RESOURCE_NAME_) throws -> ResponseRepresentable {
try _VAR_NAME_.delete()
return JSON([:])
}
*/
/*
func clear(request: Request) throws -> ResponseRepresentable {
try _RESOURCE_NAME_.query().delete()
return JSON([])
}
*/
/*
func update(request: Request, _VAR_NAME_: _RESOURCE_NAME_) throws -> ResponseRepresentable {
let new = try request._VAR_NAME_()
var _VAR_NAME_ = _VAR_NAME_
// Update Logic Here
try _VAR_NAME_.save()
return _VAR_NAME_
}
*/
/*
func replace(request: Request, _VAR_NAME_: _RESOURCE_NAME_) throws -> ResponseRepresentable {
try _VAR_NAME_.delete()
return try create(request: request)
}
*/
func makeResource() -> Resource<_RESOURCE_NAME_> {
return Resource(
index: nil, // index,
show: nil, // show,
new: nil, // new,
create: nil, // create,
edit: nil, // edit,
replace: nil, // replace,
update: nil, // update,
destroy: nil, // delete,
clear: nil // clear
)
}
}
extension Request {
func _VAR_NAME_() throws -> _RESOURCE_NAME_ {
guard let json = json else { throw Abort.badRequest }
return try _RESOURCE_NAME_(node: json)
}
}
import Vapor
import HTTP
final class _CLASS_NAME_: ApplicationController {
}
import Vapor
import Fluent
import Foundation
final class _CLASS_NAME_: Model {
static let tableName = "_TABLE_NAME_"
var id: Node?
_IVARS_DEFINITION_
init(node: Node, in context: Context) throws {
id = try node.extract("id")
_IVARS_INITIALIZER_
}
func makeNode(context: Context) throws -> Node {
return try Node(node: [
"id": id,
_IVARS_DICTIONARY_PAIRS_
])
}
}
extension _CLASS_NAME_: Preparation {
static func prepare(_ database: Database) throws {
try database.create(_CLASS_NAME_.tableName) { _IVAR_NAME_ in
_IVAR_NAME_.id()
_TABLE_ROWS_DEFINITION_
}
}
static func revert(_ database: Database) throws {
try database.delete(_CLASS_NAME_.tableName)
}
}
router.resource("_ROUTE_", _HANDLER_)
import Routing
import Vapor
import HTTP
func configureRoutes<T : Routing.RouteBuilder>(router: T) where T.Value == HTTP.Responder {
}
router._METHOD_("_ROUTE_") { request in
return _HANDLER_
}
#extend("base")
#export("body") {
<h1>Hello, world!</h1>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment