From 516bcd0a441085ab81bd707472efab4db2e6846e Mon Sep 17 00:00:00 2001 From: TopherMayor Date: Wed, 29 Apr 2026 22:19:10 -0700 Subject: [PATCH] feat: support external runtime state for hosting --- README.md | 10 +++++++++- server.js | 15 +++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 52c191c..d47174b 100644 --- a/README.md +++ b/README.md @@ -25,9 +25,17 @@ node server.js Votes are stored in `data/votes.json` (created on first run). Edit directly or use the admin panel. System seed data auto-refreshes researched package options and budget scenarios while preserving existing votes and user-added options. +For hosted deployments, set `DATA_DIR` or `DATA_FILE` so mutable vote data lives outside the Git checkout. + ## Deployment -Deployed on `ice:3001` via Node.js directly (not Docker). Routed through Traefik on `ubuntu` via the `cabo-voting.local.tophermayor.com` service route. +The app can run directly under `systemd` with: + +```bash +PORT=3021 DATA_DIR=/srv/state/cabo-voting node server.js +``` + +Traefik can then reverse proxy to the chosen host port. See [Gitea Issues](https://gitea.tophermayor.com/TopherMayor/cabo-voting-app/issues) for the UI/UX roadmap. diff --git a/server.js b/server.js index f9c74a6..4153640 100644 --- a/server.js +++ b/server.js @@ -11,8 +11,13 @@ const app = express(); const server = http.createServer(app); const wss = new WebSocketServer({ server }); -const DATA_DIR = path.join(__dirname, 'data'); -const DATA_FILE = path.join(DATA_DIR, 'votes.json'); +const DEFAULT_DATA_DIR = path.join(__dirname, 'data'); +const DATA_DIR = process.env.DATA_DIR + ? path.resolve(process.env.DATA_DIR) + : DEFAULT_DATA_DIR; +const DATA_FILE = process.env.DATA_FILE + ? path.resolve(process.env.DATA_FILE) + : path.join(DATA_DIR, 'votes.json'); app.use(cors()); app.use(express.json()); @@ -354,6 +359,8 @@ wss.on('connection', (ws) => { }); const PORT = process.env.PORT || 3001; -server.listen(PORT, '0.0.0.0', () => { - console.log(`🏄 Cabo Voting App → http://0.0.0.0:${PORT}`); +const HOST = process.env.HOST || '0.0.0.0'; + +server.listen(PORT, HOST, () => { + console.log(`🏄 Cabo Voting App → http://${HOST}:${PORT}`); });