Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
5 changes: 5 additions & 0 deletions back/src/services/vhost.ts
Original file line number Diff line number Diff line change
@@ -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<Vhost[]> {
if (!checkMainDirExist()) {
return []
}

const sitesEnabled = await fs.readdir(SITES_ENABLED)
const sitesAvailable = await fs.readdir(SITES_AVAILABLE)

Expand Down
34 changes: 34 additions & 0 deletions back/src/utils/dir.helper.ts
Original file line number Diff line number Diff line change
@@ -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
}
Loading