1
0
Fork 1
mirror of https://example.com synced 2024-11-22 12:16:40 +09:00

refactor: move env.ts to native-utils (partially)

This commit is contained in:
naskya 2024-01-18 00:11:42 +09:00
parent e35ae0846d
commit c4c9041222
Signed by: naskya
GPG key ID: 712D413B3A9FED5C
14 changed files with 49 additions and 38 deletions

View file

@ -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;

View file

@ -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(),
}
}

View file

@ -0,0 +1,2 @@
pub mod environment;
pub mod server;

View file

@ -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'");

View file

@ -1,3 +1,4 @@
pub mod config;
pub mod database;
pub mod macros;
pub mod model;

View file

@ -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>) -> 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>) -> String {
#[cfg_attr(feature = "napi", napi_derive::napi)]
pub fn is_self_host(host: Option<String>) -> bool {
if let Some(x) = host {
extract_host(config().url) == to_puny(x)
extract_host(read_server_config().url) == to_puny(x)
} else {
true
}

View file

@ -1,5 +1,4 @@
pub mod acct;
pub mod config;
pub mod convert_host;
pub mod convert_to_hidden_post;
pub mod escape_sql;

View file

@ -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";

View file

@ -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";

View file

@ -1,3 +1,5 @@
import load from "./load.js";
import { readEnvironmentConfig } from "native-utils/built/index.js";
export default load();
export const envOption = readEnvironmentConfig();

View file

@ -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 };

View file

@ -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";

View file

@ -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";

View file

@ -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";