yoke.use("/admin", new BasicAuth(config.adminUsername, config.adminPassword));
// install body parser for /admin requests
yoke.use("/admin", new BodyParser());
// install router for admin requests
yoke.use(new Router()
.get("/admin", new Middleware() {
@Override
public void handle(final YokeRequest request, final Handler<Object> next) {
final Config.Domain domain = request.get("domain");
db.keys(domain.namespace + "&*", new Handler<Message<JsonObject>>() {
@Override
public void handle(Message<JsonObject> message) {
if (!"ok".equals(message.body().getString("status"))) {
next.handle(message.body().getString("message"));
} else {
StringBuilder keys = new StringBuilder();
JsonArray json = message.body().getArray("value");
for (int i = 0; i < json.size(); i++) {
keys.append("<li data-value=\"");
keys.append(json.get(i));
keys.append("\">");
keys.append(json.get(i));
keys.append("</li>");
}
request.put("keys", keys);
request.response().render("admin.shtml", next);
}
}
});
}
})
.get("/admin/keys", new Middleware() {
@Override
public void handle(final YokeRequest request, final Handler<Object> next) {
final Config.Domain domain = request.get("domain");
db.keys(domain.namespace + "&*", new Handler<Message<JsonObject>>() {
@Override
public void handle(Message<JsonObject> message) {
if (!"ok".equals(message.body().getString("status"))) {
next.handle(message.body().getString("message"));
} else {
request.response().end(message.body().getString("value"));
}
}
});
}
})
.get("/admin/get", new Middleware() {
@Override
public void handle(final YokeRequest request, final Handler<Object> next) {
final Config.Domain domain = request.get("domain");
String key = request.params().get("key");
if (key == null) {
request.response().end("Missing key");
return;
}
db.get(domain.namespace + "&" + key, new Handler<Message<JsonObject>>() {
@Override
public void handle(Message<JsonObject> message) {
if (!"ok".equals(message.body().getString("status"))) {
next.handle(message.body().getString("message"));
} else {
request.response().setContentType("application/json");
// TODO: escape
request.response().end("\"" + message.body().getString("value") + "\"");
}
}
});
}
})
.post("/admin/set", new Middleware() {
@Override
public void handle(final YokeRequest request, final Handler<Object> next) {
final Config.Domain domain = request.get("domain");
MultiMap body = request.formAttributes();
String key = body.get("key");
String value = body.get("value");
if (key == null) {
request.response().end("Missing key");
return;
}
if (value == null) {
request.response().end("Missing value");
return;
}
db.set(domain.namespace + "&" + key, value, new Handler<Message<JsonObject>>() {
@Override
public void handle(Message<JsonObject> message) {
if (!"ok".equals(message.body().getString("status"))) {
next.handle(message.body().getString("message"));
} else {
request.response().setContentType("application/json");
request.response().end("\"OK\"");
}
}
});
}
})
.post("/admin/unset", new Middleware() {
@Override
public void handle(final YokeRequest request, final Handler<Object> next) {
final Config.Domain domain = request.get("domain");
MultiMap body = request.formAttributes();
String key = body.get("key");
if (key == null) {
request.response().end("Missing key");
return;
}
db.del(domain.namespace + "&" + key, new Handler<Message<JsonObject>>() {
@Override
public void handle(Message<JsonObject> message) {
if (!"ok".equals(message.body().getString("status"))) {
next.handle(message.body().getString("message"));
} else {
request.response().setContentType("application/json");
request.response().end("\"OK\"");
}
}
});
}
})
.get("/admin/export", new Middleware() {
@Override
public void handle(final YokeRequest request, final Handler<Object> next) {
final Config.Domain domain = request.get("domain");
db.keys(domain.namespace + "&*", new Handler<Message<JsonObject>>() {
@Override
public void handle(Message<JsonObject> message) {
if (!"ok".equals(message.body().getString("status"))) {
next.handle(message.body().getString("message"));
} else {
// need to iterate all json array elements and get from redis
new AsyncIterator<Object>(message.body().getArray("value")) {
final JsonArray buffer = new JsonArray();
@Override
public void handle(Object o) {
if (hasNext()) {
final String key = (String) o;
db.get(domain.namespace + "&" + key, new Handler<Message<JsonObject>>() {
@Override
public void handle(Message<JsonObject> message) {
if (!"ok".equals(message.body().getString("status"))) {
next.handle(message.body().getString("message"));
} else {
JsonObject json = new JsonObject();
json.putString("key", key);
json.putString("value", message.body().getString("value"));
buffer.addObject(json);
next();
}
}
});
} else {
YokeResponse response = request.response();
String filename = System.currentTimeMillis() + "_export.kit";
response.putHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
response.end(buffer);
}
}
};
}
}
});
}
})
.post("/admin/import", new Middleware() {
@Override
public void handle(final YokeRequest request, final Handler<Object> next) {
final Config.Domain domain = request.get("domain");
final YokeFileUpload file = request.getFile("file");
fileSystem.readFile(file.path(), new AsyncResultHandler<Buffer>() {
@Override
public void handle(AsyncResult<Buffer> read) {
// delete the temp file
file.delete();
// parse
JsonArray json = new JsonArray(read.result().toString());
// iterate
new AsyncIterator<Object>(json) {
@Override
public void handle(Object o) {
if (hasNext()) {
final JsonObject json = (JsonObject) o;
db.set(domain.namespace + "&" + json.getString("key"), json.getString("value"), new Handler<Message<JsonObject>>() {
@Override
public void handle(Message<JsonObject> message) {
if (!"ok".equals(message.body().getString("status"))) {
next.handle(message.body().getString("message"));
} else {
next();
}
}
});
} else {
request.response().redirect("/admin");
}
}
};
}
});
}
}));
// if the request fall through it is a view to render from the db
yoke.use(new Middleware() {
@Override
public void handle(final YokeRequest request, final Handler<Object> next) {
final Config.Domain domain = request.get("domain");
final String file = request.path().toLowerCase();