Add docker detection and lint
This commit is contained in:
parent
b9a57d6083
commit
3018bf2ee2
2 changed files with 34 additions and 29 deletions
|
@ -11,3 +11,4 @@ services:
|
||||||
MAX_PARTY_LINES: '1'
|
MAX_PARTY_LINES: '1'
|
||||||
INITIAL_RUMOR: '...nothing has been said, yet.'
|
INITIAL_RUMOR: '...nothing has been said, yet.'
|
||||||
ENVIRONMENT: 'production'
|
ENVIRONMENT: 'production'
|
||||||
|
DOCKER: 'true'
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
import express from 'express';
|
import express from 'express';
|
||||||
import { body, query, validationResult } from 'express-validator';
|
import {body, query, validationResult} from 'express-validator';
|
||||||
import rateLimit from 'express-rate-limit';
|
import rateLimit from 'express-rate-limit';
|
||||||
import cors from 'cors';
|
import cors from 'cors';
|
||||||
import bodyParser from 'body-parser';
|
import bodyParser from 'body-parser';
|
||||||
import { v4 as uuidv4 } from 'uuid';
|
import {v4 as uuidv4} from 'uuid';
|
||||||
import dotenv from 'dotenv';
|
import dotenv from 'dotenv';
|
||||||
import helmet from 'helmet';
|
import helmet from 'helmet';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
@ -43,7 +43,7 @@ app.get('*', (_req, res) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
// Force HTTPS redirection in production
|
// Force HTTPS redirection in production
|
||||||
if(process.env.ENVIRONMENT !== 'development') {
|
if (process.env.ENVIRONMENT !== 'development') {
|
||||||
app.use((req, res, next) => {
|
app.use((req, res, next) => {
|
||||||
if (req.headers['x-forwarded-proto'] !== 'https') {
|
if (req.headers['x-forwarded-proto'] !== 'https') {
|
||||||
return res.redirect(`https://${req.headers.host}${req.url}`);
|
return res.redirect(`https://${req.headers.host}${req.url}`);
|
||||||
|
@ -83,18 +83,18 @@ app.get('/joinPartyLine', [
|
||||||
], (req: any, res: any) => {
|
], (req: any, res: any) => {
|
||||||
const errors = validationResult(req);
|
const errors = validationResult(req);
|
||||||
if (!errors.isEmpty()) {
|
if (!errors.isEmpty()) {
|
||||||
return res.status(400).json({ errors: errors.array() });
|
return res.status(400).json({errors: errors.array()});
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const currentPartyLine = req.query.partyLine;
|
const currentPartyLine = req.query.partyLine;
|
||||||
const partyLine = partyLines[currentPartyLine];
|
const partyLine = partyLines[currentPartyLine];
|
||||||
if (!partyLine) {
|
if (!partyLine) {
|
||||||
return res.status(404).send({ status: 'Party line not found' });
|
return res.status(404).send({status: 'Party line not found'});
|
||||||
}
|
}
|
||||||
res.status(200).send({ status: 'Connection to party line authorized' });
|
res.status(200).send({status: 'Connection to party line authorized'});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('ERROR: Failed to join party line', error);
|
console.error('ERROR: Failed to join party line', error);
|
||||||
res.status(500).send({ status: 'Internal server error' });
|
res.status(500).send({status: 'Internal server error'});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -104,13 +104,13 @@ app.get('/connectPartyLine', [
|
||||||
], (req: any, res: any) => {
|
], (req: any, res: any) => {
|
||||||
const errors = validationResult(req);
|
const errors = validationResult(req);
|
||||||
if (!errors.isEmpty()) {
|
if (!errors.isEmpty()) {
|
||||||
return res.status(400).json({ errors: errors.array() });
|
return res.status(400).json({errors: errors.array()});
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const connectedPartyLine = req.query.partyLine;
|
const connectedPartyLine = req.query.partyLine;
|
||||||
const partyLine = partyLines[connectedPartyLine];
|
const partyLine = partyLines[connectedPartyLine];
|
||||||
if (!partyLine) {
|
if (!partyLine) {
|
||||||
return res.status(404).send({ status: 'Party line not found' });
|
return res.status(404).send({status: 'Party line not found'});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set headers for Server-Sent Events (SSE)
|
// Set headers for Server-Sent Events (SSE)
|
||||||
|
@ -119,7 +119,7 @@ app.get('/connectPartyLine', [
|
||||||
res.setHeader('Connection', 'keep-alive');
|
res.setHeader('Connection', 'keep-alive');
|
||||||
|
|
||||||
const clientId = `${uuidv4()}`;
|
const clientId = `${uuidv4()}`;
|
||||||
const client = { clientId, response: res };
|
const client = {clientId, response: res};
|
||||||
|
|
||||||
// Add client to the party line
|
// Add client to the party line
|
||||||
partyLine.clients.push(client);
|
partyLine.clients.push(client);
|
||||||
|
@ -162,7 +162,7 @@ app.get('/connectPartyLine', [
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('ERROR: Failed to connect to party line', error);
|
console.error('ERROR: Failed to connect to party line', error);
|
||||||
res.status(500).send({ status: 'Internal server error' });
|
res.status(500).send({status: 'Internal server error'});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -187,15 +187,15 @@ app.post('/createPartyLine', [
|
||||||
], (req: any, res: any) => {
|
], (req: any, res: any) => {
|
||||||
const errors = validationResult(req);
|
const errors = validationResult(req);
|
||||||
if (!errors.isEmpty()) {
|
if (!errors.isEmpty()) {
|
||||||
return res.status(400).json({ errors: errors.array() });
|
return res.status(400).json({errors: errors.array()});
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const { partyLine: currentPartyLine } = req.body;
|
const {partyLine: currentPartyLine} = req.body;
|
||||||
if (Object.keys(partyLines).length >= MAX_PARTY_LINES) {
|
if (Object.keys(partyLines).length >= MAX_PARTY_LINES) {
|
||||||
return res.status(400).send({ status: 'Maximum number of party lines reached' });
|
return res.status(400).send({status: 'Maximum number of party lines reached'});
|
||||||
}
|
}
|
||||||
if (partyLines[currentPartyLine]) {
|
if (partyLines[currentPartyLine]) {
|
||||||
return res.status(400).send({ status: 'Party line already exists' });
|
return res.status(400).send({status: 'Party line already exists'});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a new party line
|
// Create a new party line
|
||||||
|
@ -206,10 +206,10 @@ app.post('/createPartyLine', [
|
||||||
};
|
};
|
||||||
|
|
||||||
console.log(`CREATE: Created party line: ${currentPartyLine}`);
|
console.log(`CREATE: Created party line: ${currentPartyLine}`);
|
||||||
res.status(200).send({ status: 'Party line created', currentPartyLine });
|
res.status(200).send({status: 'Party line created', currentPartyLine});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('ERROR: Failed to create party line', error);
|
console.error('ERROR: Failed to create party line', error);
|
||||||
res.status(500).send({ status: 'Internal server error' });
|
res.status(500).send({status: 'Internal server error'});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -220,13 +220,13 @@ app.post('/rumor', [
|
||||||
], (req: any, res: any) => {
|
], (req: any, res: any) => {
|
||||||
const errors = validationResult(req);
|
const errors = validationResult(req);
|
||||||
if (!errors.isEmpty()) {
|
if (!errors.isEmpty()) {
|
||||||
return res.status(400).json({ errors: errors.array() });
|
return res.status(400).json({errors: errors.array()});
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const { partyLine: connectedPartyLine, rumor } = req.body;
|
const {partyLine: connectedPartyLine, rumor} = req.body;
|
||||||
const partyLine = partyLines[connectedPartyLine];
|
const partyLine = partyLines[connectedPartyLine];
|
||||||
if (!partyLine) {
|
if (!partyLine) {
|
||||||
return res.status(404).send({ status: 'Party line not found' });
|
return res.status(404).send({status: 'Party line not found'});
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`RECEIVE: Received rumor for party line ${connectedPartyLine}: ${rumor}`);
|
console.log(`RECEIVE: Received rumor for party line ${connectedPartyLine}: ${rumor}`);
|
||||||
|
@ -236,17 +236,17 @@ app.post('/rumor', [
|
||||||
partyLine.clients.forEach((client: any) => {
|
partyLine.clients.forEach((client: any) => {
|
||||||
client.response.write(`data: ${rumor}\n\n`);
|
client.response.write(`data: ${rumor}\n\n`);
|
||||||
});
|
});
|
||||||
res.status(200).send({ status: 'Rumor broadcast' });
|
res.status(200).send({status: 'Rumor broadcast'});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('ERROR: Failed to send event', error);
|
console.error('ERROR: Failed to send event', error);
|
||||||
res.status(500).send({ status: 'Internal server error' });
|
res.status(500).send({status: 'Internal server error'});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Route to get all party lines
|
// Route to get all party lines
|
||||||
app.get('/partyLines', (_req: any, res: any) => {
|
app.get('/partyLines', (_req: any, res: any) => {
|
||||||
const allPartyLines = Object.keys(partyLines);
|
const allPartyLines = Object.keys(partyLines);
|
||||||
res.status(200).send({ allPartyLines });
|
res.status(200).send({allPartyLines});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Route to delete a party line
|
// Route to delete a party line
|
||||||
|
@ -255,13 +255,13 @@ app.delete('/deletePartyLine', [
|
||||||
], (req: any, res: any) => {
|
], (req: any, res: any) => {
|
||||||
const errors = validationResult(req);
|
const errors = validationResult(req);
|
||||||
if (!errors.isEmpty()) {
|
if (!errors.isEmpty()) {
|
||||||
return res.status(400).json({ errors: errors.array() });
|
return res.status(400).json({errors: errors.array()});
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const { partyLine: currentPartyLine } = req.body;
|
const {partyLine: currentPartyLine} = req.body;
|
||||||
const partyLine = partyLines[currentPartyLine];
|
const partyLine = partyLines[currentPartyLine];
|
||||||
if (!partyLine) {
|
if (!partyLine) {
|
||||||
return res.status(404).send({ status: 'Party line not found' });
|
return res.status(404).send({status: 'Party line not found'});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Broadcast deletion message to all clients
|
// Broadcast deletion message to all clients
|
||||||
|
@ -276,14 +276,18 @@ app.delete('/deletePartyLine', [
|
||||||
// Delete the party line
|
// Delete the party line
|
||||||
delete partyLines[currentPartyLine];
|
delete partyLines[currentPartyLine];
|
||||||
console.log(`DELETE: Deleted party line: ${currentPartyLine}`);
|
console.log(`DELETE: Deleted party line: ${currentPartyLine}`);
|
||||||
res.status(200).send({ status: 'Party line deleted', currentPartyLine });
|
res.status(200).send({status: 'Party line deleted', currentPartyLine});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('ERROR: Failed to delete party line', error);
|
console.error('ERROR: Failed to delete party line', error);
|
||||||
res.status(500).send({ status: 'Internal server error' });
|
res.status(500).send({status: 'Internal server error'});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Start the server
|
// Start the server
|
||||||
app.listen(PORT, () => {
|
app.listen(PORT, () => {
|
||||||
console.log(`Rumor Party Line - Running on http://localhost:${PORT}`);
|
console.log(`Rumor Party Line`);
|
||||||
|
console.log(`Internally running on http://localhost:${PORT} in ${process.env.ENVIRONMENT} mode.`);
|
||||||
|
if (process.env.DOCKER) {
|
||||||
|
console.log(`Seems you're running on Docker! Check your external port in the compose file to avoid confusion!`);
|
||||||
|
}
|
||||||
});
|
});
|
Loading…
Add table
Reference in a new issue