forked from CS3219-AY2324S1/ay2324s1-course-assessment-g44
-
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.
Merge branch 'CI/CD' of https://github.com/CS3219-AY2324S1/ay2324s1-c…
…ourse-assessment-g44 into CI-CD
- Loading branch information
Showing
97 changed files
with
118,398 additions
and
1,455 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
Binary file not shown.
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 | ||
npm-debug.log |
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
Binary file not shown.
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 | ||
npm-debug.log |
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 @@ | ||
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; |
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,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; |
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,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"] |
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,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); | ||
} |
Oops, something went wrong.