From 4abe5a07f271cf0de5158bfbd464dc5a3d10e5ac Mon Sep 17 00:00:00 2001 From: Sam Hadow Date: Sun, 9 Feb 2025 00:07:46 +0100 Subject: [PATCH] database --- Makefile | 6 ++++-- package.json | 1 + src/controllers/main.js | 3 +++ src/db.js | 36 ++++++++++++++++++++++++++++++++++++ src/public/ecc.js | 0 src/public/index.html | 3 ++- src/routes/root.js | 4 ++++ 7 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 src/db.js create mode 100644 src/public/ecc.js diff --git a/Makefile b/Makefile index 4689cfd..e3b8cd1 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,12 @@ run: clean build - podman run --replace -dt -p 3333:3333 --name=e2ee e2ee-messaging-service:latest + podman pod create --name=e2ee -p 3333:3333 + podman run -d --pod=e2ee -e POSTGRES_PASSWORD="password" -e POSTGRES_DB="e2ee" -e POSTGRES_USER="e2ee" -e POSTGRES_INITDB_ARGS="--encoding=UTF-8 --lc-collate=C --lc-ctype=C" --name=e2ee-db docker.io/library/postgres:15 + podman run -d --pod=e2ee --name=e2ee-app e2ee-messaging-service:latest build: podman build -t e2ee-messaging-service . test: curl http://localhost:3333 podman logs e2ee clean: - podman rm -f e2ee + podman pod rm -f e2ee diff --git a/package.json b/package.json index 6f36b00..39d1684 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "bootstrap": "^5.3.3", "cookie-parser": "^1.4.7", "express": "^4.21.2", + "pg": "^8.13.1", "socket.io": "^4.8.1" } } diff --git a/src/controllers/main.js b/src/controllers/main.js index e225050..db3db58 100644 --- a/src/controllers/main.js +++ b/src/controllers/main.js @@ -9,6 +9,9 @@ const mainController = { }, script: (req, res) => { res.sendFile(path.resolve(__dirname + '/../public/script.js')); + }, + ecc: (req, res) => { + res.sendFile(path.resolve(__dirname + '/../public/ecc.js')); } }; diff --git a/src/db.js b/src/db.js new file mode 100644 index 0000000..9e5e654 --- /dev/null +++ b/src/db.js @@ -0,0 +1,36 @@ +const { Client } = require('pg'); + +const dbConfig = { + user: process.env.POSTGRES_USER || 'e2ee', + password: process.env.POSTGRES_PASSWORD, + host: process.env.POSTGRES_HOST || '127.0.0.1', + port: process.env.POSTGRES_PORT || '5432', + database: process.env.POSTGRES_DB || 'e2ee', +}; + +const client = new Client(dbConfig); + +const database = { + init: () => { + client.connect().then(() => { + let querryString = ` + CREATE table user ( + uuid integer primary key, + pubkey text + ); + ` + client.query(querryString, (err, _) => { + if (err) { + console.error('Error executing query', err); + } + client.end().then(() => {}).catch((err) => { + console.error('Error closing connection', err); + }); + }); + }).catch((err) => { + console.error('Error connecting to database ', err); + }); + } +}; + +module.exports = database; diff --git a/src/public/ecc.js b/src/public/ecc.js new file mode 100644 index 0000000..e69de29 diff --git a/src/public/index.html b/src/public/index.html index 8e0a5ea..76009c0 100644 --- a/src/public/index.html +++ b/src/public/index.html @@ -6,7 +6,8 @@ - + + diff --git a/src/routes/root.js b/src/routes/root.js index 3e2cbb7..a6d9068 100644 --- a/src/routes/root.js +++ b/src/routes/root.js @@ -14,4 +14,8 @@ router .route("/script.js") .get(mainController.script); +router + .route("/ecc.js") + .get(mainController.ecc); + module.exports = router;