Split architecture: Trainer=backend, Demo=dashboard
Browse filesDockerfile
- Replaces hard-coded uvicorn CMD with /bin/sh /app/start.sh
- Sets ownership correctly before switching to non-root user
start.sh (new)
- Reads BACKEND_URL env var β writes it into dashboard/js/config.js
- Reads SERVE_DASHBOARD_ONLY env var:
0 / unset β full stack via dashboard_server.py (Trainer Space)
1 β static-only via serve_demo.py (Demo Space)
serve_demo.py (new)
- Minimal FastAPI + StaticFiles server for the Demo space
- Serves dashboard/ on port 7860 with CORS enabled
dashboard/js/config.js (new)
- Sets window.CYBERSOC_BACKEND_URL = '' by default
- Overwritten at container startup by start.sh when BACKEND_URL is set
dashboard/js/api.js
- Reads window.CYBERSOC_BACKEND_URL set by config.js
- _wsUrl() and _httpBase() use the override URL when set,
fall back to same-origin auto-detection when empty
dashboard/index.html
- Loads js/config.js before api.js so the override is available
HF Space setup:
CyberSOC-trainer β no extra env vars (full backend mode)
Demo β SERVE_DASHBOARD_ONLY=1
BACKEND_URL=https://ajay00747-cybersoc-trainer.hf.space
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Files changed (6)
- Dockerfile+14-10
- dashboard/index.html+1
- dashboard/js/api.js+15
- dashboard/js/config.jsAdded+12
- serve_demo.pyAdded+43
- start.shAdded+37
12345678910111213141516171819FROM python:3.10-slim# Create user with home directoryRUN useradd -m -u 1000 userUSER userENV PATH="/home/user/.local/bin:$PATH"WORKDIR /app# Copy requirements and installCOPY --chown=user ./requirements.txt requirements.txtRUN pip install --no-cache-dir --upgrade -r requirements.txt# Copy all environment filesCOPY --chown=user . /app# The hackathon expects the OpenEnv Server to run on 7860 for Spaces Gradio endpoints# We will use uvicorn to host the app which complies with the specCMD ["uvicorn", "dashboard_server:app", "--host", "0.0.0.0", "--port", "7860"]1234567891011121314151617181920212223FROM python:3.10-slim# Create non-root userRUN useradd -m -u 1000 userWORKDIR /app# Install dependencies as root so pip cache is sharedCOPY ./requirements.txt requirements.txtRUN pip install --no-cache-dir --upgrade -r requirements.txt# Copy project files and set ownershipCOPY . /appRUN chmod +x /app/start.sh && chown -R user:user /appUSER userENV PATH="/home/user/.local/bin:$PATH"# start.sh reads BACKEND_URL and SERVE_DASHBOARD_ONLY env vars:# Trainer Space β SERVE_DASHBOARD_ONLY unset β full backend (dashboard_server.py)# Demo Space β SERVE_DASHBOARD_ONLY=1 β static dashboard only (serve_demo.py)# BACKEND_URL=https://ajay00747-cybersoc-trainer.hf.spaceCMD ["/bin/sh", "/app/start.sh"]
289 unmodified lines290291292293294295289 unmodified lines</main><!-- /main-grid --><!-- Scripts (order matters) --><script src="js/animations.js"></script><script src="js/api.js"></script><script src="js/graphs.js"></script>289 unmodified lines290291292293294295296289 unmodified lines</main><!-- /main-grid --><!-- Scripts (order matters) --><script src="js/config.js"></script><script src="js/animations.js"></script><script src="js/api.js"></script><script src="js/graphs.js"></script>
48 unmodified lines49505152535455566 unmodified lines63646566676848 unmodified lines}})();// ββ WebSocket URL ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββfunction _wsUrl() {if (typeof window === 'undefined') {return `ws://localhost:8000/ws/${_sessionId}`;}6 unmodified lines// HTTP base URL β used only by checkConnection() which pings /health over HTTPfunction _httpBase() {if (typeof window === 'undefined') return 'http://localhost:8000';const { protocol, hostname, port } = window.location;if (protocol === 'file:') return 'http://localhost:8000';48 unmodified lines495051525354555657585960616263646566676869706 unmodified lines7778798081828348 unmodified lines}})();// ββ External backend override (set by dashboard/js/config.js in Demo mode) β// config.js sets window.CYBERSOC_BACKEND_URL to the trainer Space URL.// Empty string β auto-detect from page origin (default for full-stack mode).const _backendOverride = (typeof window !== 'undefined' &&typeof window.CYBERSOC_BACKEND_URL === 'string' &&window.CYBERSOC_BACKEND_URL.trim()) ? window.CYBERSOC_BACKEND_URL.trim().replace(/\/$/, '') : '';// ββ WebSocket URL ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββfunction _wsUrl() {if (_backendOverride) {const wsProto = _backendOverride.startsWith('https') ? 'wss:' : 'ws:';const host = _backendOverride.replace(/^https?:\/\//, '');return `${wsProto}//${host}/ws/${_sessionId}`;}if (typeof window === 'undefined') {return `ws://localhost:8000/ws/${_sessionId}`;}6 unmodified lines// HTTP base URL β used only by checkConnection() which pings /health over HTTPfunction _httpBase() {if (_backendOverride) return _backendOverride;if (typeof window === 'undefined') return 'http://localhost:8000';const { protocol, hostname, port } = window.location;if (protocol === 'file:') return 'http://localhost:8000';
123456789101112// CyberSOC Backend URL Configuration// -------------------------------------------------------------// Leave as empty string when the dashboard and backend are on// the same origin (default for the CyberSOC-upgraded space).//// Set to the trainer Space URL for standalone Dashboard mode:// window.CYBERSOC_BACKEND_URL = 'https://ajay00747-cybersoc-trainer.hf.space';//// In production this file is overwritten at container startup by// start.sh using the BACKEND_URL environment variable.// -------------------------------------------------------------window.CYBERSOC_BACKEND_URL = '';12345678910111213141516171819202122232425262728293031323334353637383940414243"""Minimal static file server for the CyberSOC Demo HF Space.
Serves the dashboard/ directory on port 7860.The BACKEND_URL environment variable is injected intodashboard/js/config.js at startup by start.sh before thisprocess is launched."""
import osimport uvicornfrom fastapi import FastAPIfrom fastapi.responses import RedirectResponsefrom fastapi.staticfiles import StaticFilesfrom fastapi.middleware.cors import CORSMiddleware
app = FastAPI(title="CyberSOC Demo Dashboard")
app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"],)
@app.get("/")def root(): return RedirectResponse(url="/index.html")
@app.get("/health")def health(): return {"status": "ok", "service": "cybersoc-demo"}
# Serve everything in the dashboard/ folder_dashboard_dir = os.path.join(os.path.dirname(__file__), "dashboard")app.mount("/", StaticFiles(directory=_dashboard_dir, html=True), name="static")
if __name__ == "__main__": uvicorn.run(app, host="0.0.0.0", port=7860)12345678910111213141516171819202122232425262728293031323334353637#!/bin/sh# CyberSOC container startup script## Environment variables (set in HF Space settings):# BACKEND_URL β URL of the CyberSOC trainer Space, e.g.# https://ajay00747-cybersoc-trainer.hf.space# Leave unset for same-origin (full-stack mode).# SERVE_DASHBOARD_ONLY β Set to "1" for the Demo space (static files only).# Leave unset or "0" for the Trainer space (full API).## Demo Space settings:# BACKEND_URL = https://ajay00747-cybersoc-trainer.hf.space# SERVE_DASHBOARD_ONLY = 1## Trainer Space settings:# (no extra env vars required)
set -e
# ββ Inject backend URL into config.js ββββββββββββββββββββββββββββββββββββββββCONFIG_JS="/app/dashboard/js/config.js"if [ -n "${BACKEND_URL}" ]; then printf "window.CYBERSOC_BACKEND_URL = '%s';\n" "${BACKEND_URL}" > "${CONFIG_JS}" echo "[startup] Demo mode β backend URL: ${BACKEND_URL}"else printf "window.CYBERSOC_BACKEND_URL = '';\n" > "${CONFIG_JS}" echo "[startup] Full-stack mode β backend on same origin"fi
# ββ Launch the correct server βββββββββββββββββββββββββββββββββββββββββββββββββif [ "${SERVE_DASHBOARD_ONLY:-0}" = "1" ]; then echo "[startup] Serving dashboard only (serve_demo.py) on port 7860" exec python /app/serve_demo.pyelse echo "[startup] Serving full stack (dashboard_server.py) on port 7860" exec uvicorn dashboard_server:app --host 0.0.0.0 --port 7860fi