Created
April 18, 2017 18:11
-
-
Save Siemian/6cb29db4a13279c0e83b69a6a2914856 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<html> | |
<head> | |
</head> | |
<body> | |
{{#users}} | |
<h1>Hello {{name}}</h1> | |
{{/users}} | |
</body> | |
</html> |
This file contains hidden or 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 PerfectLib | |
import PerfectHTTP | |
import PerfectHTTPServer | |
import PerfectMustache | |
struct MustacheHelper: MustachePageHandler { | |
var values: MustacheEvaluationContext.MapType | |
func extendValuesForResponse(context contxt: MustacheWebEvaluationContext, collector: MustacheEvaluationOutputCollector) { | |
contxt.extendValues(with: values) | |
do { | |
try contxt.requestCompleted(withCollector: collector) | |
} catch { | |
contxt.webResponse.appendBody(string: "\(error)").completed(status: .internalServerError) | |
} | |
} | |
} | |
func helloMustache(request: HTTPRequest, response: HTTPResponse) { | |
var values = MustacheEvaluationContext.MapType() | |
values["users"] = [ | |
["name": "Bill"], | |
["name": "John"], | |
["name": "Tom"] | |
] | |
mustacheRequest(request: request, response: response, handler: MustacheHelper(values: values), templatePath: request.documentRoot + "/index2.mustache") | |
} | |
let server = HTTPServer() | |
server.serverPort = 8080 | |
server.documentRoot = "webroot" | |
var routes = Routes() | |
routes.add(method: .get, uri: "", handler: helloMustache) | |
server.addRoutes(routes) | |
do { | |
try server.start() | |
} catch PerfectError.networkError(let error, let message) { | |
print("Error: \(error), Message: \(message)") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment