[backend] Add environment variables to configure config file location, files directory and custom assets directory

This commit is contained in:
Laura Hausmann 2023-11-23 20:16:40 +01:00
parent bb669edb82
commit e5276e2765
Signed by: zotan
GPG key ID: D044E84C5BE01605
5 changed files with 25 additions and 25 deletions

View file

@ -10,6 +10,7 @@ const cssnano = require("gulp-cssnano");
const locales = require("./locales"); const locales = require("./locales");
const meta = require("./package.json"); const meta = require("./package.json");
const customDir = process.env.ICESHRIMP_CUSTOM_DIR ?? "./custom";
gulp.task("copy:backend:views", () => gulp.task("copy:backend:views", () =>
gulp gulp
@ -19,7 +20,7 @@ gulp.task("copy:backend:views", () =>
gulp.task("copy:backend:custom", () => gulp.task("copy:backend:custom", () =>
gulp gulp
.src("./custom/assets/**/*") .src(`${customDir}/assets/**/*`)
.pipe(gulp.dest("./packages/backend/assets/")), .pipe(gulp.dest("./packages/backend/assets/")),
); );

View file

@ -6,6 +6,7 @@ const fs = require("fs");
const yaml = require("js-yaml"); const yaml = require("js-yaml");
const languages = []; const languages = [];
const languages_custom = []; const languages_custom = [];
const customDir = process.env.ICESHRIMP_CUSTOM_DIR ?? __dirname + "/../custom";
const merge = (...args) => const merge = (...args) =>
args.reduce( args.reduce(
@ -26,7 +27,7 @@ fs.readdirSync(__dirname).forEach((file) => {
} }
}); });
fs.readdirSync(__dirname + "/../custom/locales").forEach((file) => { fs.readdirSync(`${customDir}/locales`).forEach((file) => {
if (file.includes(".yml")) { if (file.includes(".yml")) {
file = file.slice(0, file.indexOf(".")); file = file.slice(0, file.indexOf("."));
languages_custom.push(file); languages_custom.push(file);
@ -57,7 +58,7 @@ const locales_custom = languages_custom.reduce(
(a[c] = (a[c] =
yaml.load( yaml.load(
clean( clean(
fs.readFileSync(`${__dirname}/../custom/locales/${c}.yml`, "utf-8"), fs.readFileSync(`${customDir}/locales/${c}.yml`, "utf-8"),
), ),
) || {}), ) || {}),
a a

View file

@ -7,22 +7,23 @@ import { fileURLToPath } from "node:url";
import { dirname } from "node:path"; import { dirname } from "node:path";
import * as yaml from "js-yaml"; import * as yaml from "js-yaml";
import type { Source, Mixin } from "./types.js"; import type { Source, Mixin } from "./types.js";
import Path from "node:path";
const _filename = fileURLToPath(import.meta.url);
const _dirname = dirname(_filename);
/**
* Path of configuration directory
*/
const dir = `${_dirname}/../../../../.config`;
/**
* Path of configuration file
*/
const path =
process.env.NODE_ENV === "test" ? `${dir}/test.yml` : `${dir}/default.yml`;
export default function load() { export default function load() {
const _filename = fileURLToPath(import.meta.url);
const _dirname = dirname(_filename);
const dir = `${_dirname}/../../../..`;
const {
ICESHRIMP_CONFIG: configFile,
ICESHRIMP_MEDIA_DIR: mediaDir,
} = process.env;
const path =
process.env.NODE_ENV === "test"
? `${dir}/.config/test.yml`
: configFile ?? `${dir}/.config/default.yml`;
const meta = JSON.parse( const meta = JSON.parse(
fs.readFileSync(`${_dirname}/../../../../built/meta.json`, "utf-8"), fs.readFileSync(`${_dirname}/../../../../built/meta.json`, "utf-8"),
); );
@ -63,6 +64,7 @@ export default function load() {
mixin.driveUrl = `${mixin.scheme}://${mixin.host}/files`; mixin.driveUrl = `${mixin.scheme}://${mixin.host}/files`;
mixin.userAgent = `Iceshrimp/${meta.version} (${config.url})`; mixin.userAgent = `Iceshrimp/${meta.version} (${config.url})`;
mixin.clientEntry = clientManifest["src/init.ts"]; mixin.clientEntry = clientManifest["src/init.ts"];
mixin.mediaDir = mediaDir ?? `${dir}/files`;
if (!config.redis.prefix) config.redis.prefix = mixin.hostname; if (!config.redis.prefix) config.redis.prefix = mixin.hostname;

View file

@ -144,6 +144,7 @@ export type Mixin = {
driveUrl: string; driveUrl: string;
userAgent: string; userAgent: string;
clientEntry: string; clientEntry: string;
mediaDir: string;
}; };
export type Config = Source & Mixin; export type Config = Source & Mixin;

View file

@ -4,27 +4,22 @@ import { fileURLToPath } from "node:url";
import { dirname } from "node:path"; import { dirname } from "node:path";
import config from "@/config/index.js"; import config from "@/config/index.js";
const _filename = fileURLToPath(import.meta.url);
const _dirname = dirname(_filename);
export class InternalStorage { export class InternalStorage {
private static readonly path = Path.resolve(_dirname, "../../../../../files");
public static resolvePath = (key: string) => public static resolvePath = (key: string) =>
Path.resolve(InternalStorage.path, key); Path.resolve(config.mediaDir, key);
public static read(key: string) { public static read(key: string) {
return fs.createReadStream(InternalStorage.resolvePath(key)); return fs.createReadStream(InternalStorage.resolvePath(key));
} }
public static saveFromPath(key: string, srcPath: string) { public static saveFromPath(key: string, srcPath: string) {
fs.mkdirSync(InternalStorage.path, { recursive: true }); fs.mkdirSync(config.mediaDir, { recursive: true });
fs.copyFileSync(srcPath, InternalStorage.resolvePath(key)); fs.copyFileSync(srcPath, InternalStorage.resolvePath(key));
return `${config.url}/files/${key}`; return `${config.url}/files/${key}`;
} }
public static saveFromBuffer(key: string, data: Buffer) { public static saveFromBuffer(key: string, data: Buffer) {
fs.mkdirSync(InternalStorage.path, { recursive: true }); fs.mkdirSync(config.mediaDir, { recursive: true });
fs.writeFileSync(InternalStorage.resolvePath(key), data); fs.writeFileSync(InternalStorage.resolvePath(key), data);
return `${config.url}/files/${key}`; return `${config.url}/files/${key}`;
} }