1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-09 12:12:35 +01:00

Improve first setup, make server load initial configuraiton

This commit is contained in:
TheArcaneBrony 2022-08-20 19:24:20 +02:00
parent 5ffaaf771e
commit 674fa8364e
No known key found for this signature in database
GPG Key ID: 32FC5AAADAD75A22
9 changed files with 875 additions and 40 deletions

2
.gitignore vendored
View File

@ -26,3 +26,5 @@ initial.json
build.json
yarn.lock
.yarn/install-state.gz

783
.yarn/releases/yarn-3.2.2.cjs vendored Executable file

File diff suppressed because one or more lines are too long

View File

@ -1 +1,3 @@
nodeLinker: node-modules
yarnPath: .yarn/releases/yarn-3.2.2.cjs

69
scripts/first_setup.js Normal file → Executable file
View File

@ -1,3 +1,4 @@
#!/usr/bin/node
const path = require("path");
const fs = require("fs");
const { stdout, exit } = require("process");
@ -142,9 +143,15 @@ async function main() {
execIn("npx yarn set version stable", process.cwd());
console.log(" ==> Installing base packages");
execIn("npx --yes yarn install", process.cwd(), { stdio: "inherit" });
if (data.extra_pkgs.length > 0) {
console.log(" ==> Checking dependencies...");
checkCompilers();
if (data.extra_pkgs.includes("canvas")) checkCanvasDeps();
if (data.extra_pkgs.includes("bcrypt")) checkBcryptDeps();
console.log(` ==> Installing extra packages: ${data.extra_pkgs.join(", ")}...`);
execIn(`npx --yes yarn add -O ${data.extra_pkgs.join(" ")}`, process.cwd(), { stdio: "inherit" });
console.log(` ==> Installing extra packages: ${data.extra_pkgs.join(", ")}...`);
execIn(`npx --yes yarn add -O ${data.extra_pkgs.join(" ")}`, process.cwd(), { stdio: "inherit" });
}
console.log("==> Building...");
execIn("npx --yes yarn run build", process.cwd(), { stdio: "inherit" });
@ -208,7 +215,7 @@ function printTitle(input) {
console.log();
}
async function ask(question) {
return new Promise((resolve, reject) => {
return new Promise((resolve, _reject) => {
return rl.question(question, (answer) => {
resolve(answer);
});
@ -220,3 +227,59 @@ async function ask(question) {
function BitFlag(int) {
return 1n << BigInt(int);
}
function checkCanvasDeps() {
if (
!(
checkDep("pixman", "/usr/include/pixman-1/pixman.h") &&
checkDep("pixman", "/usr/lib/libpixman-1.so") &&
checkDep("cairo", "/usr/include/cairo/cairo.h") &&
checkDep("cairo", "/usr/lib/libcairo.so") &&
checkDep("pango", "/usr/include/pango-1.0/pango/pangocairo.h") &&
checkDep("pango", "/usr/lib/libpango-1.0.so") &&
checkDep("pkgconfig", "/usr/bin/pkg-config")
)
) {
console.log("Canvas requires the following dependencies to be installed: pixman, cairo, pango, pkgconfig");
exit(1);
}
}
function checkBcryptDeps() {
/*if (!(checkDep("bcrypt", "/usr/include/bcrypt.h") && checkDep("bcrypt", "/usr/lib/libbcrypt.so"))) {
console.log("Bcrypt requires the following dependencies to be installed: bcrypt");
exit(1);
}*/
//TODO: check if required
}
function checkCompilers() {
//check for gcc, grep, make, python-is-python3
if (
!(
checkDep("gcc", "/usr/bin/gcc") &&
checkDep("grep", "/usr/bin/grep") &&
checkDep("make", "/usr/bin/make") &&
checkDep("python3", "/usr/bin/python3")
)
) {
console.log("Compiler requirements not met. Please install the following: gcc, grep, make, python3");
exit(1);
}
//check if /usr/bin/python is a symlink to /usr/bin/python3
if (!fs.lstatSync("/usr/bin/python").isSymbolicLink()) {
console.log("/usr/bin/python is not a symlink. Please make sure it is a symlink to /usr/bin/python3");
if (fs.existsSync("/usr/bin/python3")) {
console.log("Hint: sudo ln -s /usr/bin/python3 /usr/bin/python");
}
exit(1);
}
}
function checkDep(name, path, message) {
if (!fs.existsSync(path)) {
console.log(`${name} not found at ${path}! Installation of some modules may fail!`);
console.log(message ?? `Please consult your distro's manual for installation instructions.`);
}
return fs.existsSync(path);
}

View File

@ -38,34 +38,6 @@ async function main() {
server.listen(port);
await getOrInitialiseDatabase();
await Config.init();
// only set endpointPublic, if not already set
await Config.set({
cdn: {
endpointClient: "${location.host}",
endpointPrivate: `http://localhost:${port}`
},
gateway: {
endpointClient: '${location.protocol === "https:" ? "wss://" : "ws://"}${location.host}',
endpointPrivate: `ws://localhost:${port}`,
...(!Config.get().gateway.endpointPublic && {
endpointPublic: `ws://localhost:${port}`
})
}
// regions: {
// default: "fosscord",
// useDefaultAsOptimal: true,
// available: [
// {
// id: "fosscord",
// name: "Fosscord",
// endpoint: "127.0.0.1:3001",
// vip: false,
// custom: false,
// deprecated: false,
// },
// ],
// },
} as any);
//Sentry
if (Config.get().sentry.enabled) {

View File

@ -94,11 +94,8 @@ export default function TestClient(app: Application) {
}
function applyEnv(html: string): string {
const CDN_ENDPOINT = (Config.get().cdn.endpointClient || Config.get()?.cdn.endpointPublic || process.env.CDN || "").replace(
/(https?)?(:\/\/?)/g,
""
);
const GATEWAY_ENDPOINT = Config.get().gateway.endpointClient || Config.get()?.gateway.endpointPublic || process.env.GATEWAY || "";
const CDN_ENDPOINT = (Config.get()?.cdn.endpointPublic || process.env.CDN || "").replace(/(https?)?(:\/\/?)/g, "");
const GATEWAY_ENDPOINT = Config.get()?.gateway.endpointPublic || process.env.GATEWAY || "";
if (CDN_ENDPOINT) {
html = html.replace(/CDN_HOST: .+/, `CDN_HOST: \`${CDN_ENDPOINT}\`,`);

View File

@ -19,8 +19,14 @@ import {
} from ".";
export class ConfigValue {
gateway: EndpointConfiguration = new EndpointConfiguration();
cdn: EndpointConfiguration = new EndpointConfiguration();
gateway: EndpointConfiguration = {
endpointPublic: '${location.protocol === "https:" ? "wss://" : "ws://"}${location.host}',
endpointPrivate: `ws://localhost:3001`
};
cdn: EndpointConfiguration = {
endpointPublic: "${location.host}",
endpointPrivate: `http://localhost:3001`
};
api: ApiConfiguration = new ApiConfiguration();
general: GeneralConfiguration = new GeneralConfiguration();
limits: LimitsConfiguration = new LimitsConfiguration();

View File

@ -1,5 +1,4 @@
export class EndpointConfiguration {
endpointClient: string | null = null;
endpointPrivate: string | null = null;
endpointPublic: string | null = null;
}

View File

@ -1,4 +1,5 @@
import fs from "fs";
import path from "path";
import { OrmUtils } from ".";
import { ConfigValue } from "../config";
import { ConfigEntity } from "../entities/Config";
@ -24,11 +25,21 @@ export const Config = {
if (process.env.CONFIG_PATH)
try {
const overrideConfig = JSON.parse(fs.readFileSync(overridePath, { encoding: "utf8" }));
config = overrideConfig.merge(config);
config = OrmUtils.mergeDeep(config, overrideConfig);
} catch (error) {
fs.writeFileSync(overridePath, JSON.stringify(config, null, 4));
}
if (fs.existsSync(path.join(process.cwd(), "initial.json")))
try {
console.log("[Config] Found initial configuration, merging...");
const overrideConfig = JSON.parse(fs.readFileSync(path.join(process.cwd(), "initial.json"), { encoding: "utf8" }));
config = OrmUtils.mergeDeep(config, overrideConfig);
fs.rmSync(path.join(process.cwd(), "initial.json"));
} catch (error) {
fs.writeFileSync(path.join(process.cwd(), "failed.conf"), JSON.stringify(config, null, 4));
}
return this.set(config);
},
get: function get() {