From a4de93f9763603dcb3e375e15db421e1bd9d3957 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Fri, 23 Dec 2022 22:43:56 +0800 Subject: [PATCH] WIP --- docker/dockerfile | 3 ++ docker/my.cnf | 0 server/database.js | 64 ++++++++++++++++++++++++++++---------- server/embedded-mariadb.js | 51 ++++++++++++++++++++++++++++++ 4 files changed, 101 insertions(+), 17 deletions(-) create mode 100644 docker/my.cnf create mode 100644 server/embedded-mariadb.js diff --git a/docker/dockerfile b/docker/dockerfile index 3cb27b50..acba709e 100644 --- a/docker/dockerfile +++ b/docker/dockerfile @@ -48,7 +48,10 @@ CMD ["node", "server/server.js"] ############################################ FROM release-slim AS release RUN apt update && \ + apt --yes --no-install-recommends install curl && \ + curl -LsS https://r.mariadb.com/downloads/mariadb_repo_setup | bash -s -- --mariadb-server-version="mariadb-10.11" && \ apt --yes --no-install-recommends install mariadb-server && \ + apt remove curl && \ rm -rf /var/lib/apt/lists/* && \ apt --yes autoremove diff --git a/docker/my.cnf b/docker/my.cnf new file mode 100644 index 00000000..e69de29b diff --git a/server/database.js b/server/database.js index 2544f197..9bf94f9b 100644 --- a/server/database.js +++ b/server/database.js @@ -4,6 +4,7 @@ const { setSetting, setting } = require("./util-server"); const { log, sleep } = require("../src/util"); const dayjs = require("dayjs"); const knex = require("knex"); +const path = require("path"); /** * Database & App Data Folder @@ -109,24 +110,53 @@ class Database { static async connect(testMode = false, autoloadModels = true, noLog = false) { const acquireConnectionTimeout = 120 * 1000; - const Dialect = require("knex/lib/dialects/sqlite3/index.js"); - Dialect.prototype._driver = () => require("@louislam/sqlite3"); + let dbConfig; - const knexInstance = knex({ - client: Dialect, - connection: { - filename: Database.path, - acquireConnectionTimeout: acquireConnectionTimeout, - }, - useNullAsDefault: true, - pool: { - min: 1, - max: 1, - idleTimeoutMillis: 120 * 1000, - propagateCreateError: false, - acquireTimeoutMillis: acquireConnectionTimeout, - } - }); + try { + dbConfig = fs.readFileSync(path.join(Database.dataDir, "db-config.json")); + } catch (_) { + dbConfig = { + type: "sqlite", + }; + } + + let config = {}; + + if (dbConfig.type === "sqlite") { + const Dialect = require("knex/lib/dialects/sqlite3/index.js"); + Dialect.prototype._driver = () => require("@louislam/sqlite3"); + + config = { + client: Dialect, + connection: { + filename: Database.path, + acquireConnectionTimeout: acquireConnectionTimeout, + }, + useNullAsDefault: true, + pool: { + min: 1, + max: 1, + idleTimeoutMillis: 120 * 1000, + propagateCreateError: false, + acquireTimeoutMillis: acquireConnectionTimeout, + } + }; + } else if (dbConfig === "embedded-mariadb") { + config = { + client: "mysql", + connection: { + host: "127.0.0.1", + port: 3306, + user: "your_database_user", + password: "your_database_password", + database: "kuma" + } + }; + } else { + throw new Error("Unknown Database type"); + } + + const knexInstance = knex(config); R.setup(knexInstance); diff --git a/server/embedded-mariadb.js b/server/embedded-mariadb.js new file mode 100644 index 00000000..eef78920 --- /dev/null +++ b/server/embedded-mariadb.js @@ -0,0 +1,51 @@ +const { log } = require("../src/util"); +const childProcess = require("child_process"); + +class EmbeddedMariaDB { + + static childProcess = null; + static running = false; + + static init() { + + } + + static start() { + if (this.childProcess) { + log.log("mariadb", "Already started"); + return; + } + + this.running = true; + this.emitChange("Starting cloudflared"); + this.childProcess = childProcess.spawn(this.cloudflaredPath, args); + this.childProcess.stdout.pipe(process.stdout); + this.childProcess.stderr.pipe(process.stderr); + + this.childProcess.on("close", (code) => { + this.running = false; + this.childProcess = null; + this.emitChange("Stopped cloudflared", code); + }); + + this.childProcess.on("error", (err) => { + if (err.code === "ENOENT") { + this.emitError(`Cloudflared error: ${this.cloudflaredPath} is not found`); + } else { + this.emitError(err); + } + }); + + this.childProcess.stderr.on("data", (data) => { + this.emitError(data.toString()); + }); + } + + static stop() { + if (this.childProcess) { + this.childProcess.kill("SIGINT"); + this.childProcess = null; + } + } + +}