diff options
| author | Autumn <git@autumnfo.rest> | 2026-05-11 17:47:44 +0100 |
|---|---|---|
| committer | Autumn <git@autumnfo.rest> | 2026-05-11 17:47:44 +0100 |
| commit | 951844836fca55b5321999a4d4637d1b56fdb448 (patch) | |
| tree | 3ed928cd5040f102a13667ef8310e77c7df61327 /src | |
| parent | d51a6e14f75e3394ec9ca8ebc72fb10bf693edc7 (diff) | |
[app] added .env support
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/dotenv.ts | 28 | ||||
| -rw-r--r-- | src/server.ts | 8 |
2 files changed, 34 insertions, 2 deletions
diff --git a/src/lib/dotenv.ts b/src/lib/dotenv.ts new file mode 100644 index 0000000..2fb8e65 --- /dev/null +++ b/src/lib/dotenv.ts @@ -0,0 +1,28 @@ +// +// ~~~ read a .env file +// + +// imports +import { readFileSync } from "node:fs" + +// load .env function +export default function loadDotenv() { + + const file: string = ".env" // todo: don't hard-code the location + + try { + + const fileContents = readFileSync(file, "utf8") + .split("\n") + .filter((line) => line !== "") + .map((line) => line.split("=")) + + const fileEntries = new Map(fileContents) + return Object.fromEntries(fileEntries) + + } catch(e) { + + console.error("!> cannot read .env file") + + } +} diff --git a/src/server.ts b/src/server.ts index 8741e16..424a4f0 100644 --- a/src/server.ts +++ b/src/server.ts @@ -2,10 +2,14 @@ // ~~~ server process // -// setup app +// imports import app from "./app.ts" +import loadDotenv from "@lib/dotenv.ts" + +// setup app +const dotenv = loadDotenv() -const PORT = 3000 +const PORT = dotenv?.HC_PORT || 3000 // start app app.listen(PORT, () => { |
