| 1 | // This is a simple Express server to serve Swagger UI |
| 2 | import express from "express"; |
| 3 | import fs from "fs"; |
| 4 | import yaml from "js-yaml"; |
| 5 | import path from "path"; |
| 6 | import swaggerUi from "swagger-ui-express"; |
| 7 | import { fileURLToPath } from "url"; |
| 8 | |
| 9 | // ES Module handling for __dirname |
| 10 | const __filename = fileURLToPath(import.meta.url); |
| 11 | const __dirname = path.dirname(__filename); |
| 12 | |
| 13 | // Read the OpenAPI spec |
| 14 | const openApiYaml = fs.readFileSync( |
| 15 | path.join(__dirname, "openapi.yaml"), |
| 16 | "utf8", |
| 17 | ); |
| 18 | const openApiSpec = yaml.load(openApiYaml); |
| 19 | |
| 20 | const app = express(); |
| 21 | const port = process.env.PORT || 3002; |
| 22 | |
| 23 | // Serve the Swagger UI at the root URL |
| 24 | app.use( |
| 25 | "/", |
| 26 | swaggerUi.serve, |
| 27 | swaggerUi.setup(openApiSpec, { |
| 28 | explorer: true, |
| 29 | customCss: ".swagger-ui .topbar { display: none }", |
| 30 | swaggerOptions: { |
| 31 | docExpansion: "list", // or 'full' or 'none' |
| 32 | filter: true, |
| 33 | showExtensions: true, |
| 34 | showCommonExtensions: true, |
| 35 | }, |
| 36 | }), |
| 37 | ); |
| 38 | |
| 39 | // Start the server |
| 40 | app.listen(port, () => { |
| 41 | console.log(`Swagger UI is running at http://localhost:${port}`); |
| 42 | console.log(`Press Ctrl+C to stop the server`); |
| 43 | }); |