113 lines
3.1 KiB
TypeScript
113 lines
3.1 KiB
TypeScript
import * as Router from "koa-router";
|
|
import Settings from "../../settings";
|
|
import getForm from "../helper/form";
|
|
import Logging from "@hibas123/nodelogging";
|
|
import getTable from "../helper/table";
|
|
import { BadRequestError } from "../helper/errors";
|
|
import { DatabaseManager } from "../../database/database";
|
|
|
|
const AdminRoute = new Router();
|
|
|
|
AdminRoute.use((ctx, next) => {
|
|
//TODO: Check permission
|
|
return next();
|
|
})
|
|
|
|
AdminRoute.get("/settings", async ctx => {
|
|
let res = await new Promise<string[][]>((yes, no) => {
|
|
const stream = Settings.db.createReadStream({
|
|
keys: true,
|
|
values: true,
|
|
valueAsBuffer: true
|
|
});
|
|
let res = [["key", "value"]];
|
|
stream.on("data", ({ key, value }) => {
|
|
res.push([key, value]);
|
|
})
|
|
|
|
stream.on("error", no);
|
|
stream.on("end", () => yes(res))
|
|
})
|
|
|
|
if (ctx.query.view) {
|
|
return getTable("Settings", res, ctx);
|
|
} else {
|
|
ctx.body = res;
|
|
}
|
|
})
|
|
|
|
AdminRoute.get("/data", async ctx => {
|
|
const { database } = ctx.query;
|
|
let db = DatabaseManager.getDatabase(database);
|
|
if (!db)
|
|
throw new BadRequestError("Database not found");
|
|
let res = await new Promise<string[][]>((yes, no) => {
|
|
|
|
const stream = db.level.createReadStream({
|
|
keys: true,
|
|
values: true,
|
|
valueAsBuffer: true
|
|
});
|
|
let res = [["key", "value"]];
|
|
stream.on("data", ({ key, value }) => {
|
|
res.push([key, value]);
|
|
})
|
|
|
|
stream.on("error", no);
|
|
stream.on("end", () => yes(res))
|
|
})
|
|
|
|
if (ctx.query.view) {
|
|
return getTable("Data from " + database, res, ctx);
|
|
} else {
|
|
ctx.body = res;
|
|
}
|
|
})
|
|
|
|
AdminRoute
|
|
.get("/database", ctx => {
|
|
const isFull = ctx.query.full === "true" || ctx.query.full === "1";
|
|
let res;
|
|
if (isFull) {
|
|
//TODO: Better than JSON.parse / JSON.stringify
|
|
res = Array.from(DatabaseManager.databases.entries()).map(([name, config]) => ({ name, ...(JSON.parse(JSON.stringify(config))) }));
|
|
} else {
|
|
res = Array.from(DatabaseManager.databases.keys());
|
|
}
|
|
|
|
if (ctx.query.view) {
|
|
return getTable("Databases" + (isFull ? "" : " small"), res, ctx);
|
|
} else {
|
|
ctx.body = res;
|
|
}
|
|
})
|
|
.post("/database", async ctx => {
|
|
const { name, rules, publickey, accesskey } = ctx.request.body;
|
|
|
|
if (!name)
|
|
throw new BadRequestError("Name must be set!");
|
|
|
|
let db = DatabaseManager.getDatabase(name);
|
|
if (!db)
|
|
db = await DatabaseManager.addDatabase(name);
|
|
|
|
if (publickey)
|
|
await db.setPublicKey(publickey);
|
|
|
|
if (rules)
|
|
await db.setRules(rules);
|
|
db
|
|
if (accesskey)
|
|
await db.setAccessKey(accesskey);
|
|
|
|
ctx.body = "Success";
|
|
})
|
|
|
|
AdminRoute.get("/database/new", getForm("/v1/admin/database", "New/Change Database", {
|
|
name: { label: "Name", type: "text", },
|
|
accesskey: { label: "Access Key", type: "text" },
|
|
rules: { label: "Rules", type: "textarea", value: `{\n ".write": true, \n ".read": true \n}` },
|
|
publickey: { label: "Public Key", type: "textarea" }
|
|
}))
|
|
|
|
export default AdminRoute; |