From 4dbbb916eeb6855a90a7b020a5437b7204166ed1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Gonz=C3=A1lez=20Rubio?= Date: Fri, 4 Oct 2024 01:03:58 +0200 Subject: [PATCH] feat: added @oh/config - fix #96 (#97) --- app/server/deno.json | 1 + app/server/deno.lock | 32 +++++++++++++++- app/server/src/shared/utils/config.utils.ts | 41 --------------------- app/server/src/shared/utils/main.ts | 1 - app/server/src/system/main.ts | 5 ++- 5 files changed, 35 insertions(+), 45 deletions(-) delete mode 100644 app/server/src/shared/utils/config.utils.ts diff --git a/app/server/deno.json b/app/server/deno.json index b7e5bb2..e7ea26c 100644 --- a/app/server/deno.json +++ b/app/server/deno.json @@ -16,6 +16,7 @@ "imports": { "@oh/queue": "jsr:@oh/queue@1.1.1", "@oh/updater": "jsr:@oh/updater@1.1.1", + "@oh/config": "jsr:@oh/config@1.0.1", "shared/": "./src/shared/", "system/": "./src/system/", diff --git a/app/server/deno.lock b/app/server/deno.lock index d81030e..3dafe64 100644 --- a/app/server/deno.lock +++ b/app/server/deno.lock @@ -2,13 +2,24 @@ "version": "3", "packages": { "specifiers": { + "jsr:@oh/config@1.0.1": "jsr:@oh/config@1.0.1", "jsr:@oh/queue@1.1.1": "jsr:@oh/queue@1.1.1", "jsr:@oh/updater@1.1.1": "jsr:@oh/updater@1.1.1", + "jsr:@oh/yaml@1.0.2": "jsr:@oh/yaml@1.0.2", + "jsr:@std/fs@1.0.4": "jsr:@std/fs@1.0.4", "jsr:@std/path@1.0.6": "jsr:@std/path@1.0.6", + "jsr:@std/path@^1.0.6": "jsr:@std/path@1.0.6", "npm:nodemailer@6.9.15": "npm:nodemailer@6.9.15", - "npm:yaml@2.4.2": "npm:yaml@2.4.2" + "npm:yaml@2.4.2": "npm:yaml@2.4.2", + "npm:yaml@2.5.1": "npm:yaml@2.5.1" }, "jsr": { + "@oh/config@1.0.1": { + "integrity": "91271bbd88999b13ffe1d161453f83cef2b37900a4b1467da7a9688d051d71fc", + "dependencies": [ + "jsr:@oh/yaml@1.0.2" + ] + }, "@oh/queue@1.1.1": { "integrity": "8e8ec809202c48beaf436b3ea01b4c3a17f8c015ca686d906822482c9c881390" }, @@ -18,6 +29,20 @@ "jsr:@std/path@1.0.6" ] }, + "@oh/yaml@1.0.2": { + "integrity": "06921094d666924dc39280961578acc197a9449199b9425647a60738140bd279", + "dependencies": [ + "jsr:@std/fs@1.0.4", + "jsr:@std/path@1.0.6", + "npm:yaml@2.5.1" + ] + }, + "@std/fs@1.0.4": { + "integrity": "2907d32d8d1d9e540588fd5fe0ec21ee638134bd51df327ad4e443aaef07123c", + "dependencies": [ + "jsr:@std/path@^1.0.6" + ] + }, "@std/path@1.0.6": { "integrity": "ab2c55f902b380cf28e0eec501b4906e4c1960d13f00e11cfbcd21de15f18fed" } @@ -30,6 +55,10 @@ "yaml@2.4.2": { "integrity": "sha512-B3VqDZ+JAg1nZpaEmWtTXUlBneoGx6CPM9b0TENK6aoSu5t73dItudwdgmi6tHlIZZId4dZ9skcAQ2UbcyAeVA==", "dependencies": {} + }, + "yaml@2.5.1": { + "integrity": "sha512-bLQOjaX/ADgQ20isPJRvF0iRUHIxVhYvr53Of7wGcWlO2jvtUlH5m87DsmulFVxRpNLOnI4tB6p/oh8D7kpn9Q==", + "dependencies": {} } } }, @@ -174,6 +203,7 @@ }, "workspace": { "dependencies": [ + "jsr:@oh/config@1.0.1", "jsr:@oh/queue@1.1.1", "jsr:@oh/updater@1.1.1", "npm:nodemailer@6.9.15", diff --git a/app/server/src/shared/utils/config.utils.ts b/app/server/src/shared/utils/config.utils.ts deleted file mode 100644 index aa87d3f..0000000 --- a/app/server/src/shared/utils/config.utils.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { readYaml, writeYaml } from "./yaml.utils.ts"; -import { ConfigTypes } from "shared/types/config.types.ts"; -import { CONFIG_DEFAULT } from "shared/consts/config.consts.ts"; - -export const getConfig = async (): Promise => { - let config: ConfigTypes = {} as ConfigTypes; - try { - config = await readYaml("./config.yml"); - } catch (e) {} - - const defaults: ConfigTypes = { - port: config?.port || CONFIG_DEFAULT.port, - url: config?.url || CONFIG_DEFAULT.url, - sessions: { - checkInterval: - config?.sessions?.checkInterval || - CONFIG_DEFAULT.sessions.checkInterval, - }, - database: { - filename: config?.database?.filename || CONFIG_DEFAULT.database.filename, - }, - captcha: { - enabled: config?.captcha?.enabled ?? CONFIG_DEFAULT.captcha.enabled, - url: config?.captcha?.url ?? CONFIG_DEFAULT.captcha.url, - id: config?.captcha?.id ?? CONFIG_DEFAULT.captcha.id, - token: config?.captcha?.token ?? CONFIG_DEFAULT.captcha.token, - }, - email: { - enabled: config?.email?.enabled ?? CONFIG_DEFAULT.email.enabled, - hostname: config?.email?.hostname ?? CONFIG_DEFAULT.email.hostname, - username: config?.email?.username ?? CONFIG_DEFAULT.email.username, - password: config?.email?.password ?? CONFIG_DEFAULT.email.password, - port: config?.email?.port ?? CONFIG_DEFAULT.email.port, - }, - }; - try { - await writeYaml("./config.yml", defaults, { async: true }); - } catch (e) {} - - return defaults; -}; diff --git a/app/server/src/shared/utils/main.ts b/app/server/src/shared/utils/main.ts index acca6f4..3ae10c9 100644 --- a/app/server/src/shared/utils/main.ts +++ b/app/server/src/shared/utils/main.ts @@ -1,7 +1,6 @@ export * from "./request.utils.ts"; export * from "./cors.utils.ts"; export * from "./random.utils.ts"; -export * from "./config.utils.ts"; export * from "./directory.utils.ts"; export * from "./yaml.utils.ts"; export * from "./envs.utils.ts"; diff --git a/app/server/src/system/main.ts b/app/server/src/system/main.ts index 0bcc9d5..1640be7 100644 --- a/app/server/src/system/main.ts +++ b/app/server/src/system/main.ts @@ -1,13 +1,14 @@ import { db } from "./db.ts"; import { api } from "./api.ts"; import { ConfigTypes, Envs } from "shared/types/main.ts"; -import { getConfig as $getConfig } from "shared/utils/main.ts"; +import { getConfig as $getConfig } from "@oh/config"; import { load as loadUpdater } from "modules/updater/main.ts"; import { captcha } from "./captcha.ts"; import { email } from "./email.ts"; import { otp } from "./otp.ts"; import { tasks } from "./tasks.ts"; import { sessions } from "./sessions.ts"; +import { CONFIG_DEFAULT } from "shared/consts/config.consts.ts"; export const System = (() => { const $db = db(); @@ -24,7 +25,7 @@ export const System = (() => { const load = async (envs: Envs) => { if (await loadUpdater(envs)) return; - $config = await $getConfig(); + $config = await $getConfig(CONFIG_DEFAULT); $envs = envs; $tasks.load();