Created
October 15, 2013 18:59
-
-
Save etscrivner/6996865 to your computer and use it in GitHub Desktop.
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
/** index.d */ | |
module index; | |
import std.conv; | |
import std.datetime; | |
import vibe.d; | |
void showHome(HTTPServerRequest req, HTTPServerResponse res) { | |
auto username = "Eric Scrivner"; | |
auto total_time = Clock.currTime() - req.timeCreated; | |
auto ellapsed = text(total_time.fracSec.usecs / 1000000.0); | |
res.writeJsonBody(["username": username, "total_time": ellapsed]); | |
} | |
/** app.d */ | |
module app; | |
import vibe.d; | |
import index; | |
import std.stdio; | |
void showError(HTTPServerRequest req, HTTPServerResponse res, HTTPServerErrorInfo error) { | |
writeln(error.debugMessage); | |
res.writeJsonBody(["error": error.debugMessage]); | |
} | |
shared static this() { | |
auto router = new URLRouter(); | |
router.get("/", &showHome); | |
auto settings = new HTTPServerSettings; | |
settings.port = 8080; | |
settings.errorPageHandler = toDelegate(&showError); | |
listenHTTP(settings, router); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment