Adding Dockerfile and build script

This commit is contained in:
Fabian
2019-10-01 17:45:38 +02:00
parent 65588f4b98
commit 405e589328
11 changed files with 98 additions and 45 deletions

View File

@ -1,16 +1,20 @@
import * as ini from "ini";
import * as fs from "fs";
import Logging from "@hibas123/nodelogging";
import * as dotenv from "dotenv";
interface IConfig {
general: {
port: string;
admin: string;
access_log: boolean;
dev: boolean
}
port: number;
admin: string;
access_log: boolean;
dev: boolean
}
const config = ini.parse(fs.readFileSync("config.ini").toString()) as IConfig;
Logging.debug("Config:", config);
const config: IConfig = {
port: Number(process.env.PORT),
access_log: (process.env.ACCESS_LOG || "").toLowerCase() === "true",
admin: process.env.ADMIN_KEY,
dev: (process.env.DEV || "").toLowerCase() === "true"
}
dotenv.config()
export default config;

View File

@ -13,7 +13,7 @@ export class DatabaseManager {
let databases = await Settings.getDatabases();
databases.forEach(dbconfig => {
let db = new Database(dbconfig.name, dbconfig.accesskey, dbconfig.rules, dbconfig.publickey);
let db = new Database(dbconfig.name, dbconfig.accesskey, dbconfig.rules, dbconfig.publickey, dbconfig.rootkey);
this.databases.set(dbconfig.name, db);
})
}
@ -68,7 +68,7 @@ export class Database {
}
}
constructor(public name: string, public accesskey?: string, rawRules?: string, public publickey?: string) {
constructor(public name: string, public accesskey?: string, rawRules?: string, public publickey?: string, public rootkey?: string) {
if (rawRules)
this.rules = new Rules(rawRules);
}
@ -84,11 +84,17 @@ export class Database {
this.accesskey = key;
}
async setRootKey(key: string) {
await Settings.setDatabaseAccessKey(this.name, key);
this.accesskey = key;
}
async setPublicKey(key: string) {
await Settings.setDatabasePublicKey(this.name, key);
this.publickey = key;
}
getQuery(path: string[]) {
return new Query(this, path);
}

View File

@ -1,18 +1,14 @@
import Logging from "@hibas123/nodelogging";
// import getLevelDB from "./storage";
// import Settings from "./settings";
import Web from "./web";
import config from "./config";
import { DatabaseManager } from "./database/database";
import { createServer } from "http";
import { ConnectionManager } from "./connection";
// Logging.debug(getLevelDB("system"));
DatabaseManager.init().then(() => {
const http = createServer(Web.callback());
ConnectionManager.bind(http);
const port = Number(config.general.port) || 5013;
const port = config.port || 5000;
http.listen(port, () => Logging.log("Listening on port:", port))
}).catch(err => {
Logging.error(err);

View File

@ -1,7 +1,4 @@
import getLevelDB, { resNull } from "./storage";
import Encoder, { DataTypes } from "@hibas123/binary-encoder";
import Logging from "@hibas123/nodelogging";
import { Lock } from "@hibas123/utils";
interface IDatabaseConfig {
@ -9,19 +6,9 @@ interface IDatabaseConfig {
publickey?: string;
rules?: string;
accesskey?: string;
rootkey?: string;
}
// const DatabaseEncoder = new Encoder<IDatabaseConfig>({
// publickey: {
// index: 1,
// type: DataTypes.STRING,
// allowNull: true
// },
// rules: {
// index: 2,
// type: DataTypes.STRING
// }
// });
class SettingComponent {
db = getLevelDB("_server");
@ -54,7 +41,8 @@ class SettingComponent {
await Promise.all([
this.getField(database, "publickey").then(r => res.publickey = r),
this.getField(database, "rules").then(r => res.rules = r),
this.getField(database, "accesskey").then(r => res.accesskey = r)
this.getField(database, "accesskey").then(r => res.accesskey = r),
this.getField(database, "rootkey").then(r => res.rootkey = r)
])
return res;
})))
@ -107,6 +95,14 @@ class SettingComponent {
lock.release();
}
async setDatabaseRootKey(name: string, accesskey: string) {
const lock = await this.databaseLock.getLock();
await this.setField(name, "rootkey", accesskey);
lock.release();
}
async deleteDatabase(name: string) {
const lock = await this.databaseLock.getLock();
@ -119,6 +115,7 @@ class SettingComponent {
.del(pref + ":publickey")
.del(pref + ":rules")
.del(pref + ":accesskey")
.del(pref + ":rootkey")
.write();
lock.release();

View File

@ -42,7 +42,7 @@ const cache = new Map<string, Handlebars.TemplateDelegate>();
export default function getTemplate(name: string) {
let tl: Handlebars.TemplateDelegate;
if (!config.general.dev)
if (!config.dev)
tl = cache.get(name);
if (!tl) {

View File

@ -2,9 +2,9 @@ import { LoggingBase } from "@hibas123/nodelogging";
import { Context } from "koa";
import config from "../../config";
const route_logging = new LoggingBase({ name: "access", files: { errorfile: null }, console: config.general.dev })
const route_logging = new LoggingBase({ name: "access", files: { errorfile: null }, console: config.dev })
const RequestLog = async (ctx: Context, next) => {
if (!config.general.access_log) return next();
if (!config.access_log) return next();
let start = process.hrtime()
let to = false
let print = () => {

View File

@ -13,7 +13,7 @@ const AdminRoute = new Router();
AdminRoute.use(async (ctx, next) => {
const { key } = ctx.query;
if (key !== config.general.admin)
if (key !== config.admin)
throw new NoPermissionError("No permission!");
return next();
})
@ -87,7 +87,7 @@ AdminRoute
}
})
.post("/database", async ctx => {
const { name, rules, publickey, accesskey } = ctx.request.body;
const { name, rules, publickey, accesskey, rootkey } = ctx.request.body;
if (!name)
throw new BadRequestError("Name must be set!");
@ -101,16 +101,22 @@ AdminRoute
if (rules)
await db.setRules(rules);
db
if (accesskey)
await db.setAccessKey(accesskey);
if (rootkey)
await db.setRootKey(rootkey);
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" },
rootkey: { label: "Root access key", type: "text" },
rules: { label: "Rules", type: "textarea", value: `{\n ".write": true, \n ".read": true \n}` },
publickey: { label: "Public Key", type: "textarea" }
}))