Embed Swagger UI
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -15,3 +15,5 @@ default-env.json
|
|||||||
packages/messageBox
|
packages/messageBox
|
||||||
reviews/msg-box
|
reviews/msg-box
|
||||||
reviews/db/test.db
|
reviews/db/test.db
|
||||||
|
|
||||||
|
*.openapi3.json
|
||||||
|
|||||||
@@ -10,7 +10,8 @@
|
|||||||
"scripts": {
|
"scripts": {
|
||||||
"genres": "cds serve test/genres.cds",
|
"genres": "cds serve test/genres.cds",
|
||||||
"start": "cds run",
|
"start": "cds run",
|
||||||
"watch": "cds watch"
|
"watch": "cds watch",
|
||||||
|
"to-openapi": "cds compile srv --to openapi -s CatalogService -o srv"
|
||||||
},
|
},
|
||||||
"cds": {
|
"cds": {
|
||||||
"requires": {
|
"requires": {
|
||||||
|
|||||||
48
bookshop/server.js
Normal file
48
bookshop/server.js
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
const cds = require ('@sap/cds')
|
||||||
|
const {resolve} = require ('path')
|
||||||
|
const {promisify} = require('util')
|
||||||
|
const readFile = promisify(require('fs').readFile)
|
||||||
|
const swaggerUi = require ('swagger-ui-express')
|
||||||
|
const DEBUG = cds.debug('openapi')
|
||||||
|
|
||||||
|
let app, host, docCache={}
|
||||||
|
|
||||||
|
cds
|
||||||
|
.on ('bootstrap', _app => { app = _app })
|
||||||
|
.on ('serving', service => {
|
||||||
|
const apiPath = '/api-docs'+service.path
|
||||||
|
console.log (`[Open API] - serving ${service.name} at ${apiPath}`)
|
||||||
|
app.use(apiPath, async (req, _, next) => {
|
||||||
|
req.swaggerDoc = await toOpenApiDoc(service, host, docCache)
|
||||||
|
next()
|
||||||
|
}, swaggerUi.serve, swaggerUi.setup())
|
||||||
|
})
|
||||||
|
.on ('listening', ({server})=> { host = 'localhost:'+server.address().port })
|
||||||
|
|
||||||
|
async function toOpenApiDoc(service, host, cache) {
|
||||||
|
if (!cache[service.name]) {
|
||||||
|
const spec = await openApiFromFile(service)
|
||||||
|
if (spec) {
|
||||||
|
cache[service.name] = spec
|
||||||
|
}
|
||||||
|
else if (cds.compile.to.openapi) { // on-the-fly exporter available?
|
||||||
|
DEBUG && DEBUG ('Compiling Open API spec for', service.name)
|
||||||
|
cache[service.name] = cds.compile.to.openapi (service.model, {
|
||||||
|
service: service.name,
|
||||||
|
scheme: 'http', host, basePath: service.path
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return cache[service.name]
|
||||||
|
}
|
||||||
|
|
||||||
|
async function openApiFromFile(service) {
|
||||||
|
const fileName = resolve(`srv/${service.name}.openapi3.json`)
|
||||||
|
const file = await readFile(fileName).catch(()=>{/*no such file*/})
|
||||||
|
if (file) {
|
||||||
|
DEBUG && DEBUG ('Using Open API spec from file', fileName)
|
||||||
|
return JSON.parse(file)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = cds.server
|
||||||
@@ -18,7 +18,8 @@
|
|||||||
"chai": "^4.2.0",
|
"chai": "^4.2.0",
|
||||||
"chai-as-promised": "^7.1.1",
|
"chai-as-promised": "^7.1.1",
|
||||||
"chai-subset": "^1.6.0",
|
"chai-subset": "^1.6.0",
|
||||||
"sqlite3": "^5"
|
"sqlite3": "^5",
|
||||||
|
"swagger-ui-express": "^4.1.4"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"bookshop": "cds watch bookshop",
|
"bookshop": "cds watch bookshop",
|
||||||
|
|||||||
Reference in New Issue
Block a user