From e5276e27651ed2ba0715e29d141e7f4550070507 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Thu, 23 Nov 2023 20:16:40 +0100 Subject: [PATCH] [backend] Add environment variables to configure config file location, files directory and custom assets directory --- gulpfile.js | 3 +- locales/index.js | 5 ++-- packages/backend/src/config/load.ts | 30 ++++++++++--------- packages/backend/src/config/types.ts | 1 + .../src/services/drive/internal-storage.ts | 11 ++----- 5 files changed, 25 insertions(+), 25 deletions(-) diff --git a/gulpfile.js b/gulpfile.js index 7220d8421..86c9920f3 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -10,6 +10,7 @@ const cssnano = require("gulp-cssnano"); const locales = require("./locales"); const meta = require("./package.json"); +const customDir = process.env.ICESHRIMP_CUSTOM_DIR ?? "./custom"; gulp.task("copy:backend:views", () => gulp @@ -19,7 +20,7 @@ gulp.task("copy:backend:views", () => gulp.task("copy:backend:custom", () => gulp - .src("./custom/assets/**/*") + .src(`${customDir}/assets/**/*`) .pipe(gulp.dest("./packages/backend/assets/")), ); diff --git a/locales/index.js b/locales/index.js index 8a1368114..8a56c8663 100644 --- a/locales/index.js +++ b/locales/index.js @@ -6,6 +6,7 @@ const fs = require("fs"); const yaml = require("js-yaml"); const languages = []; const languages_custom = []; +const customDir = process.env.ICESHRIMP_CUSTOM_DIR ?? __dirname + "/../custom"; const merge = (...args) => 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")) { file = file.slice(0, file.indexOf(".")); languages_custom.push(file); @@ -57,7 +58,7 @@ const locales_custom = languages_custom.reduce( (a[c] = yaml.load( clean( - fs.readFileSync(`${__dirname}/../custom/locales/${c}.yml`, "utf-8"), + fs.readFileSync(`${customDir}/locales/${c}.yml`, "utf-8"), ), ) || {}), a diff --git a/packages/backend/src/config/load.ts b/packages/backend/src/config/load.ts index d2b6d3a88..1a8a7302c 100644 --- a/packages/backend/src/config/load.ts +++ b/packages/backend/src/config/load.ts @@ -7,22 +7,23 @@ import { fileURLToPath } from "node:url"; import { dirname } from "node:path"; import * as yaml from "js-yaml"; import type { Source, Mixin } from "./types.js"; - -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`; +import Path from "node:path"; 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( fs.readFileSync(`${_dirname}/../../../../built/meta.json`, "utf-8"), ); @@ -63,6 +64,7 @@ export default function load() { mixin.driveUrl = `${mixin.scheme}://${mixin.host}/files`; mixin.userAgent = `Iceshrimp/${meta.version} (${config.url})`; mixin.clientEntry = clientManifest["src/init.ts"]; + mixin.mediaDir = mediaDir ?? `${dir}/files`; if (!config.redis.prefix) config.redis.prefix = mixin.hostname; diff --git a/packages/backend/src/config/types.ts b/packages/backend/src/config/types.ts index 108a9a7e5..cfa521c97 100644 --- a/packages/backend/src/config/types.ts +++ b/packages/backend/src/config/types.ts @@ -144,6 +144,7 @@ export type Mixin = { driveUrl: string; userAgent: string; clientEntry: string; + mediaDir: string; }; export type Config = Source & Mixin; diff --git a/packages/backend/src/services/drive/internal-storage.ts b/packages/backend/src/services/drive/internal-storage.ts index bccb123be..6cb88574d 100644 --- a/packages/backend/src/services/drive/internal-storage.ts +++ b/packages/backend/src/services/drive/internal-storage.ts @@ -4,27 +4,22 @@ import { fileURLToPath } from "node:url"; import { dirname } from "node:path"; import config from "@/config/index.js"; -const _filename = fileURLToPath(import.meta.url); -const _dirname = dirname(_filename); - export class InternalStorage { - private static readonly path = Path.resolve(_dirname, "../../../../../files"); - public static resolvePath = (key: string) => - Path.resolve(InternalStorage.path, key); + Path.resolve(config.mediaDir, key); public static read(key: string) { return fs.createReadStream(InternalStorage.resolvePath(key)); } 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)); return `${config.url}/files/${key}`; } 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); return `${config.url}/files/${key}`; }