Serve client from server as static and cleanup env fallbacks

This commit is contained in:
pedrocx486 2025-01-27 23:06:11 -03:00
parent d2aa0d04df
commit 37da346aa4

View file

@ -6,15 +6,16 @@ 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';
dotenv.config(); dotenv.config();
const app = express(); const app = express();
const PORT = Number(process.env.PORT) || 3000; const PORT = Number(process.env.PORT);
// CORS configuration options // CORS configuration options
const corsOptions = { const corsOptions = {
origin: process.env.CLIENT_URL || 'http://localhost:5173', origin: process.env.CLIENT_URL,
optionsSuccessStatus: 200, optionsSuccessStatus: 200,
methods: ['GET', 'POST', 'DELETE'], methods: ['GET', 'POST', 'DELETE'],
allowedHeaders: ['Content-Type', 'Authorization'] allowedHeaders: ['Content-Type', 'Authorization']
@ -34,9 +35,15 @@ app.use(cors(corsOptions));
app.use(bodyParser.json()); app.use(bodyParser.json());
app.use(helmet()); app.use(helmet());
app.use(limiter); app.use(limiter);
app.use('/', express.static(path.join(__dirname, '../static')));
// Navigate to the admin route in the client
app.get('*', (_req, res) => {
res.sendFile(path.resolve(__dirname, '../static', 'index.html'));
});
// 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}`);
@ -46,7 +53,7 @@ if(process.env.environment !== 'development') {
} }
// Maximum number of party lines allowed // Maximum number of party lines allowed
const MAX_PARTY_LINES = Number(process.env.MAX_PARTY_LINES) || 5; const MAX_PARTY_LINES = Number(process.env.MAX_PARTY_LINES);
// In-memory storage for party lines // In-memory storage for party lines
const partyLines = {}; const partyLines = {};
@ -194,7 +201,7 @@ app.post('/createPartyLine', [
// Create a new party line // Create a new party line
partyLines[currentPartyLine] = { partyLines[currentPartyLine] = {
clients: [], clients: [],
lastEvent: process.env.INITIAL_RUMOR || '...nothing has been said, yet.', lastEvent: process.env.INITIAL_RUMOR,
lastActivity: Date.now() lastActivity: Date.now()
}; };