This commit is contained in:
Louis Lam 2022-12-23 22:43:56 +08:00
parent 5e976afb27
commit a4de93f976
4 changed files with 101 additions and 17 deletions

View file

@ -48,7 +48,10 @@ CMD ["node", "server/server.js"]
############################################ ############################################
FROM release-slim AS release FROM release-slim AS release
RUN apt update && \ 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 --yes --no-install-recommends install mariadb-server && \
apt remove curl && \
rm -rf /var/lib/apt/lists/* && \ rm -rf /var/lib/apt/lists/* && \
apt --yes autoremove apt --yes autoremove

0
docker/my.cnf Normal file
View file

View file

@ -4,6 +4,7 @@ const { setSetting, setting } = require("./util-server");
const { log, sleep } = require("../src/util"); const { log, sleep } = require("../src/util");
const dayjs = require("dayjs"); const dayjs = require("dayjs");
const knex = require("knex"); const knex = require("knex");
const path = require("path");
/** /**
* Database & App Data Folder * Database & App Data Folder
@ -109,10 +110,23 @@ class Database {
static async connect(testMode = false, autoloadModels = true, noLog = false) { static async connect(testMode = false, autoloadModels = true, noLog = false) {
const acquireConnectionTimeout = 120 * 1000; const acquireConnectionTimeout = 120 * 1000;
let dbConfig;
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"); const Dialect = require("knex/lib/dialects/sqlite3/index.js");
Dialect.prototype._driver = () => require("@louislam/sqlite3"); Dialect.prototype._driver = () => require("@louislam/sqlite3");
const knexInstance = knex({ config = {
client: Dialect, client: Dialect,
connection: { connection: {
filename: Database.path, filename: Database.path,
@ -126,7 +140,23 @@ class Database {
propagateCreateError: false, propagateCreateError: false,
acquireTimeoutMillis: acquireConnectionTimeout, 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); R.setup(knexInstance);

View file

@ -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;
}
}
}