Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
zylee348 committed Nov 13, 2023
2 parents 766cb50 + d04416b commit f905cdb
Show file tree
Hide file tree
Showing 97 changed files with 118,398 additions and 1,455 deletions.
Binary file added .DS_Store
Binary file not shown.
29 changes: 0 additions & 29 deletions .github/workflows/node.js.yml

This file was deleted.

11 changes: 7 additions & 4 deletions .github/workflows/question-service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
pull_request:

jobs:
build-and-test:
build-and-run-server:
runs-on: ubuntu-latest
env:
ENV: PROD
Expand All @@ -23,6 +23,9 @@ jobs:
- name: Install Dependencies
working-directory: ./src/backend/question_backend
run: npm i
- name: Run Tests
working-directory: ./src/backend/question_backend
run: npm test
- name: Install typescript
working-directory: ./src/cypress
run: npm install --save-dev typescript
- name: Run test cases
working-directory: ./src
run: node backend/question_backend/index.js & npx cypress run --spec "cypress/e2e/question_api.cy.js"
31 changes: 0 additions & 31 deletions .github/workflows/user-service.yml

This file was deleted.

Binary file modified src/.DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions src/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
npm-debug.log
10 changes: 0 additions & 10 deletions src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,6 @@ button {
font-size: calc(10px + 2vmin);
}

.main{
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #212121;
color: whitesmoke;
flex-direction: column;
}

.main-header{
font-family: 'Montserrat', sans-serif;
}
Expand Down
11 changes: 9 additions & 2 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import Settings from "./pages/Settings";
import UserProfile from "./pages/UserProfile";
import Home from './pages/home';
import MatchFound from "./components/matching_elements/matchFound"
import Room from "./pages/Room"
import ChatboxPage from "./pages/ChatboxPage";

// import Login from "./pages/Login";
// import Profile from "./components/profile";
import { BrowserRouter as Router, Route, Routes } from "react-router-dom";
Expand All @@ -15,6 +18,8 @@ import Logout from "./pages/Logout";
import { ModalsProvider } from "@mantine/modals";
import { Notifications } from '@mantine/notifications';
import '@mantine/notifications/styles.css';
import TagQuestions from "./pages/TagQuestions";
import QuestionNavbar from "./components/question_crud/questionNavbar";

function App() {

Expand All @@ -28,12 +33,14 @@ function App() {
<Route path="/signup" element={<Signup />} />
<Route path="/profile" element={<UserProfile />} />
<Route path="/viewQuestions" element={<ViewQuestions />} />
<Route path="tagQuestions" element={<TagQuestions/>} />
<Route path="/settings" element={<Settings />} />
<Route path="/matchFound" element={<MatchFound/>}/>
<Route path="/room/:roomID" element={<Room/>}/>
<Route path="/logout" element={<Logout/>} />
<Route path="/chatbox" element={<ChatboxPage/>} />
</Routes>
</Router>
<Notifications />
<Notifications/>
</ModalsProvider>
</MantineProvider>
);
Expand Down
Binary file added src/backend/.DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions src/backend/chatbox_backend/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
npm-debug.log
23 changes: 23 additions & 0 deletions src/backend/chatbox_backend/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var cors = require('cors');

var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();
app.use(cors()); // add this line

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);

module.exports = app;
76 changes: 76 additions & 0 deletions src/backend/chatbox_backend/chat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const uuidv4 = require('uuid').v4;

const messages = new Set();
const users = new Map();

const defaultUser = {
id: 'anon',
name: 'Anonymous',
};

const messageExpirationTimeMS = 5*60 * 10000;

class Connection {
constructor(io, socket) {
this.socket = socket;
this.io = io;

socket.on('getMessages', () => this.getMessages());
socket.on('message', (value) => this.handleMessage(value));
socket.on('disconnect', () => this.disconnect());
socket.on('connect_error', (err) => {
console.log(`connect_error due to ${err.message}`);
});
socket.on('leaveRoom', (roomID) => this.leaveRoom(roomID));
socket.on('joinRoom', (roomID) => this.joinRoom(roomID));
}

leaveRoom(roomID) {
this.socket.leave(roomID);
}

joinRoom(roomID) {
this.socket.join(roomID);
}

sendMessage(message) {
const roomID = message.value.room;
this.io.sockets.to(roomID).emit('message', message);
}

getMessages() {
messages.forEach((message) => this.sendMessage(message));
}

handleMessage(value) {
const message = {
id: uuidv4(),
user: users.get(this.socket) || defaultUser,
value,
time: Date.now()
};

messages.add(message);
this.sendMessage(message);

setTimeout(
() => {
messages.delete(message);
this.io.sockets.emit('deleteMessage', message.id);
},
messageExpirationTimeMS,
);
}

disconnect() {
users.delete(this.socket);
}
}

function chat(io) {
io.on('connection', (socket) => {
new Connection(io, socket);
});
};

module.exports = chat;
15 changes: 15 additions & 0 deletions src/backend/chatbox_backend/dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM node:18

WORKDIR /usr/src/chatbox_backend

COPY package*.json ./

RUN npm install

# RUN npm ci --omit=dev

COPY . .

EXPOSE 3000

CMD ["node", "index.js"]
103 changes: 103 additions & 0 deletions src/backend/chatbox_backend/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#!/usr/bin/env node

var PORT = '3000';

var chat = require('./chat');
var socketio = require('socket.io');

/**
* Module dependencies.
*/

var app = require('./app');
var debug = require('debug')('chat-server:server');
var http = require('http');

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || PORT);
app.set('port', port);

/**
* Create HTTP server.
*/

var server = http.createServer(app);
var io = socketio(server,{
cors: {
origin: '*',
methods: ['GET', 'POST']
}
});
chat(io);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
console.log(`Listening on port ${PORT}`);
server.on('error', onError);
server.on('listening', onListening);

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
var port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}

var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
Loading

0 comments on commit f905cdb

Please sign in to comment.