Classes that I use as templates for Server Side Swift using Vapor.
Forked from gtranchedone/App Templates For Vapor.md
Last active
February 1, 2017 22:09
-
-
Save wpuricz/84007a4ceacd92b9ccc22600dfb29b4e to your computer and use it in GitHub Desktop.
Vapor Classes Templates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Vapor | |
import HTTP | |
final class _CLASS_NAME_: ResourceRepresentable { | |
func index(request: Request) throws -> ResponseRepresentable { | |
return try _RESOURCE_NAME_.all().makeNode().converted(to: JSON.self) | |
} | |
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_ | |
} | |
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_ | |
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: index, | |
store: create, | |
show: show, | |
replace: replace, | |
modify: update, | |
destroy: delete, | |
clear: clear | |
) | |
} | |
} | |
extension Request { | |
func _VAR_NAME_() throws -> _RESOURCE_NAME_ { | |
guard let json = json else { throw Abort.badRequest } | |
return try _RESOURCE_NAME_(node: json) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Vapor | |
import HTTP | |
final class _CLASS_NAME_: ApplicationController { | |
// this is a simple controller | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Fluent | |
final class _MIGRATION_NAME_: Preparation { | |
static func prepare(_ database: Database) throws { | |
try database.modify("_TABLE_NAME_", closure: { _TABLE_NAME_ in | |
//_TABLE_NAME_.string("foo", length: 150, optional: false, unique: false, default: nil) | |
}) | |
} | |
static func revert(_ database: Database) throws { | |
try database.modify("_TABLE_NAME_", closure: { _TABLE_NAME_ in | |
//_TABLE_NAME_.delete("foo") | |
}) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Vapor | |
func loadPreparations(drop:Droplet) { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
drop.preparations.append(_PREPARATION_.self) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
router.resource("_ROUTE_", _HANDLER_) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Routing | |
import Vapor | |
import HTTP | |
func configureRoutes<T : Routing.RouteBuilder>(router: T) where T.Value == HTTP.Responder { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
router._METHOD_("_ROUTE_") { request in | |
return _HANDLER_ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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