Skip to content

Instantly share code, notes, and snippets.

@Siemian
Created April 18, 2017 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Siemian/21a83ad5a93bca14e815259a2423c161 to your computer and use it in GitHub Desktop.
Save Siemian/21a83ad5a93bca14e815259a2423c161 to your computer and use it in GitHub Desktop.
<html>
<head>
</head>
<body>
<h1>Hello</h1>
</body>
</html>
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) {
let values = MustacheEvaluationContext.MapType()
mustacheRequest(request: request, response: response, handler: MustacheHelper(values: values), templatePath: request.documentRoot + "/index.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