diff --git a/neko/index.js b/neko/index.js index 54ceecb3..ebfc5d97 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 00000000..b38d21e9 --- /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 00000000..b708f2b2 --- /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 ccf14816..f9adc54d 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 f18e69a4..9e0bd3ec 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 03022897..d69201f6 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 441b0325..79c10c95 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 489350a1..d4c47c5f 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 84767a92..f617c859 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 ae197b09..7e854501 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 a788a0fb..00000000 --- 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 000e477b..3537866b 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 ad14d9ab..4fcc6912 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 5a429ae3..d4be47af 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";