diff --git a/CHANGELOG.md b/CHANGELOG.md index e237303..d7b3c99 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,3 +8,7 @@ - Display expiration date of SSL certificates (commit [48218](https://github.com/MathieuRA/nguix/commit/4821882d17b8cb7d23fdcdf9452b0d48e2884244)) - Display `nginx.service` status(commit [852bd](https://github.com/MathieuRA/nguix/commit/852bd52e735687fa4b6855f3f41bba0eae322e2f)) - Disable/Enable virtual hosts (commit [af4f6](https://github.com/MathieuRA/nguix/commit/af4f6578b05386e9ffab3aeb8a9a1d91ad0b10ed)) + +## Fixes + +- Don't crash the app if `SITES_AVAILABLE` or `SITES_ENABLED` point to a non existing dir (PR [#3](https://github.com/MathieuRA/nguix/pull/3)) \ No newline at end of file diff --git a/back/src/services/vhost.ts b/back/src/services/vhost.ts index a45acb3..a13f956 100644 --- a/back/src/services/vhost.ts +++ b/back/src/services/vhost.ts @@ -1,11 +1,16 @@ import fs from "node:fs/promises" import { Vhost } from "../classes/vhost.ts"; import { getEnv } from "../env.ts"; +import { checkMainDirExist } from "../utils/dir.helper.ts"; const SITES_AVAILABLE = getEnv('SITES_AVAILABLE') const SITES_ENABLED = getEnv('SITES_ENABLED') export async function getVirtualHosts(): Promise { + if (!checkMainDirExist()) { + return [] + } + const sitesEnabled = await fs.readdir(SITES_ENABLED) const sitesAvailable = await fs.readdir(SITES_AVAILABLE) diff --git a/back/src/utils/dir.helper.ts b/back/src/utils/dir.helper.ts new file mode 100644 index 0000000..179e33c --- /dev/null +++ b/back/src/utils/dir.helper.ts @@ -0,0 +1,34 @@ +import fs from "node:fs" +import { getEnv } from "../env.ts" + +const MAIN_DIRS = ["SITES_AVAILABLE", "SITES_ENABLED"] + +export function checkMainDirExist() { + const dirExist = MAIN_DIRS.map(envVar => { + const path = getEnv(envVar) + + return { + exist: fs.existsSync(path), + name: envVar, + path + } + }) + + if (dirExist.some(dir => !dir.exist)) { + dirExist.forEach(dir => { + if (!dir.exist) { + const error = new Error('path do not exist', { + cause: { + path: dir.path, + variable: dir.name + } + }) + console.warn(`[getVirtualHosts]:`, error) + } + }) + + return false + } + + return true +} \ No newline at end of file