Created
April 18, 2017 18:09
-
-
Save Siemian/21a83ad5a93bca14e815259a2423c161 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> | |
<h1>Hello</h1> | |
</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) { | |
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