From 5c92f0efac4e5948c69462efae1bd44b8852dc3a Mon Sep 17 00:00:00 2001 From: Tim Siegler Date: Sat, 5 Jun 2021 01:51:13 +0200 Subject: [PATCH] Api doc --- README.md | 9 +++++++++ app.js | 4 +++- package.json | 1 - src/routers/api-doc.js | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 src/routers/api-doc.js diff --git a/README.md b/README.md index 6f85dd6..52b10f2 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,15 @@ An API for retrieving and saving words to build a 'word of day' application. It's also a learning project for various technologies like nodejs, express and mongoose deployment. +## Documentation +The API is documented with swagger/openapi. The resources will automatically mount on startup. + +* Integrated swagger UI: `http://localhost:8000/`. +* Openapi.yaml: `http://localhost:8000/doc/openapi.yaml` +* Swagger.yaml: `http://localhost:8000/doc/swagger.yaml` +* Openapi.json: `http://localhost:8000/doc/openapi.json` +* Swagger.json: `http://localhost:8000/doc/swagger.json` + ## Application env file Create an .env file in `src/config` for defining the application properties diff --git a/app.js b/app.js index 44177a6..1c08344 100644 --- a/app.js +++ b/app.js @@ -29,8 +29,10 @@ router.use('/batch', require('./src/routers/batch.js')); router.use('/random', require('./src/routers/random.js')); router.use('/search', require('./src/routers/search')); router.use('/health', require('./src/routers/healthcheck')); - +router.use('/tim', require('./src/routers/api-doc')); app.use('/api', router); + +app.use('/doc', require('./src/routers/api-doc')); app.use('/', swaggerUi.serve, swaggerUi.setup(swaggerDocument, { explorer: false })); module.exports = app; diff --git a/package.json b/package.json index e4efe0c..3e95dd4 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,6 @@ "cors": "^2.8.5", "dotenv": "^8.2.0", "express": "^4.17.1", - "marked": "^0.7.0", "moment": "^2.29.1", "moment-duration-format": "^2.3.2", "mongoose": "^5.12.0", diff --git a/src/routers/api-doc.js b/src/routers/api-doc.js new file mode 100644 index 0000000..83bf9c0 --- /dev/null +++ b/src/routers/api-doc.js @@ -0,0 +1,32 @@ +const express = require('express'); +const router = express.Router(); +const { promisify } = require('util'); +const fs = require('fs'); +const readFileAsync = promisify(fs.readFile); + +async function getFile(name) { + return readFileAsync(__dirname + '../../../api-doc/' + name); +} + +router.route('/openapi.json').get(async function(req, res) { + const [file] = await Promise.all([getFile('openapi.json')]); + res.setHeader('Content-Type', 'application/json'); + res.send(file.toString()); +}); +router.route('/openapi.yaml').get(async function(req, res) { + const [file] = await Promise.all([getFile('openapi.yaml')]); + res.setHeader('Content-Type', 'application/json'); + res.send(file.toString()); +}); +router.route('/swagger.json').get(async function(req, res) { + const [file] = await Promise.all([getFile('swagger.json')]); + res.setHeader('Content-Type', 'application/json'); + res.send(file.toString()); +}); +router.route('/swagger.yaml').get(async function(req, res) { + const [file] = await Promise.all([getFile('swagger.yaml')]); + res.setHeader('Content-Type', 'application/json'); + res.send(file.toString()); +}); + +module.exports = router;