99 lines
3.3 KiB
JavaScript
99 lines
3.3 KiB
JavaScript
import 'dotenv/config';
|
|
import express from 'express';
|
|
import cors from 'cors';
|
|
import path from 'path';
|
|
import fs from 'fs';
|
|
import { fileURLToPath } from 'url';
|
|
import { initDb } from './db.js';
|
|
import authRoutes from './routes/auth.js';
|
|
import contentRoutes from './routes/content.js';
|
|
import uploadRoutes from './routes/upload.js';
|
|
import multer from 'multer';
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
const PORT = process.env.PORT || 3001;
|
|
const UPLOAD_DIR = process.env.UPLOAD_DIR || path.join(__dirname, 'uploads');
|
|
// Public URL prefix on Apache (e.g. /citpl_website). Leave empty if site is at domain root.
|
|
const SITE_BASE = (process.env.SITE_BASE || '/citpl_website').replace(/\/$/, '');
|
|
const distDir = path.join(__dirname, '..', 'dist');
|
|
const publicAssetsDir = path.join(__dirname, '..', 'public', 'assets');
|
|
const serveStatic =
|
|
process.env.SERVE_STATIC === '1' ||
|
|
process.env.SERVE_STATIC === 'true' ||
|
|
(process.env.SERVE_STATIC !== '0' && fs.existsSync(distDir));
|
|
|
|
initDb();
|
|
|
|
const app = express();
|
|
|
|
app.use(cors({
|
|
origin: process.env.CORS_ORIGIN || true,
|
|
credentials: true,
|
|
}));
|
|
|
|
app.use(express.json({ limit: '10mb' }));
|
|
|
|
function mountApp(base) {
|
|
const prefix = base || '';
|
|
|
|
app.use(`${prefix}/uploads`, express.static(UPLOAD_DIR));
|
|
|
|
app.get(`${prefix}/api/health`, (_req, res) => {
|
|
res.json({ status: 'ok', timestamp: new Date().toISOString() });
|
|
});
|
|
|
|
app.use(`${prefix}/api/auth`, authRoutes);
|
|
app.use(`${prefix}/api/content`, contentRoutes);
|
|
app.use(`${prefix}/api/upload`, uploadRoutes);
|
|
|
|
if (serveStatic) {
|
|
app.use(prefix || '/', express.static(distDir));
|
|
// SPA fallback only for the app's base path, not root-level unknown files
|
|
app.get(`${prefix}/*`, (req, res, next) => {
|
|
const ext = req.path.split('.').pop();
|
|
if (ext && ext !== req.path) return next();
|
|
res.sendFile(path.join(distDir, 'index.html'));
|
|
});
|
|
}
|
|
}
|
|
|
|
// Root paths — used when Apache proxies /citpl_website/api → http://127.0.0.1:3001/api
|
|
mountApp('');
|
|
// Prefixed paths — used when Apache proxies /citpl_website → http://127.0.0.1:3001/citpl_website
|
|
if (SITE_BASE) {
|
|
mountApp(SITE_BASE);
|
|
}
|
|
|
|
// Fallback: serve public/assets directly at /assets/ and /citpl_website/assets/
|
|
// This ensures cert/logo images load regardless of which base the build used
|
|
app.use('/assets', express.static(publicAssetsDir));
|
|
if (SITE_BASE) {
|
|
app.use(`${SITE_BASE}/assets`, express.static(publicAssetsDir));
|
|
}
|
|
|
|
// Return proper 404 for missing static assets (prevents index.html served as JS/CSS)
|
|
app.use((req, res, next) => {
|
|
const ext = req.path.split('.').pop();
|
|
if (ext && ext !== req.path) {
|
|
return res.status(404).json({ error: 'Not found' });
|
|
}
|
|
next();
|
|
});
|
|
|
|
app.use((err, _req, res, _next) => {
|
|
if (err instanceof multer.MulterError && err.code === 'LIMIT_FILE_SIZE') {
|
|
return res.status(413).json({ error: 'File too large (max 50MB)', code: 'FILE_TOO_LARGE' });
|
|
}
|
|
console.error(err);
|
|
res.status(500).json({ error: err.message || 'Internal server error', code: 'SERVER_ERROR' });
|
|
});
|
|
|
|
app.listen(PORT, () => {
|
|
console.log(`CITPL Node server on http://localhost:${PORT}`);
|
|
console.log(` API: /api/* and ${SITE_BASE || ''}/api/*`);
|
|
console.log(` Auth: ADMIN_USERNAME from .env (${process.env.ADMIN_USERNAME || 'admin'})`);
|
|
if (serveStatic) {
|
|
console.log(` Static: ${distDir}`);
|
|
}
|
|
});
|