diff options
Diffstat (limited to 'source/web.d')
| -rw-r--r-- | source/web.d | 93 |
1 files changed, 45 insertions, 48 deletions
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); +} + |
