Skip to content

Instantly share code, notes, and snippets.

@yannick
Created April 27, 2015 00:50
Show Gist options
  • Select an option

  • Save yannick/6e74437d2e40fe37d62d to your computer and use it in GitHub Desktop.

Select an option

Save yannick/6e74437d2e40fe37d62d to your computer and use it in GitHub Desktop.
import vibe.d;
import std.format : format;
import core.atomic : atomicOp;
import std.stdio;
shared int serial;
shared shared(Log)*[int] logstore;
shared struct Log
{
int s;
string data;
}
void routea(HTTPServerRequest req, HTTPServerResponse res)
{
auto s = atomicOp!"+="(serial, 1);
auto log = new shared Log(s, "test");
logstore[s] = 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);
auto l = logstore[s];
writefln("found: %d", l.s);
logstore.remove(s);
res.writeBody(s_str ~ "\n");
}
shared static this()
{
auto router = new URLRouter;
router.get("/a", &routea);
router.get("/b", &routeb);
auto settings = new HTTPServerSettings;
settings.port = 8080;
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:8080/ in your browser.");
}
void hello(HTTPServerRequest req, HTTPServerResponse res)
{
res.writeBody("Hello, World!");
}
{
"name": "bug-shared",
"description": "A simple vibe.d server application.",
"copyright": "Copyright © 2015, yannick",
"authors": ["yannick"],
"dependencies": {
"vibe-d": "~>0.7.19"
},
"versions": ["VibeDefaultMain"]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment