Skip to content

Instantly share code, notes, and snippets.

@yannick
Created April 27, 2015 15:16
Show Gist options
  • Select an option

  • Save yannick/840b9e2a17b3d46d95c7 to your computer and use it in GitHub Desktop.

Select an option

Save yannick/840b9e2a17b3d46d95c7 to your computer and use it in GitHub Desktop.
module x;
import vibe.d;
import std.format : format;
import core.atomic : atomicOp;
import std.stdio;
import routes;
shared static this()
{
_logstore = new Logstore();
auto router = new URLRouter;
router.get("/a", &routea);
router.get("/b", &routeb);
auto settings = new HTTPServerSettings;
settings.port = 9090;
settings.bindAddresses = ["::1", "127.0.0.1"];
settings.options = settings.options | HTTPServerOption.parseCookies
| HTTPServerOption.distribute | HTTPServerOption.parseQueryString;
listenHTTP(settings, router);
logInfo("Please open http://127.0.0.1:9090/ in your browser.");
}
void hello(HTTPServerRequest req, HTTPServerResponse res)
{
res.writeBody("Hello, World!");
}
module routes;
import vibe.d;
import std.format : format;
import core.atomic : atomicOp;
import std.stdio;
synchronized class Logstore
{
shared(Log)*[ulong] _store;
void add(shared(Log)* l)
{
_store[l.id] = l;
}
shared(Log)* getLog(ulong id)
{
return _store[id];
}
void remove(ulong id)
{
_store.remove(id);
}
}
shared Logstore _logstore;
shared ulong serial;
shared struct Log
{
ulong id;
string data;
}
void routea(HTTPServerRequest req, HTTPServerResponse res)
{
auto s = atomicOp!"+="(serial, 1);
auto log = new shared Log(s, "test");
synchronized(_logstore){
_logstore.add(log);
}
auto redirectUrl = format("/b?s=%s", s);
res.redirect(redirectUrl);
}
void routeb(HTTPServerRequest req, HTTPServerResponse res)
{
auto s_str = req.query.get("s", "0");
int s = to!int(s_str);
Log* l;
synchronized(_logstore){
//get the log and do some work with it
//then remove it
_logstore.remove(s);
}
writefln("found: %d", l.id);
res.writeBody(s_str ~ "\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment