Skip to content

Commit f8a701e

Browse files
added swager
1 parent 4ea2a70 commit f8a701e

3 files changed

Lines changed: 595 additions & 1 deletion

File tree

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,20 @@
4141
"mongoose": "^8.8.0",
4242
"nodemailer": "^7.0.12",
4343
"sanitize-html": "^2.17.0",
44+
"swagger-jsdoc": "^6.2.8",
45+
"swagger-ui-express": "^5.0.1",
4446
"zod": "^4.2.1"
4547
},
4648
"devDependencies": {
4749
"@types/body-parser": "^1.19.5",
48-
"@types/sanitize-html": "^2.16.0",
4950
"@types/cookie-parser": "^1.4.7",
5051
"@types/cors": "^2.8.17",
5152
"@types/dotenv-safe": "^8.1.6",
5253
"@types/jsonwebtoken": "^9.0.6",
5354
"@types/nodemailer": "^6.4.14",
55+
"@types/sanitize-html": "^2.16.0",
56+
"@types/swagger-jsdoc": "^6.0.4",
57+
"@types/swagger-ui-express": "^4.1.8",
5458
"@typescript-eslint/eslint-plugin": "^7.1.0",
5559
"@typescript-eslint/parser": "^7.1.0",
5660
"eslint": "^8.57.0",

src/app.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import express, { ErrorRequestHandler } from 'express';
66
import http from 'http';
77
import cors from 'cors';
88
import rateLimit from 'express-rate-limit';
9+
import swaggerUi from 'swagger-ui-express';
910
import api from './api';
1011
import { migrate } from './migrations';
1112
import { AppError } from '@/utils';
13+
import { swaggerSpec } from './services/swagger';
1214

1315
// Validate required environment variables
1416
const requiredEnvVars = ['JWT_SECRET', 'MONGODB_URI'];
@@ -64,6 +66,22 @@ app.get('/health', (req, res) => {
6466
});
6567
});
6668

69+
// Swagger documentation
70+
app.use(
71+
'/docs',
72+
swaggerUi.serve,
73+
swaggerUi.setup(swaggerSpec, {
74+
customCss: '.swagger-ui .topbar { display: none }',
75+
customSiteTitle: 'Zoomment API Docs'
76+
})
77+
);
78+
79+
// Swagger JSON endpoint
80+
app.get('/docs.json', (req, res) => {
81+
res.setHeader('Content-Type', 'application/json');
82+
res.send(swaggerSpec);
83+
});
84+
6785
app.use('/api', api);
6886

6987
// Error handler
@@ -87,6 +105,7 @@ const server = http.createServer(app);
87105
setImmediate(() => {
88106
server.listen(process.env.PORT, () => {
89107
console.log('Express server listening on http://localhost:%s/', process.env.PORT);
108+
console.log('API Docs available at http://localhost:%s/docs', process.env.PORT);
90109
});
91110
});
92111

0 commit comments

Comments
 (0)