diff --git a/neko/index.js b/neko/index.js index 54ceecb36..ebfc5d97f 100644 --- a/neko/index.js +++ b/neko/index.js @@ -266,9 +266,11 @@ if (!nativeBinding) { } const { + EnvConfig, + readEnvironmentConfig, + readServerConfig, stringToAcct, acctToString, - config, getFullApAccount, isSelfHost, extractHost, @@ -286,9 +288,11 @@ const { nativeInitIdGenerator, } = nativeBinding; +module.exports.EnvConfig = EnvConfig; +module.exports.readEnvironmentConfig = readEnvironmentConfig; +module.exports.readServerConfig = readServerConfig; module.exports.stringToAcct = stringToAcct; module.exports.acctToString = acctToString; -module.exports.config = config; module.exports.getFullApAccount = getFullApAccount; module.exports.isSelfHost = isSelfHost; module.exports.extractHost = extractHost; diff --git a/packages/backend/native-utils/src/config/environment.rs b/packages/backend/native-utils/src/config/environment.rs new file mode 100644 index 000000000..b38d21e99 --- /dev/null +++ b/packages/backend/native-utils/src/config/environment.rs @@ -0,0 +1,28 @@ +#[cfg_attr(feature = "napi", napi_derive::napi)] +pub struct EnvConfig { + pub only_queue: bool, + pub only_server: bool, + pub no_daemons: bool, + pub disable_clustering: bool, + pub verbose: bool, + pub with_log_time: bool, + pub quiet: bool, + pub slow: bool, +} + +#[cfg_attr(feature = "napi", napi_derive::napi)] +pub fn read_environment_config() -> EnvConfig { + let node_env = std::env::var("NODE_ENV").unwrap_or_default().to_lowercase(); + let is_testing = node_env == "test" || node_env == "testing"; + + EnvConfig { + only_queue: std::env::var("MK_ONLY_QUEUE").is_ok(), + only_server: std::env::var("MK_ONLY_SERVER").is_ok(), + no_daemons: is_testing || std::env::var("MK_NO_DAEMONS").is_ok(), + disable_clustering: is_testing || std::env::var("MK_DISABLE_CLUSTERING").is_ok(), + verbose: std::env::var("MK_VERBOSE").is_ok(), + with_log_time: std::env::var("MK_WITH_LOG_TIME").is_ok(), + quiet: is_testing || std::env::var("MK_QUIET").is_ok(), + slow: std::env::var("MK_SLOW").is_ok(), + } +} diff --git a/packages/backend/native-utils/src/config/mod.rs b/packages/backend/native-utils/src/config/mod.rs new file mode 100644 index 000000000..b708f2b26 --- /dev/null +++ b/packages/backend/native-utils/src/config/mod.rs @@ -0,0 +1,2 @@ +pub mod environment; +pub mod server; diff --git a/packages/backend/native-utils/src/util/config.rs b/packages/backend/native-utils/src/config/server.rs similarity index 95% rename from packages/backend/native-utils/src/util/config.rs rename to packages/backend/native-utils/src/config/server.rs index ccf14816e..f9adc54d3 100644 --- a/packages/backend/native-utils/src/util/config.rs +++ b/packages/backend/native-utils/src/config/server.rs @@ -6,7 +6,7 @@ use std::fs; #[derive(Debug, PartialEq, Deserialize)] #[serde(rename_all = "camelCase")] #[cfg_attr(feature = "napi", napi_derive::napi(object))] -pub struct Config { +pub struct ServerConfig { pub url: String, pub db: DbConfig, pub redis: RedisConfig, @@ -46,7 +46,7 @@ pub struct TlsConfig { } #[cfg_attr(feature = "napi", napi_derive::napi)] -pub fn config() -> Config { +pub fn read_server_config() -> ServerConfig { let cwd = env::current_dir().unwrap(); let yml = fs::File::open(cwd.join("../../.config/default.yml")) .expect("Failed to open '.config/default.yml'"); diff --git a/packages/backend/native-utils/src/lib.rs b/packages/backend/native-utils/src/lib.rs index f18e69a48..9e0bd3ece 100644 --- a/packages/backend/native-utils/src/lib.rs +++ b/packages/backend/native-utils/src/lib.rs @@ -1,3 +1,4 @@ +pub mod config; pub mod database; pub mod macros; pub mod model; diff --git a/packages/backend/native-utils/src/util/convert_host.rs b/packages/backend/native-utils/src/util/convert_host.rs index 030228971..d69201f6c 100644 --- a/packages/backend/native-utils/src/util/convert_host.rs +++ b/packages/backend/native-utils/src/util/convert_host.rs @@ -1,11 +1,11 @@ -use crate::util::config::config; +use crate::config::server::read_server_config; use idna; use url::Url; #[cfg_attr(feature = "napi", napi_derive::napi)] pub fn get_full_ap_account(username: String, host: Option) -> String { if host.is_none() { - format!("{}@{}", username, extract_host(config().url)) + format!("{}@{}", username, extract_host(read_server_config().url)) } else { format!("{}@{}", username, to_puny(host.unwrap())) } @@ -14,7 +14,7 @@ pub fn get_full_ap_account(username: String, host: Option) -> String { #[cfg_attr(feature = "napi", napi_derive::napi)] pub fn is_self_host(host: Option) -> bool { if let Some(x) = host { - extract_host(config().url) == to_puny(x) + extract_host(read_server_config().url) == to_puny(x) } else { true } diff --git a/packages/backend/native-utils/src/util/mod.rs b/packages/backend/native-utils/src/util/mod.rs index 441b0325c..79c10c951 100644 --- a/packages/backend/native-utils/src/util/mod.rs +++ b/packages/backend/native-utils/src/util/mod.rs @@ -1,5 +1,4 @@ pub mod acct; -pub mod config; pub mod convert_host; pub mod convert_to_hidden_post; pub mod escape_sql; diff --git a/packages/backend/src/boot/index.ts b/packages/backend/src/boot/index.ts index 489350a15..d4c47c5fe 100644 --- a/packages/backend/src/boot/index.ts +++ b/packages/backend/src/boot/index.ts @@ -3,7 +3,7 @@ import chalk from "chalk"; import Xev from "xev"; import Logger from "@/services/logger.js"; -import { envOption } from "../env.js"; +import { envOption } from "@/config/index.js"; // for typeorm import "reflect-metadata"; diff --git a/packages/backend/src/boot/master.ts b/packages/backend/src/boot/master.ts index 84767a92c..f617c8594 100644 --- a/packages/backend/src/boot/master.ts +++ b/packages/backend/src/boot/master.ts @@ -10,7 +10,7 @@ import semver from "semver"; import Logger from "@/services/logger.js"; import loadConfig from "@/config/load.js"; import type { Config } from "@/config/types.js"; -import { envOption } from "@/env.js"; +import { envOption } from "@/config/index.js"; import { showMachineInfo } from "@/misc/show-machine-info.js"; import { db, initDb } from "@/db/postgre.js"; diff --git a/packages/backend/src/config/index.ts b/packages/backend/src/config/index.ts index ae197b09c..7e8545015 100644 --- a/packages/backend/src/config/index.ts +++ b/packages/backend/src/config/index.ts @@ -1,3 +1,5 @@ import load from "./load.js"; +import { readEnvironmentConfig } from "native-utils/built/index.js"; export default load(); +export const envOption = readEnvironmentConfig(); diff --git a/packages/backend/src/env.ts b/packages/backend/src/env.ts deleted file mode 100644 index a788a0fba..000000000 --- a/packages/backend/src/env.ts +++ /dev/null @@ -1,25 +0,0 @@ -const envOption = { - onlyQueue: false, - onlyServer: false, - noDaemons: false, - disableClustering: false, - verbose: false, - withLogTime: false, - quiet: false, - slow: false, -}; - -for (const key of Object.keys(envOption) as (keyof typeof envOption)[]) { - if ( - process.env[ - `MK_${key.replace(/[A-Z]/g, (letter) => `_${letter}`).toUpperCase()}` - ] - ) - envOption[key] = true; -} - -if (process.env.NODE_ENV === "test") envOption.disableClustering = true; -if (process.env.NODE_ENV === "test") envOption.quiet = true; -if (process.env.NODE_ENV === "test") envOption.noDaemons = true; - -export { envOption }; diff --git a/packages/backend/src/queue/index.ts b/packages/backend/src/queue/index.ts index 000e477b5..3537866b8 100644 --- a/packages/backend/src/queue/index.ts +++ b/packages/backend/src/queue/index.ts @@ -5,7 +5,7 @@ import config from "@/config/index.js"; import type { DriveFile } from "@/models/entities/drive-file.js"; import type { IActivity } from "@/remote/activitypub/type.js"; import type { Webhook, webhookEventTypes } from "@/models/entities/webhook.js"; -import { envOption } from "../env.js"; +import { envOption } from "@/config/index.js"; import processDeliver from "./processors/deliver.js"; import processInbox from "./processors/inbox.js"; diff --git a/packages/backend/src/server/index.ts b/packages/backend/src/server/index.ts index ad14d9abf..4fcc6912c 100644 --- a/packages/backend/src/server/index.ts +++ b/packages/backend/src/server/index.ts @@ -20,7 +20,7 @@ import { fetchMeta } from "@/misc/fetch-meta.js"; import { genIdenticon } from "@/misc/gen-identicon.js"; import { createTemp } from "@/misc/create-temp.js"; import { stringToAcct } from "native-utils/built/index.js"; -import { envOption } from "@/env.js"; +import { envOption } from "@/config/index.js"; import megalodon, { MegalodonInterface } from "megalodon"; import activityPub from "./activitypub.js"; import nodeinfo from "./nodeinfo.js"; diff --git a/packages/backend/src/services/logger.ts b/packages/backend/src/services/logger.ts index 5a429ae33..d4be47aff 100644 --- a/packages/backend/src/services/logger.ts +++ b/packages/backend/src/services/logger.ts @@ -2,7 +2,7 @@ import cluster from "node:cluster"; import chalk from "chalk"; import { default as convertColor } from "color-convert"; import { format as dateFormat } from "date-fns"; -import { envOption } from "@/env.js"; +import { envOption } from "@/config/index.js"; import config from "@/config/index.js"; import * as SyslogPro from "syslog-pro";