Skip to content

Commit

Permalink
Api doc
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTimeey committed Jun 4, 2021
1 parent 105ca4e commit 5c92f0e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
32 changes: 32 additions & 0 deletions src/routers/api-doc.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 5c92f0e

Please sign in to comment.