Skip to content

Commit ba98cf9

Browse files
committed
what the sigma
1 parent 6ab7b07 commit ba98cf9

File tree

12 files changed

+1453
-2
lines changed

12 files changed

+1453
-2
lines changed

collab-service/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

collab-service/Dockerfile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Base image
2+
FROM node:20-alpine AS base
3+
WORKDIR /app
4+
COPY package*.json ./
5+
6+
# Development stage
7+
FROM base AS dev
8+
RUN npm install
9+
COPY . .
10+
# Run the TypeScript code directly in dev mode (useful for hot-reloading)
11+
CMD ["npm", "run", "dev"]
12+
13+
# Build stage
14+
FROM base AS build
15+
RUN npm install
16+
COPY . .
17+
RUN npm run build
18+
19+
# Production stage
20+
FROM node:20-alpine AS prod
21+
WORKDIR /app
22+
COPY --from=build /app/dist ./dist
23+
COPY package*.json ./
24+
RUN npm ci --only=production
25+
CMD ["node", "dist/index.js"]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Request } from 'express';
2+
3+
interface CreateRoomRequestBody {
4+
user1: string;
5+
user2: string;
6+
}
7+
8+
export async function create_room(req: Request<{}, {}, CreateRoomRequestBody>, res) {
9+
// this function is to create a connection between two users
10+
// the connection is store in mongodb
11+
12+
const { user1, user2 } = req.body;
13+
14+
}

collab-service/app/index.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import express, { Application, Request, Response, NextFunction } from 'express';
2+
import cors from 'cors';
3+
import collabRoutes from './routes/collab-routes';
4+
5+
const app: Application = express();
6+
7+
app.use(express.urlencoded({ extended: true }));
8+
app.use(express.json());
9+
app.use(cors()); // config cors so that front-end can use
10+
app.options("*", cors());
11+
12+
13+
// To handle CORS Errors
14+
app.use((req, res, next) => {
15+
res.header("Access-Control-Allow-Origin", "*"); // "*" -> Allow all links to access
16+
17+
res.header(
18+
"Access-Control-Allow-Headers",
19+
"Origin, X-Requested-With, Content-Type, Accept, Authorization",
20+
);
21+
22+
// Browsers usually send this before PUT or POST Requests
23+
if (req.method === "OPTIONS") {
24+
res.header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, PATCH");
25+
return res.status(200).json({});
26+
}
27+
28+
// Continue Route Processing
29+
next();
30+
});
31+
32+
33+
app.get("/", (req, res, next) => {
34+
console.log("Sending Greetings!");
35+
res.json({
36+
message: "Hello World from collab-service",
37+
});
38+
});
39+
40+
// Handle When No Route Match Is Found
41+
app.use((req, res, next) => {
42+
const error: any = new Error("Route Not Found");
43+
error.status = 404;
44+
next(error);
45+
});
46+
47+
app.use((error, req, res, next) => {
48+
res.status(error.status || 500);
49+
res.json({
50+
error: {
51+
message: error.message,
52+
},
53+
});
54+
});
55+
56+
app.use("/collab", collabRoutes);
57+
58+
export default app;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { connect } from 'mongoose';
2+
import UsersSession from './usersSession-model';
3+
import mongoose from 'mongoose';
4+
5+
export async function connectToMongo() {
6+
await connect('mongodb+srv://admin:[email protected]/');
7+
}
8+
9+
export async function createRoom(user1: string, user2: string, roomId: string) {
10+
try {
11+
const newRoom = new UsersSession({
12+
users: [user1, user2],
13+
roomId: new mongoose.Types.ObjectId().toString(), // Generate a unique room ID
14+
lastUpdated: new Date()
15+
});
16+
17+
const savedRoom = await newRoom.save();
18+
return savedRoom;
19+
} catch (error) {
20+
console.error('Error creating room:', error);
21+
return null;
22+
}
23+
}
24+
25+
export async function get_roomID(user: string): Promise<mongoose.Document | null> {
26+
try {
27+
const room = await UsersSession.findOne({ users: user });
28+
return room;
29+
} catch (error) {
30+
console.error('Error finding room for ${user}:', error);
31+
return null;
32+
}
33+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import mongoose from 'mongoose';
2+
3+
const Schema = mongoose.Schema;
4+
5+
const usersSessionSchema = new Schema({
6+
users: {
7+
type: [String],
8+
required: true
9+
},
10+
roomId: {
11+
type: String,
12+
required: true
13+
},
14+
lastUpdated: {
15+
type: Date,
16+
required: true,
17+
default: Date.now
18+
}
19+
});
20+
21+
export default mongoose.model('UsersSession', usersSessionSchema);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import express from "express";
2+
3+
import { create_room } from "../controller/collab-controller";
4+
5+
const router = express.Router();
6+
7+
router.post("/create-room", create_room);
8+
9+
export default router;

collab-service/app/server.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { connectToMongo } from "./model/repository";
2+
import http from "http";
3+
import { index } from "./index.ts";
4+
const PORT = process.env.PORT || 5000;
5+
const server = http.createServer(index);

0 commit comments

Comments
 (0)