summary refs log tree commit diff
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2025-05-29 22:28:19 +0200
committerRory& <root@rory.gay>2025-05-29 22:28:19 +0200
commita089651693cf6912864a6589de0f7aad911a8b83 (patch)
treebd3d229b1c5891a40709ff34e3f7fb17d654a142
parentAllow unfree in nix flake, add mongodb-compass to devShell closure, basic mon... (diff)
downloadnodejs-final-assignment-a089651693cf6912864a6589de0f7aad911a8b83.tar.xz
Get database working
-rw-r--r--.idea/dataSources.xml7
-rw-r--r--.idea/modules.xml8
-rw-r--r--.idea/nodejs-final-assignment.iml12
-rw-r--r--README.md9
-rw-r--r--src/api/routes/auth/registerRoute.js2
-rw-r--r--src/api/routes/statusRoute.js2
-rw-r--r--src/db/db.js9
-rw-r--r--src/db/schemas/user.js (renamed from src/db/schemas/userSchema.js)0
-rw-r--r--src/util/index.js1
-rw-r--r--src/util/secretUtils.js9
10 files changed, 52 insertions, 7 deletions
diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml

index 8df7846..f495692 100644 --- a/.idea/dataSources.xml +++ b/.idea/dataSources.xml
@@ -1,12 +1,15 @@ <?xml version="1.0" encoding="UTF-8"?> <project version="4"> <component name="DataSourceManagerImpl" format="xml" multifile-model="true"> - <data-source source="LOCAL" name="myproject@localhost" uuid="6433d464-6ca0-4d7d-a023-05bd4df14dd6"> + <data-source source="LOCAL" name="nodejs@localhost" uuid="6433d464-6ca0-4d7d-a023-05bd4df14dd6"> <driver-ref>mongo.4</driver-ref> <synchronize>true</synchronize> <jdbc-driver>com.dbschema.MongoJdbcDriver</jdbc-driver> - <jdbc-url>mongodb://localhost:27017/myproject</jdbc-url> + <jdbc-url>mongodb://localhost:27017/nodejs</jdbc-url> <working-dir>$ProjectFileDir$</working-dir> + <driver-properties> + <property name="authSource" value="admin" /> + </driver-properties> </data-source> </component> </project> \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644
index 0000000..ca60690 --- /dev/null +++ b/.idea/modules.xml
@@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="ProjectModuleManager"> + <modules> + <module fileurl="file://$PROJECT_DIR$/.idea/nodejs-final-assignment.iml" filepath="$PROJECT_DIR$/.idea/nodejs-final-assignment.iml" /> + </modules> + </component> +</project> \ No newline at end of file diff --git a/.idea/nodejs-final-assignment.iml b/.idea/nodejs-final-assignment.iml new file mode 100644
index 0000000..24643cc --- /dev/null +++ b/.idea/nodejs-final-assignment.iml
@@ -0,0 +1,12 @@ +<?xml version="1.0" encoding="UTF-8"?> +<module type="WEB_MODULE" version="4"> + <component name="NewModuleRootManager"> + <content url="file://$MODULE_DIR$"> + <excludeFolder url="file://$MODULE_DIR$/.tmp" /> + <excludeFolder url="file://$MODULE_DIR$/temp" /> + <excludeFolder url="file://$MODULE_DIR$/tmp" /> + </content> + <orderEntry type="inheritedJdk" /> + <orderEntry type="sourceFolder" forTests="false" /> + </component> +</module> \ No newline at end of file diff --git a/README.md b/README.md
index 3bc89d4..e15cc71 100644 --- a/README.md +++ b/README.md
@@ -1 +1,10 @@ [![Review Assignment Due Date](https://classroom.github.com/assets/deadline-readme-button-22041afd0340ce965d47ae6ef1cefeee28c7c493a6346c4f15d667ab976d596c.svg)](https://classroom.github.com/a/3A9ByVp3) + +# Deployment + +Environment variables: +| Name | Default | Description | +| ---- | ------- | ----------- | +| `PORT` | `3000` | The port the server will run on. | +| `LOG_REQUESTS` | `-` | Requests to log to the console by status, `-` to invert. | +| `DATABASE_SECRET_PATH` | `` | The path to the mongodb connection string. | \ No newline at end of file diff --git a/src/api/routes/auth/registerRoute.js b/src/api/routes/auth/registerRoute.js
index b71c466..14188a4 100644 --- a/src/api/routes/auth/registerRoute.js +++ b/src/api/routes/auth/registerRoute.js
@@ -1,4 +1,4 @@ -import { User } from "#db/schemas/userSchema.js"; +import { User } from "#db/schemas/user.js"; export const registerRoute = { route: "/auth/register", diff --git a/src/api/routes/statusRoute.js b/src/api/routes/statusRoute.js
index f1cdb99..b9974a5 100644 --- a/src/api/routes/statusRoute.js +++ b/src/api/routes/statusRoute.js
@@ -1,4 +1,4 @@ -import { User } from "#db/schemas/userSchema.js"; +import { User } from "#db/schemas/user.js"; export const statusRoute = { route: "/status", diff --git a/src/db/db.js b/src/db/db.js
index bf7ccfa..36f2105 100644 --- a/src/db/db.js +++ b/src/db/db.js
@@ -1,13 +1,16 @@ import { connect } from "mongoose"; +import { readSecret } from "#util/secretUtils.js"; export async function initDb() { - const connStr = "mongodb://root:Foxy1987@localhost/myproject"; + const connectionString = await readSecret( + process.env["DATABASE_SECRET_PATH"], + ); try { - const res = await connect(connStr); + const res = await connect(connectionString); if (res.connection.readyState === 1) { console.log("[MONGODB] Connected successfully!"); } else { - console.error("[MONGODB] Failed to connect to ", connStr); + console.error("[MONGODB] Failed to connect to ", connectionString); } } catch (e) { console.error("[MONGODB] Error connecting to database!"); diff --git a/src/db/schemas/userSchema.js b/src/db/schemas/user.js
index eaddb08..eaddb08 100644 --- a/src/db/schemas/userSchema.js +++ b/src/db/schemas/user.js
diff --git a/src/util/index.js b/src/util/index.js
index e69de29..9a6c774 100644 --- a/src/util/index.js +++ b/src/util/index.js
@@ -0,0 +1 @@ +export * from "./secretUtils.js"; diff --git a/src/util/secretUtils.js b/src/util/secretUtils.js new file mode 100644
index 0000000..33e5fef --- /dev/null +++ b/src/util/secretUtils.js
@@ -0,0 +1,9 @@ +import fs from "node:fs/promises"; + +export async function readSecret(path) { + if (!path) { + throw new Error("Path to secret file is required"); + } + const content = await fs.readFile(path, "utf8"); + return content.trim(); +}