Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
2 changes: 2 additions & 0 deletions backend/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.env
3 changes: 3 additions & 0 deletions backend/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/bookmyvenue
JWT_SECRET= your_jwt_secret_key
FRONTEND_URL=http://localhost:5173
7 changes: 7 additions & 0 deletions backend/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"singleQuote": true,
"trailingComma": "es5",
"tabWidth": 2,
"semi": true,
"printWidth": 100
}
13 changes: 13 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM node:20

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 5005

CMD ["npm", "run", "dev"]
11 changes: 11 additions & 0 deletions backend/drizzle.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'dotenv/config';
import { defineConfig } from 'drizzle-kit';

export default defineConfig({
out: './drizzle',
schema: './src/models/index.js',
dialect: 'postgresql',
dbCredentials: {
url: 'postgresql://postgres:postgres@localhost:5432/bookmyvenue',
},
});
8 changes: 8 additions & 0 deletions backend/drizzle/0000_nervous_midnight.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE "users" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"username" varchar(255) NOT NULL,
"email" varchar(255) NOT NULL,
"password" varchar(255) NOT NULL,
"salt" text NOT NULL,
CONSTRAINT "users_email_unique" UNIQUE("email")
);
71 changes: 71 additions & 0 deletions backend/drizzle/meta/0000_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"id": "c7508fa4-824c-4a94-8492-0985c35edf7c",
"prevId": "00000000-0000-0000-0000-000000000000",
"version": "7",
"dialect": "postgresql",
"tables": {
"public.users": {
"name": "users",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"username": {
"name": "username",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
},
"email": {
"name": "email",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
},
"password": {
"name": "password",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
},
"salt": {
"name": "salt",
"type": "text",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"users_email_unique": {
"name": "users_email_unique",
"nullsNotDistinct": false,
"columns": [
"email"
]
}
},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
}
},
"enums": {},
"schemas": {},
"sequences": {},
"roles": {},
"policies": {},
"views": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}
13 changes: 13 additions & 0 deletions backend/drizzle/meta/_journal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "7",
"dialect": "postgresql",
"entries": [
{
"idx": 0,
"version": "7",
"when": 1780129487718,
"tag": "0000_nervous_midnight",
"breakpoints": true
}
]
}
99 changes: 99 additions & 0 deletions backend/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import express from 'express';
import dotenv from 'dotenv';
import routes from './src/routes/index.js';
import { globalErrorHandler } from './src/handlers/error_handlers.js';
import cors from 'cors';
import cookieParser from 'cookie-parser';
import { WebSocketServer } from 'ws';
import { addClient, removeClient } from './src/utils/wsClient.js';
import { verifyToken } from './src/utils/utils.js';
import {parseCookies} from './src/utils/utils.js';

import conversationService from './src/services/conversationService.js';

dotenv.config();

const app = express();
const PORT = process.env.PORT || 5005;

app.use(express.json());
app.use(cookieParser());
app.use(cors({
origin: 'http://localhost:5173',
credentials: true,
}));

app.use((req, res, next) => {
console.log(`Incoming Request: ${req.method} ${req.url}`);
next();
});

app.get('/', (req, res) => {
res.send('Server is up and running');
});

app.use(routes);
app.use(globalErrorHandler);

// capture the return value of app.listen as server
const server = app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});

const wss = new WebSocketServer({ noServer: true });

server.on('upgrade', (req, socket, head) => {
try {
const cookies = parseCookies(req);
const token = cookies['accessToken'];

if (!token) {
socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n');
socket.destroy();
return;
}

const decoded = verifyToken(token);
req.user = { id: decoded.userId, email: decoded.email, role: decoded.role };

wss.handleUpgrade(req, socket, head, (ws) => {
wss.emit('connection', ws, req);
});
} catch (err) {
socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n');
socket.destroy();
}
});

wss.on('connection', (ws, req) => {
const userId = req.user.id;
addClient(userId, ws);
console.log(`WS connected: ${userId}`);

ws.on('message', async (data) => {
try {
const { type, payload } = JSON.parse(data);
if (type === 'SEND_MESSAGE') {
const { conversationId, content, venueId } = payload;
await conversationService.sendMessage(
conversationId,
userId,
content,
venueId
);
}
} catch (err) {
console.error(`Error handling message for ${userId}:`, err);
}
});

ws.on('close', () => {
removeClient(userId, ws);
console.log(`WS disconnected: ${userId}`);
});

ws.on('error', (err) => {
console.error(`WS error for ${userId}:`, err);
removeClient(userId, ws);
});
});
Loading