schema.js 900 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. const express = require('express');
  2. const schema = require('../schema');
  3. const PACKAGE = require('../package.json');
  4. const router = express.Router({
  5. caseSensitive: true,
  6. strict: true,
  7. mergeParams: true
  8. });
  9. router
  10. .route('/')
  11. .options((_, res) => {
  12. res.sendStatus(204);
  13. })
  14. /**
  15. * GET /schema
  16. */
  17. .get(async (req, res) => {
  18. let swaggerJSON = await schema.getCompiledSchema();
  19. let proto = req.protocol;
  20. if (typeof req.headers['x-forwarded-proto'] !== 'undefined' && req.headers['x-forwarded-proto']) {
  21. proto = req.headers['x-forwarded-proto'];
  22. }
  23. let origin = proto + '://' + req.hostname;
  24. if (typeof req.headers.origin !== 'undefined' && req.headers.origin) {
  25. origin = req.headers.origin;
  26. }
  27. swaggerJSON.info.version = PACKAGE.version;
  28. swaggerJSON.servers[0].url = origin + '/api';
  29. res.status(200).send(swaggerJSON);
  30. });
  31. module.exports = router;