discovery.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 createApp(projectRoot, discoveryClient) {
  32. const app = express()
  33. app.use(cors())
  34. app.use(bodyParser.json())
  35. // FIXME app.use(bodyParser.urlencoded({ extended: true }));
  36. // Load & extend/configure API docs
  37. const api = yaml.safeLoad(fs.readFileSync(path.resolve(projectRoot, 'api-base.yml')))
  38. api['x-express-openapi-additional-middleware'] = [validateResponses]
  39. api['x-express-openapi-validation-strict'] = true
  40. openapi.initialize({
  41. apiDoc: api,
  42. app,
  43. // paths: path.resolve(projectRoot, 'discovery_app_paths'),
  44. paths: {
  45. path: '/discover/v0/{id}',
  46. module: require('../paths/discover/v0/{id}'),
  47. },
  48. docsPath: '/swagger.json',
  49. dependencies: {
  50. discoveryClient,
  51. },
  52. })
  53. // If no other handler gets triggered (errors), respond with the
  54. // error serialized to JSON.
  55. app.use(function (err, req, res) {
  56. res.status(err.status).json(err)
  57. })
  58. return app
  59. }
  60. module.exports = createApp