diff --git a/backend/server.js b/backend/server.js index 6d100ca..ee39ef2 100644 --- a/backend/server.js +++ b/backend/server.js @@ -12,6 +12,7 @@ logger.info('Server starting up', { timestamp: new Date().toISOString() }); +const fs = require('fs'); const express = require('express'); const helmet = require('helmet'); const cors = require('cors'); @@ -219,6 +220,22 @@ app.use('/api/public', require('./src/routes/publicCMS')); app.use('/api/images', require('./src/routes/protectedImages')); app.use('/api/secure-images', secureImagesRoutes); +// Optional: Serve built frontend (native installs) +try { + const serveFrontend = process.env.SERVE_FRONTEND === 'true'; + const frontendDir = process.env.FRONTEND_DIR || path.join(__dirname, '../frontend/dist'); + if (serveFrontend && fs.existsSync(frontendDir)) { + logger.info(`Serving frontend from ${frontendDir}`); + app.use(express.static(frontendDir)); + // SPA fallback for non-API routes + app.get([ '/', '/admin', '/admin/*', '/gallery/*' ], (req, res) => { + res.sendFile(path.join(frontendDir, 'index.html')); + }); + } +} catch (e) { + logger.warn('Failed to enable frontend static serving', { error: e.message }); +} + // Error handling middleware app.use((err, req, res, next) => { console.error('EXPRESS ERROR HANDLER:', err); diff --git a/scripts/setup.sh b/scripts/setup.sh index 56466b7..7b93fa4 100755 --- a/scripts/setup.sh +++ b/scripts/setup.sh @@ -602,9 +602,19 @@ setup_native_installation() { # Install backend production dependencies cd "$NATIVE_APP_DIR/app/backend" npm install --production - # Ensure SQLite data directory exists for native installs mkdir -p "$NATIVE_APP_DIR/app/backend/data" + + # Build frontend for native serving + log_step "Building frontend..." + if [[ -d "$NATIVE_APP_DIR/app/frontend" ]]; then + cd "$NATIVE_APP_DIR/app/frontend" + # Try ci (faster/clean) then fallback to install + run_as_user "npm ci --include=dev" || run_as_user "npm install" + run_as_user "npm run build" + else + log_warn "Frontend directory not found; admin UI will not be served by backend" + fi # Generate secrets local jwt_secret=$(generate_jwt_secret) @@ -654,6 +664,10 @@ DEFAULT_EXPIRY_DAYS=30 # Logging LOG_DIR=$NATIVE_APP_DIR/logs LOG_LEVEL=info + +# Frontend serving (native installs) +SERVE_FRONTEND=true +FRONTEND_DIR=$NATIVE_APP_DIR/app/frontend/dist EOF # Set permissions