From 4bee0454e669f1109ace4f91260c0b3f9af9610e Mon Sep 17 00:00:00 2001 From: Kuba Orlik Date: Thu, 8 Feb 2024 13:20:54 +0100 Subject: [PATCH] Move env-based config to a separate file --- src/back/app.ts | 26 ++++++++++---------------- src/back/config.ts | 17 +++++++++++++++++ src/back/index.ts | 7 +++++-- src/back/test_utils/with-prod-app.ts | 11 ++++++----- 4 files changed, 38 insertions(+), 23 deletions(-) create mode 100644 src/back/config.ts diff --git a/src/back/app.ts b/src/back/app.ts index 782f595..3ed1093 100644 --- a/src/back/app.ts +++ b/src/back/app.ts @@ -2,25 +2,19 @@ import _locreq from "locreq"; import Sealious, { App, LoggerMailer, SMTPMailer } from "sealious"; import type { LoggerLevel } from "sealious/@types/src/app/logger.js"; import { collections } from "./collections/collections.js"; +import { + BASE_URL, + MAILCATCHER_HOST, + MAILCATCHER_SMTP_PORT, + MAILER, + MONGO_HOST, + MONGO_PORT, + PORT, +} from "./config.js"; import ADMIN_CREDENTIALS from "./default-admin-credentials.js"; import { module_dirname } from "./util.js"; const locreq = _locreq(module_dirname(import.meta.url)); -const PORT = process.env.SEALIOUS_PORT ? parseInt(process.env.SEALIOUS_PORT) : 8080; -const base_url = process.env.SEALIOUS_BASE_URL || `http://localhost:${PORT}`; -export const MONGO_PORT = process.env.SEALIOUS_MONGO_PORT - ? parseInt(process.env.SEALIOUS_MONGO_PORT) - : 20747; -export const MONGO_HOST = process.env.SEALIOUS_MONGO_HOST || "127.0.0.1"; -export const MAILCATCHER_HOST = process.env.SEALIOUS_MAILCATCHER_HOST || "127.0.0.1"; -export const MAILCATCHER_SMTP_PORT = parseInt( - process.env.SEALIOUS_MAILCATCHER_SMTP_PORT || "1026" -); -export const MAILCATCHER_API_PORT = parseInt( - process.env.SEALIOUS_MAILCATCHER_API_PORT || "1082" -); -export const MAILER = process.env.SEALIOUS_MAILER; - declare module "koa" { interface BaseContext { $context: Sealious.Context; @@ -56,7 +50,7 @@ export default class TheApp extends App { logo: locreq.resolve("assets/logo.png"), version: "0.0.1", default_language: "en", - base_url, + base_url: BASE_URL, admin_email: ADMIN_CREDENTIALS.email, colors: { primary: "#5294a1", diff --git a/src/back/config.ts b/src/back/config.ts new file mode 100644 index 0000000..38515c1 --- /dev/null +++ b/src/back/config.ts @@ -0,0 +1,17 @@ +export const SEALIOUS_SANITY = Boolean(process.env.SEALIOUS_SANITY); +export const PORT = process.env.SEALIOUS_PORT + ? parseInt(process.env.SEALIOUS_PORT) + : 8080; +export const BASE_URL = process.env.SEALIOUS_BASE_URL || `http://localhost:${PORT}`; +export const MONGO_PORT = process.env.SEALIOUS_MONGO_PORT + ? parseInt(process.env.SEALIOUS_MONGO_PORT) + : 20747; +export const MONGO_HOST = process.env.SEALIOUS_MONGO_HOST || "127.0.0.1"; +export const MAILCATCHER_HOST = process.env.SEALIOUS_MAILCATCHER_HOST || "127.0.0.1"; +export const MAILCATCHER_SMTP_PORT = parseInt( + process.env.SEALIOUS_MAILCATCHER_SMTP_PORT || "1026" +); +export const MAILCATCHER_API_PORT = parseInt( + process.env.SEALIOUS_MAILCATCHER_API_PORT || "1082" +); +export const MAILER = process.env.SEALIOUS_MAILER; diff --git a/src/back/index.ts b/src/back/index.ts index bd0a628..b7e2a2f 100644 --- a/src/back/index.ts +++ b/src/back/index.ts @@ -1,16 +1,19 @@ import kill from "kill-port"; import _locreq from "locreq"; import TheApp from "./app.js"; +import { SEALIOUS_SANITY } from "./config.js"; import { mainRouter } from "./routes/index.js"; import { module_dirname } from "./util.js"; const locreq = _locreq(module_dirname(import.meta.url)); const app = new TheApp(); +console.log({ SEALIOUS_SANITY }); + kill(app.config["www-server"].port) .then(() => app.start()) .then(async () => { - if (process.env.SEALIOUS_SANITY === "true") { + if (SEALIOUS_SANITY) { console.error("Exiting with error code 0"); process.exit(0); } @@ -18,7 +21,7 @@ kill(app.config["www-server"].port) }) .catch((error) => { console.error(error); - if (process.env.SEALIOUS_SANITY === "true") { + if (SEALIOUS_SANITY) { console.error("EXITING WITH STATUS 1"); process.exit(1); } diff --git a/src/back/test_utils/with-prod-app.ts b/src/back/test_utils/with-prod-app.ts index 1733274..ba431ba 100644 --- a/src/back/test_utils/with-prod-app.ts +++ b/src/back/test_utils/with-prod-app.ts @@ -4,14 +4,15 @@ import { v4 as uuid } from "uuid"; const locreq = _locreq(module_dirname(import.meta.url)); import { SMTPMailer } from "sealious"; import { TestUtils } from "sealious"; -import TheApp, { - MAILCATCHER_API_PORT, - MAILCATCHER_HOST, - MAILCATCHER_SMTP_PORT, -} from "../app.js"; +import TheApp from "../app.js"; import { mainRouter } from "../routes/index.js"; import { module_dirname } from "../util.js"; import getPort from "get-port"; +import { + MAILCATCHER_API_PORT, + MAILCATCHER_HOST, + MAILCATCHER_SMTP_PORT, +} from "../config.js"; const port_numbers = async function* () { yield await getPort();