Skip to content

Instantly share code, notes, and snippets.

@etscrivner
Created October 15, 2013 18:59
Show Gist options
  • Save etscrivner/6996865 to your computer and use it in GitHub Desktop.
Save etscrivner/6996865 to your computer and use it in GitHub Desktop.
/** 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