diff options
| author | 2019-02-27 01:12:44 -0500 | |
|---|---|---|
| committer | 2019-02-27 01:12:44 -0500 | |
| commit | bae8289d41bd5fc5a49d39505b3111f10e2e8448 (patch) | |
| tree | c0501396163623b53e4f86461bcc85d7cbe60ea0 | |
| parent | Switch to Vibe's Default Main (diff) | |
Cleanup
- stop using web development generation, it doesn't work very well
- rework health check
- use HTTPStatus
- move routing away from the implementation
| -rw-r--r-- | source/app.d | 13 | ||||
| -rw-r--r-- | source/web.d | 93 |
2 files changed, 56 insertions, 50 deletions
diff --git a/source/app.d b/source/app.d index c6ac6c5..f9ecc12 100644 --- a/source/app.d +++ b/source/app.d @@ -1,8 +1,9 @@ -import brpaste; +import brpaste.web; import vibe.d; shared static this() { + // HTTP settings auto settings = new HTTPServerSettings; settings.port = 8080; settings.bindAddresses = []; @@ -11,7 +12,15 @@ shared static this() { readOption("port|p", &settings.port, "Sets the port to listen on [8080]"); if(settings.bindAddresses.empty) settings.bindAddresses = [ "127.0.0.1", "::1" ]; + // setup router auto router = new URLRouter; - router.registerWebInterface(new BRPaste); + router.match(HTTPMethod.REPORT, "/health", &health); + router.get("/health", &health); + + router.get("/", staticTemplate!"index.dt"); + router.post("/", &post); + router.get("/:id", &id); + router.get("/raw/:id", &rawId); + listenHTTP(settings, router); } diff --git a/source/web.d b/source/web.d index bfabda2..a8ddc79 100644 --- a/source/web.d +++ b/source/web.d @@ -5,60 +5,57 @@ import brpaste.hash; import vibe.vibe; RedisDatabase client; -shared static this() { - string path = "redis://127.0.0.1"; - readOption("redis|r", &path, "The URL to use to connect to redis"); - URL redis = path; - client = connectRedisDB(redis); -} -class BRPaste { - @method(HTTPMethod.REPORT) - @path("/health") - void health(HTTPServerResponse res) { - import std.array; - res.statusCode = 200; - auto app = appender!string; +void id(HTTPServerRequest req, HTTPServerResponse res) { + string id = req.params["id"]; + string language = "none"; + // TODO: rewrite the next two lines once #2273 is resolved + if (req.query.length > 0) language = req.query.byKey.front; + enforceHTTP(client.exists(id), HTTPStatus.notFound, "No paste under " ~ id ~ "."); - // is Redis healthy? - FIXME: stack traces over HTTP are fun, I guess - import std.random; - long val = uniform!uint; - auto ech = client.client.echo!(long, long)(val); - if(val != ech) { - res.statusCode = 500; - app.put("Redis: failed."); - } else app.put("Redis: pass."); + auto data = client.get(id); + render!("code.dt", data, language)(res); +} - res.writeBody(app.data); - } +void rawId(HTTPServerRequest req, HTTPServerResponse res) { + string id = req.params["id"]; + enforceHTTP(client.exists(id), HTTPStatus.notFound, "No paste under " ~ id ~ "."); - void index() { - render!("index.dt"); - } + auto data = client.get(id); + res.contentType = "text/plain"; + res.writeBody(data); +} - @path("/:id") - void getId(string _id) { - if (!client.exists(_id)) throw new HTTPStatusException(404); - string language = "none"; - // TODO: rewrite the next two lines once #2273 is resolved - auto req = request; - if (req.query.length > 0) language = req.query.byKey.front; - auto data = client.get(_id); - render!("code.dt", data, language); - } +void post(HTTPServerRequest req, HTTPServerResponse res) { + enforceHTTP("data" in req.form, HTTPStatus.badRequest, "Missing data field."); + auto data = req.form["data"]; - @path("/raw/:id") - void getRawId(HTTPServerResponse res, string _id) { - if (!client.exists(_id)) throw new HTTPStatusException(404); - auto val = client.get(_id); - res.contentType = "text/plain"; - res.writeBody(val); - } + auto hash = data.hash; + client.set(hash, data); + res.statusCode = HTTPStatus.created; + res.writeBody(hash); +} - void post(string data) { - auto hash = data.hash; - client.set(hash, data); - status(201); - response.writeBody(hash); +void health(HTTPServerRequest req, HTTPServerResponse res) { + res.statusCode = HTTPStatus.noContent; + scope(exit) res.writeBody(""); + + // Redis + try { + client.client.ping; + } catch (Exception e) { + logCritical("Redis is down!"); + res.statusCode = HTTPStatus.serviceUnavailable; + res.statusPhrase = "Backend Storage Unavailable"; + res.headers["Retry-After"] = "60"; } } + +shared static this() { + // setup redis + string path = "redis://127.0.0.1"; + readOption("redis|r", &path, "The URL to use to connect to redis"); + URL redis = path; + client = connectRedisDB(redis); +} + |
