Skip to content

Instantly share code, notes, and snippets.

@kjessup
Created June 2, 2016 04:42
Show Gist options
  • Save kjessup/605a4aace3d37591d1aba34d8f4c786f to your computer and use it in GitHub Desktop.
Save kjessup/605a4aace3d37591d1aba34d8f4c786f to your computer and use it in GitHub Desktop.
Perfect 1.0 routes + mustache
public func PerfectServerModuleInit() {
Routing.Handler.registerGlobally()
Routing.Routes["GET", "/"] = { req in return Example(path: "index.mustache") }
}
struct Example: RequestHandler {
let path: String
init(path: String) {
self.path = path
}
func handleRequest(request: WebRequest, response: WebResponse) {
do {
let path = request.documentRoot + self.path
let file = File(path)
defer {
file.close()
}
try file.openRead()
let usingTemplate = try file.readString()
let template = try MustacheParser().parse(usingTemplate)
let d = ["name":"The name"] as [String:Any]
let context = MustacheEvaluationContext(map: d)
let collector = MustacheEvaluationOutputCollector()
template.evaluate(context, collector: collector)
response.appendBodyString(collector.asString())
} catch {
// handle error
}
response.requestCompletedCallback()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment