-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswagger.js
More file actions
74 lines (69 loc) · 2.07 KB
/
Copy pathswagger.js
File metadata and controls
74 lines (69 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const swaggerJsDoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');
// Swagger configuration
const swaggerOptions = {
definition: {
openapi: "3.0.0",
info: {
title: "Simarket API",
version: "1.0.0",
description: "API documentation for Simarket application",
contact: {
name: "Ikram",
},
},
servers: [
{
url: "https://simarket-api.vercel.app",
description: "Production server",
},
{
url: "http://localhost:5000",
description: "Local server",
},
],
},
apis: ['./routes/*.js'], // Adjust the path to your route files
};
const swaggerDocs = swaggerJsDoc(swaggerOptions);
const setupSwagger = (app) => {
// Serve the Swagger UI
app.get('/api-docs', (req, res) => {
res.send(`
<!DOCTYPE html>
<html>
<head>
<title>Simarket API Docs</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.14.3/swagger-ui.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.14.3/swagger-ui-bundle.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.14.3/swagger-ui-standalone-preset.js"></script>
<link rel="icon" href="/favicon.png" type="image/png" />
<style>
.topbar { display: none; } /* Hides the Explore bar */
</style>
</head>
<body>
<div id="swagger-ui"></div>
<script>
window.onload = function() {
SwaggerUIBundle({
url: '/api-docs/swagger.json', // Points to your Swagger JSON
dom_id: '#swagger-ui',
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
layout: "BaseLayout", // Removes the Explore bar
});
};
</script>
</body>
</html>
`);
});
// Serve the Swagger JSON
app.get('/api-docs/swagger.json', (req, res) => {
res.json(swaggerDocs);
});
};
module.exports = setupSwagger;