Skip to content

Instantly share code, notes, and snippets.

@alexmherrmann
Last active February 25, 2016 19:48
Show Gist options
  • Save alexmherrmann/b5ecf66cf8e7192606ea to your computer and use it in GitHub Desktop.
Save alexmherrmann/b5ecf66cf8e7192606ea to your computer and use it in GitHub Desktop.
tiny_markdown_server
{
"name": "markdown-server",
"description": "A minimal D application.",
"copyright": "Copyright © 2016, alex",
"authors": ["alex"],
"dependencies": {
"vibe-d": "~>0.7.27"
},
"versions": ["VibeDefaultMain"]
}
import std.stdio;
import std.file;
import vibe.d;
immutable string content_type = "text/html; charset=UTF-8";
void renderMd(HTTPServerRequest req, HTTPServerResponse res) {
string file = "files/" ~ req.params["doc"] ~ ".md";
// Good to render it!
if (exists(file) && isFile(file) ) {
res.writeBody(filterMarkdown(readText(file)), content_type);
} else {
res.statusCode = 404;
res.writeBody("<h1>404!: %s could not be found</h1>".format(file), content_type);
}
}
shared static this() {
auto router = new URLRouter();
router.get("/md/:doc", &renderMd);
auto settings = new HTTPServerSettings;
listenHTTP(settings, router);
}
import std.stdio;
import std.file;
import vibe.d;
void renderMd(HTTPServerRequest req, HTTPServerResponse res) {
string file = "files/" ~ req.params["doc"] ~ ".md";
writeln("got %s", file);
writefln("file: %s exists? %s", file, exists(file) && isFile(file));
string content_type = "text/html; charset=UTF-8";
// Good to render it!
if (exists(file) && isFile(file) ) {
res.writeBody(filterMarkdown(readText(file)), content_type);
return;
} else {
res.statusCode = 404;
res.writeBody("<h1>404!: %s could not be found</h1>".format(file), content_type);
return;
}
}
static void ayy() {
writeln("beginning server");
auto router = new URLRouter();
router.get("/md/:doc", &renderMd);
auto settings = new HTTPServerSettings;
settings.port = 8085;
// settings.bindAddresses = ["localhost", "127.0.0.1"];
settings.bindAddresses = ["127.0.0.1"];
listenHTTP(settings, router);
writeln("ending server");
}
shared static this() {
ayy();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment