Skip to content

Commit b1642b8

Browse files
committed
Swagger fix
1 parent fd611ed commit b1642b8

1 file changed

Lines changed: 56 additions & 2 deletions

File tree

Backend/src/app.js

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,14 @@ app.use(cors({
3535
// Configure helmet but allow Swagger UI resources
3636
app.use(
3737
helmet({
38-
contentSecurityPolicy: false,
38+
contentSecurityPolicy: {
39+
directives: {
40+
defaultSrc: ["'self'"],
41+
scriptSrc: ["'self'", "'unsafe-inline'", "'unsafe-eval'"],
42+
styleSrc: ["'self'", "'unsafe-inline'"],
43+
imgSrc: ["'self'", "data:"]
44+
}
45+
}
3946
})
4047
);
4148

@@ -98,17 +105,52 @@ const swaggerOptions = {
98105

99106
const swaggerSpec = swaggerJsdoc(swaggerOptions);
100107

108+
// Set explicit MIME types for swagger files
109+
app.use((req, res, next) => {
110+
if (req.url.endsWith('.js')) {
111+
res.setHeader('Content-Type', 'application/javascript');
112+
} else if (req.url.endsWith('.css')) {
113+
res.setHeader('Content-Type', 'text/css');
114+
}
115+
next();
116+
});
117+
101118
// Root route - redirect to Swagger docs
102119
app.get('/', (req, res) => {
103120
res.redirect('/api-docs');
104121
});
105122

106123
// Swagger UI route
107-
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec, {
124+
app.use('/api-docs', (req, res, next) => {
125+
// Add logging for swagger requests
126+
console.log(`[LOG swagger_request] ========= Serving Swagger UI: ${req.url}`);
127+
next();
128+
}, swaggerUi.serve, swaggerUi.setup(swaggerSpec, {
108129
explorer: true,
109130
customCss: '.swagger-ui .topbar { display: none }'
110131
}));
111132

133+
// Fallback route for Swagger docs in case the UI fails to load
134+
app.get('/api-docs-simple', (req, res) => {
135+
res.send(`
136+
<html>
137+
<head>
138+
<title>UniHub API Documentation</title>
139+
<style>
140+
body { font-family: Arial, sans-serif; margin: 20px; }
141+
pre { background: #f4f4f4; padding: 10px; border-radius: 5px; }
142+
</style>
143+
</head>
144+
<body>
145+
<h1>UniHub API Documentation</h1>
146+
<p>Simple fallback documentation page. If you're seeing this, there might be issues with the Swagger UI.</p>
147+
<h2>API Specification:</h2>
148+
<pre>${JSON.stringify(swaggerSpec, null, 2)}</pre>
149+
</body>
150+
</html>
151+
`);
152+
});
153+
112154
// API routes
113155
app.use('/api/status', statusRoutes);
114156
app.use('/api/auth', authRoutes);
@@ -122,6 +164,18 @@ app.use('/api/statistics', statisticsRoutes);
122164
app.use('/api/materials', materialRoutes);
123165
app.use('/api/rag', ragRoutes);
124166

167+
// Specific error handler for API docs
168+
app.use('/api-docs', (err, req, res, next) => {
169+
console.error('[LOG swagger_error] ========= Error serving Swagger UI:', err);
170+
console.error('URL:', req.url);
171+
console.error('Stack:', err.stack);
172+
173+
// Attempt to continue processing
174+
if (!res.headersSent) {
175+
next(err);
176+
}
177+
});
178+
125179
// Error handling middleware
126180
app.use((err, req, res, next) => {
127181
console.error('Error:', err);

0 commit comments

Comments
 (0)