mirror of
https://github.com/zkldi/Tachi.git
synced 2026-10-08 06:48:04 +03:00
151 lines
4.0 KiB
TypeScript
151 lines
4.0 KiB
TypeScript
// barrel file for re-exporting env variables.
|
|
import dotenv from "dotenv";
|
|
import JSON5 from "json5";
|
|
import fs from "fs";
|
|
import p from "prudence";
|
|
import { FormatPrError } from "utils/prudence";
|
|
import { integer, StaticConfig } from "tachi-common";
|
|
dotenv.config(); // imports things like NODE_ENV from a local .env file if one is present.
|
|
|
|
// stub - having a real logger here creates a circular dependency.r
|
|
const logger = console; // CreateLogCtx(__filename);
|
|
|
|
const confLocation = process.env.TCHIS_CONF_LOCATION ?? "./conf.json5";
|
|
|
|
// reads from $pwd/conf.json5, unless an override is set
|
|
let confFile;
|
|
|
|
try {
|
|
confFile = fs.readFileSync(confLocation, "utf-8");
|
|
} catch (err) {
|
|
logger.error("Error while trying to open conf.json5. Is one present?", { err });
|
|
process.exit(1);
|
|
}
|
|
|
|
const config = JSON5.parse(confFile);
|
|
|
|
function isValidURL(self: unknown) {
|
|
if (typeof self !== "string") {
|
|
return `Expected URL, received type ${typeof self}`;
|
|
}
|
|
|
|
try {
|
|
new URL(self);
|
|
return true;
|
|
} catch (err) {
|
|
return `Invalid URL ${self}.`;
|
|
}
|
|
}
|
|
|
|
export interface OAuth2Info {
|
|
CLIENT_ID: string;
|
|
CLIENT_SECRET: string;
|
|
REDIRECT_URI: string;
|
|
}
|
|
|
|
export interface TachiConfig {
|
|
MONGO_CONNECTION_URL: string;
|
|
MONGO_DATABASE_NAME: string;
|
|
LOG_LEVEL: "debug" | "verbose" | "info" | "warn" | "error" | "severe" | "crit";
|
|
CAPTCHA_SECRET_KEY: string;
|
|
SESSION_SECRET: string;
|
|
FLO_API_URL: string;
|
|
EAG_API_URL: string;
|
|
MIN_API_URL: string;
|
|
ARC_API_URL: string;
|
|
FLO_OAUTH2_INFO?: OAuth2Info;
|
|
EAG_OAUTH2_INFO?: OAuth2Info;
|
|
MIN_OAUTH2_INFO?: OAuth2Info;
|
|
ARC_AUTH_TOKEN: string;
|
|
CDN_FILE_ROOT: string;
|
|
TYPE: "ktchi" | "btchi" | "omni";
|
|
PORT: integer;
|
|
ENABLE_SERVER_HTTPS?: boolean;
|
|
RUN_OWN_CDN?: boolean;
|
|
CLIENT_DEV_SERVER?: string | null;
|
|
SERVER_TYPE_INFO: StaticConfig.ServerConfig;
|
|
RATE_LIMIT: integer;
|
|
OAUTH_CLIENT_CAP: integer;
|
|
OPTIONS_ALWAYS_SUCCEEDS?: boolean;
|
|
NO_CONSOLE?: boolean;
|
|
EMAIL_CONFIG?: {
|
|
FROM: string;
|
|
SENDMAIL_BIN?: string;
|
|
};
|
|
USC_QUEUE_SIZE: integer;
|
|
BEATORAJA_QUEUE_SIZE: integer;
|
|
OUR_URL: string;
|
|
LOGGER_DISCORD_WEBHOOK?: string;
|
|
DISCORD_WHO_TO_TAG?: string[];
|
|
INVOKE_JOB_RUNNER?: boolean;
|
|
}
|
|
|
|
const isValidOauth2 = p.optional({
|
|
CLIENT_ID: "string",
|
|
CLIENT_SECRET: "string",
|
|
REDIRECT_URI: "string",
|
|
});
|
|
|
|
const err = p(config, {
|
|
MONGO_CONNECTION_URL: "string",
|
|
MONGO_DATABASE_NAME: "string",
|
|
LOG_LEVEL: p.isIn("debug", "verbose", "info", "warn", "error", "severe", "crit"),
|
|
CAPTCHA_SECRET_KEY: "string",
|
|
SESSION_SECRET: "string",
|
|
FLO_API_URL: isValidURL,
|
|
EAG_API_URL: isValidURL,
|
|
MIN_API_URL: isValidURL,
|
|
ARC_API_URL: isValidURL,
|
|
FLO_OAUTH2_INFO: isValidOauth2,
|
|
EAG_OAUTH2_INFO: isValidOauth2,
|
|
MIN_OAUTH2_INFO: isValidOauth2,
|
|
ARC_AUTH_TOKEN: "string",
|
|
CDN_FILE_ROOT: "string",
|
|
PORT: p.isPositiveInteger,
|
|
ENABLE_SERVER_HTTPS: "*boolean",
|
|
RUN_OWN_CDN: "*boolean",
|
|
CLIENT_DEV_SERVER: "*?string",
|
|
TYPE: p.isIn("ktchi", "btchi", "omni"),
|
|
RATE_LIMIT: p.optional(p.isPositiveInteger),
|
|
OAUTH_CLIENT_CAP: p.optional(p.isPositiveInteger),
|
|
OPTIONS_ALWAYS_SUCCEEDS: "*boolean",
|
|
NO_CONSOLE: "*boolean",
|
|
EMAIL_CONFIG: p.optional({
|
|
FROM: "string",
|
|
SENDMAIL_BIN: "*string",
|
|
}),
|
|
USC_QUEUE_SIZE: p.optional(p.gteInt(2)),
|
|
BEATORAJA_QUEUE_SIZE: p.optional(p.gteInt(2)),
|
|
OUR_URL: "string",
|
|
LOGGER_DISCORD_WEBHOOK: "*string",
|
|
DISCORD_WHO_TO_TAG: p.optional(["string"]),
|
|
INVOKE_JOB_RUNNER: "*boolean",
|
|
});
|
|
|
|
if (err) {
|
|
throw FormatPrError(err, "Invalid conf.json5 file.");
|
|
}
|
|
|
|
if (config.TYPE === "ktchi") {
|
|
config.SERVER_TYPE_INFO = StaticConfig.KTCHI_CONFIG;
|
|
} else if (config.TYPE === "btchi") {
|
|
config.SERVER_TYPE_INFO = StaticConfig.BTCHI_CONFIG;
|
|
} else if (config.TYPE === "omni") {
|
|
config.SERVER_TYPE_INFO = StaticConfig.OMNI_CONFIG;
|
|
}
|
|
|
|
const tachiConfig = config as TachiConfig;
|
|
|
|
// default rate limit 500
|
|
tachiConfig.RATE_LIMIT ??= 500;
|
|
tachiConfig.OAUTH_CLIENT_CAP ??= 15;
|
|
tachiConfig.USC_QUEUE_SIZE ??= 3;
|
|
tachiConfig.BEATORAJA_QUEUE_SIZE ??= 3;
|
|
|
|
if (tachiConfig.EMAIL_CONFIG) {
|
|
tachiConfig.EMAIL_CONFIG.SENDMAIL_BIN ??= "/usr/bin/sendmail";
|
|
}
|
|
|
|
export const ServerTypeInfo = tachiConfig.SERVER_TYPE_INFO;
|
|
export const ServerConfig = tachiConfig;
|