This commit is contained in:
valenti 2026-04-09 06:06:10 -04:00
parent 514cc9c05c
commit b64d489a2a
2 changed files with 67 additions and 6 deletions

View File

@ -37,6 +37,16 @@ if [ ! -f "package.json" ]; then
exit 1 exit 1
fi fi
if [ -f "../package.json" ]; then
print_info "Build frontend..."
cd ..
npm ci
npm run build
cd server
else
print_warning "package.json frontend non trovato, salto build frontend"
fi
# Verifica che esista tsconfig.json # Verifica che esista tsconfig.json
if [ ! -f "tsconfig.json" ]; then if [ ! -f "tsconfig.json" ]; then
print_error "File tsconfig.json non trovato!" print_error "File tsconfig.json non trovato!"
@ -79,6 +89,15 @@ if [ -d "static" ]; then
print_info "File statici copiati in dist/static" print_info "File statici copiati in dist/static"
fi fi
if [ -d "../dist" ] && [ -f "../dist/index.html" ]; then
rm -rf dist/frontend
mkdir -p dist/frontend
cp -r ../dist/* dist/frontend/
print_info "Frontend copiato in dist/frontend"
else
print_warning "Build frontend non trovato in ../dist, salto copia"
fi
# Crea file package.json per produzione (solo dipendenze runtime) # Crea file package.json per produzione (solo dipendenze runtime)
print_info "Creazione package.json per produzione..." print_info "Creazione package.json per produzione..."
node -e " node -e "

View File

@ -150,13 +150,51 @@ const swaggerOptions = {
const swaggerSpec = swaggerJsdoc(swaggerOptions); const swaggerSpec = swaggerJsdoc(swaggerOptions);
ELKService.info('Swagger specification generated'); ELKService.info('Swagger specification generated');
const resolveDirWithFile = (candidates: string[], requiredFile: string): string | null => {
for (const candidate of candidates) {
try {
const filePath = path.join(candidate, requiredFile);
if (fs.existsSync(filePath)) return candidate;
} catch {
// ignore
}
}
return null;
};
const frontendDistDir = resolveDirWithFile(
[
path.resolve(__dirname, './frontend'),
path.resolve(__dirname, '../../dist'),
path.resolve(process.cwd(), 'dist'),
path.resolve(process.cwd(), '../dist'),
],
'index.html'
);
const staticDir = resolveDirWithFile(
[
path.resolve(__dirname, './static'),
path.resolve(__dirname, '../static'),
],
''
);
// Serve static files for PDF reports // Serve static files for PDF reports
app.use('/static', express.static(path.join(__dirname, '../static'))); if (staticDir) {
ELKService.info('Static files middleware enabled for /static'); app.use('/static', express.static(staticDir));
ELKService.info(`Static files middleware enabled for /static (${staticDir})`);
} else {
ELKService.warn('Static files directory not found; /static will not be served');
}
// Serve frontend build files // Serve frontend build files
app.use(express.static(path.join(__dirname, '../../dist'))); if (frontendDistDir) {
ELKService.info('Frontend static files middleware enabled for /dist'); app.use(express.static(frontendDistDir));
ELKService.info(`Frontend static files middleware enabled (${frontendDistDir})`);
} else {
ELKService.warn('Frontend dist directory not found; frontend will not be served');
}
// Routes // Routes
app.use('/api', routes); app.use('/api', routes);
@ -190,7 +228,11 @@ app.use('/api', (req, res) => {
// Catch-all handler for React Router (must be after API routes) // Catch-all handler for React Router (must be after API routes)
app.get('*', (req, res) => { app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, '../../dist/index.html')); if (!frontendDistDir) {
res.status(404).send('Frontend not available');
return;
}
res.sendFile(path.join(frontendDistDir, 'index.html'));
}); });
ELKService.info('Catch-all route registered for React Router'); ELKService.info('Catch-all route registered for React Router');