aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.env.example1
-rw-r--r--src/lib/dotenv.ts28
-rw-r--r--src/server.ts8
-rw-r--r--tsconfig.json6
4 files changed, 40 insertions, 3 deletions
diff --git a/.env.example b/.env.example
new file mode 100644
index 0000000..5266bba
--- /dev/null
+++ b/.env.example
@@ -0,0 +1 @@
+HC_PORT=3000
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, () => {
diff --git a/tsconfig.json b/tsconfig.json
index 7edbc7b..e912135 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -6,7 +6,11 @@
"lib": [ "esnext" ],
"target": "ESNext",
"module": "CommonJS",
- "types": [ "node" ]
+ "types": [ "node" ],
+
+ "paths": {
+ "@lib/*": [ "src/lib/*" ]
+ }
},
"ts-node": {