-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
45 changed files
with
3,932 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
api-gateway/templates/api_conf.d/private_conf.d/collab_service.conf.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
location /private/collab-service/ { | ||
proxy_pass http://collab-service/; | ||
proxy_http_version 1.1; | ||
proxy_set_header Upgrade $http_upgrade; | ||
proxy_set_header Connection $connection_upgrade; | ||
proxy_set_header Host $host; | ||
proxy_set_header X-Real-IP $remote_addr; | ||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | ||
proxy_set_header X-Forwarded-Proto $scheme; | ||
} |
23 changes: 23 additions & 0 deletions
23
api-gateway/templates/api_conf.d/public_conf.d/collab_service.conf.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
location /public/collab-service/ { | ||
location /public/collab-service/chat { | ||
proxy_pass http://collab-service/chat; | ||
proxy_http_version 1.1; | ||
proxy_set_header Upgrade $http_upgrade; | ||
proxy_set_header Connection $connection_upgrade; | ||
proxy_set_header Host $host; | ||
proxy_set_header X-Real-IP $remote_addr; | ||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | ||
proxy_set_header X-Forwarded-Proto $scheme; | ||
} | ||
|
||
location /public/collab-service/yjs { | ||
proxy_pass http://collab-service/yjs; | ||
proxy_http_version 1.1; | ||
proxy_set_header Upgrade $http_upgrade; | ||
proxy_set_header Connection $connection_upgrade; | ||
proxy_set_header Host $host; | ||
proxy_set_header X-Real-IP $remote_addr; | ||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | ||
proxy_set_header X-Forwarded-Proto $scheme; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"trailingComma": "es5", | ||
"tabWidth": 2, | ||
"semi": true, | ||
"singleQuote": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Base image | ||
FROM node:20-alpine AS base | ||
WORKDIR /app | ||
COPY package*.json ./ | ||
|
||
# Install dependencies in base stage | ||
RUN npm install | ||
|
||
# Development stage | ||
FROM base AS dev | ||
COPY . . | ||
# Run the JavaScript code directly in dev mode (useful for hot-reloading) | ||
CMD ["npm", "run", "dev"] | ||
|
||
# Production stage | ||
FROM node:20-alpine AS prod | ||
WORKDIR /app | ||
COPY --from=base /app/node_modules ./node_modules | ||
COPY . . | ||
# Start the server in production mode | ||
CMD ["npm", "start"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { sendAiMessage } from "../model/repository.js"; | ||
|
||
// send ai message | ||
export async function sendAiMessageController(req, res) { | ||
const { message } = req.body; | ||
if (!message) { | ||
return res.status(400).json({ error: "Message content is required" }); | ||
} | ||
|
||
const data = await sendAiMessage(message); | ||
const aiResponse = | ||
data.choices?.[0]?.message?.content || "No response from AI"; | ||
|
||
if (aiResponse) { | ||
res.status(200).json({ data }); | ||
} else { | ||
res.status(500).json({ error: "Failed to retrieve AI response" }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { | ||
newRoom, | ||
getRoomId, | ||
getAllRooms, | ||
fetchRoomChatHistory, | ||
getQuestionIdByRoomId, | ||
getAllRoomsByUserId, | ||
} from "../model/repository.js"; | ||
import crypto from "crypto"; | ||
|
||
// Create a room between two users | ||
export async function createRoom(req, res) { | ||
const { user1, user2, question_id } = req.body; | ||
|
||
if (!user1 || !user2 || !question_id) { | ||
return res | ||
.status(400) | ||
.json({ error: "user1,user2 and question_id are required" }); | ||
} | ||
|
||
// Generate a unique room ID by hashing the two user IDs | ||
const timeSalt = new Date().toISOString().slice(0, 19); | ||
const roomId = crypto | ||
.createHash("sha256") | ||
.update(user1 + user2 + timeSalt) | ||
.digest("hex"); | ||
const room = await newRoom(user1, user2, roomId, question_id); | ||
|
||
if (room) { | ||
res.status(201).json(room); | ||
} else { | ||
res.status(500).json({ error: "Failed to create room" }); | ||
} | ||
} | ||
|
||
// Get room ID by user | ||
export async function getRoomByUser(req, res) { | ||
const { user } = req.params; | ||
|
||
if (!user) { | ||
return res.status(400).json({ error: "User is required" }); | ||
} | ||
|
||
const room = await getRoomId(user); | ||
|
||
if (room) { | ||
res.status(200).json(room); | ||
} else { | ||
res.status(404).json({ error: `Room not found for user: ${user}` }); | ||
} | ||
} | ||
|
||
// Get all rooms | ||
export async function getAllRoomsController(req, res) { | ||
const rooms = await getAllRooms(); | ||
|
||
if (rooms) { | ||
res.status(200).json(rooms); | ||
} else { | ||
res.status(500).json({ error: "Failed to retrieve rooms" }); | ||
} | ||
} | ||
|
||
// Get all rooms by user | ||
export async function getAllRoomsByUser(req, res) { | ||
const { user } = req.params; | ||
const rooms = await getAllRoomsByUserId(user); | ||
|
||
if (rooms) { | ||
res.status(200).json(rooms); | ||
} else { | ||
res.status(500).json({ error: "Failed to retrieve rooms" }); | ||
} | ||
} | ||
export async function getRoomChatHistory(req, res) { | ||
const { roomId } = req.params; | ||
|
||
if (!roomId) { | ||
return res.status(400).json({ error: "Room ID is required" }); | ||
} | ||
|
||
const room = await fetchRoomChatHistory(roomId); | ||
|
||
if (room) { | ||
res.status(200).json(room); | ||
} else { | ||
res.status(404).json({ error: `Room not found for ID: ${roomId}` }); | ||
} | ||
} | ||
|
||
// Get QuestionId from the room based on the roomId | ||
export async function getQuestionId(req, res) { | ||
const { roomId } = req.params; | ||
|
||
if (!roomId) { | ||
return res.status(400).json({ error: "Room ID is required" }); | ||
} | ||
const questionId = await getQuestionIdByRoomId(roomId); | ||
|
||
if (questionId) { | ||
res.status(200).json({ questionId }); | ||
} else { | ||
res | ||
.status(404) | ||
.json({ error: `Question ID not found for room ID: ${roomId}` }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import express from "express"; | ||
import cors from "cors"; | ||
import collabRoutes from "./routes/collab-routes.js"; | ||
|
||
const app = express(); | ||
|
||
app.use(express.urlencoded({ extended: true })); | ||
app.use(express.json()); | ||
app.use(cors()); // config cors so that front-end can use | ||
app.options("*", cors()); | ||
|
||
// To handle CORS Errors | ||
app.use((req, res, next) => { | ||
res.header("Access-Control-Allow-Origin", "*"); // "*" -> Allow all links to access | ||
|
||
res.header( | ||
"Access-Control-Allow-Headers", | ||
"Origin, X-Requested-With, Content-Type, Accept, Authorization" | ||
); | ||
|
||
// Browsers usually send this before PUT or POST Requests | ||
if (req.method === "OPTIONS") { | ||
res.header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, PATCH"); | ||
return res.status(200).json({}); | ||
} | ||
|
||
// Continue Route Processing | ||
next(); | ||
}); | ||
|
||
app.use("/collab", collabRoutes); | ||
|
||
app.get("/", (req, res, next) => { | ||
console.log("Sending Greetings!"); | ||
res.json({ | ||
message: "Hello World from collab-service", | ||
}); | ||
}); | ||
|
||
// Handle When No Route Match Is Found | ||
app.use((req, res, next) => { | ||
const error = new Error("Route Not Found"); | ||
error.status = 404; | ||
next(error); | ||
}); | ||
|
||
app.use((error, req, res, next) => { | ||
res.status(error.status || 500); | ||
res.json({ | ||
error: { | ||
message: error.message, | ||
}, | ||
}); | ||
}); | ||
|
||
export default app; |
Oops, something went wrong.