Created
June 2, 2016 04:42
-
-
Save kjessup/605a4aace3d37591d1aba34d8f4c786f to your computer and use it in GitHub Desktop.
Perfect 1.0 routes + mustache
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
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