diff --git a/Dockerfile b/Dockerfile index 7aa111b..3a5e789 100644 --- a/Dockerfile +++ b/Dockerfile @@ -31,6 +31,8 @@ USER node # Copy the rest of the source files into the image. COPY . . +CMD npm i + # Expose the port that the application listens on. EXPOSE 3000 diff --git a/backend/db.js b/backend/db.js index 4b8d3bc..351ce5e 100644 --- a/backend/db.js +++ b/backend/db.js @@ -10,7 +10,7 @@ const db = new Client({ }); // create connection to database -db .connect() +db.connect() .then(() => console.log('Successfully connected to database')) .catch((err) => console.log('Error on connecting to database: ', err)); diff --git a/backend/socket.js b/backend/socket.js index 71cdcc0..4778dc8 100644 --- a/backend/socket.js +++ b/backend/socket.js @@ -4,6 +4,28 @@ const jwtSecret = process.env.JWT_SECRET; const { db } = require('./db.js'); function initializeSocket(server) { + async function createTables() { + await db.query(` + CREATE TABLE IF NOT EXISTS accounts ( + id SERIAL PRIMARY KEY, + username VARCHAR(255) UNIQUE NOT NULL, + password VARCHAR(255) NOT NULL, + email VARCHAR(255) UNIQUE NOT NULL, + ) + `); + + // Create messages table + await db.query(` + CREATE TABLE IF NOT EXISTS messages ( + id SERIAL PRIMARY KEY, + content TEXT NOT NULL, + username VARCHAR(255) NOT NULL, + recipient VARCHAR(255) NOT NULL, + ) + `); + + } + const io = new Server(server, { cookie: { httpOnly: true, diff --git a/compose.yaml b/compose.yaml index 27da108..cf92d16 100644 --- a/compose.yaml +++ b/compose.yaml @@ -1,3 +1,5 @@ +version: '3.8' + services: web-chat: build: @@ -5,27 +7,27 @@ services: environment: NODE_ENV: production ports: - - 3000:3000 - + - "3000:3000" depends_on: - db: - condition: service_healthy + - db + db: - image: postgres:latest - restart: always - user: ${PG_USER} - volumes: - - db-data:/var/lib/postgresql/data - environment: - - POSTGRES_HOST=${PG_HOST} - - POSTGRES_DB=${PG_DATABASE} - - POSTGRES_PASSWORD=${PG_PASSWORD} - expose: - - ${PG_PORT}:5432 - healthcheck: - test: [ "CMD", "pg_isready" ] - interval: 10s - timeout: 5s - retries: 5 + image: postgres:latest + restart: always + user: postgres + volumes: + - db-data:/var/lib/postgresql/data + environment: + POSTGRES_USER: postgres + POSTGRES_DB: webchat + POSTGRES_PASSWORD: jebanechaslo + expose: + - "5432" + healthcheck: + test: ["CMD", "pg_isready", "-U", "${PG_USER}"] + interval: 10s + timeout: 5s + retries: 5 + volumes: - db-data: + db-data: