discovery.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * This file is part of the storage node for the Joystream project.
  3. * Copyright (C) 2019 Joystream Contributors
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. 'use strict';
  19. // npm requires
  20. const express = require('express');
  21. const openapi = require('express-openapi');
  22. const bodyParser = require('body-parser');
  23. const cors = require('cors');
  24. const yaml = require('js-yaml');
  25. // Node requires
  26. const fs = require('fs');
  27. const path = require('path');
  28. // Project requires
  29. const validateResponses = require('./middleware/validate_responses');
  30. // Configure app
  31. function create_app(project_root, runtime)
  32. {
  33. const app = express();
  34. app.use(cors());
  35. app.use(bodyParser.json());
  36. // FIXME app.use(bodyParser.urlencoded({ extended: true }));
  37. // Load & extend/configure API docs
  38. var api = yaml.safeLoad(fs.readFileSync(
  39. path.resolve(project_root, 'api-base.yml')));
  40. api['x-express-openapi-additional-middleware'] = [validateResponses];
  41. api['x-express-openapi-validation-strict'] = true;
  42. openapi.initialize({
  43. apiDoc: api,
  44. app: app,
  45. //paths: path.resolve(project_root, 'discovery_app_paths'),
  46. paths: {
  47. path: '/discover/v0/{id}',
  48. module: require('../paths/discover/v0/{id}')
  49. },
  50. docsPath: '/swagger.json',
  51. dependencies: {
  52. runtime: runtime,
  53. },
  54. });
  55. // If no other handler gets triggered (errors), respond with the
  56. // error serialized to JSON.
  57. app.use(function(err, req, res, next) {
  58. res.status(err.status).json(err);
  59. });
  60. return app;
  61. }
  62. module.exports = create_app;