由于某种原因,我的 swagger 配置没有获取我的任何路线。
我可以访问 swagger UI,但当我导航到
http://127.0.0.1:8181/documentation时,只会得到
No operations defined in spec!
。我确信我已经在某个时候设置了此设置,也许版本已更改。我尝试过更改启动顺序,但无济于事。
有人可以帮忙吗?
谢谢
我的server.js
const { logger } = require('./log.js')
const cors = require('@fastify/cors')
const swagger = require('@fastify/swagger')
const swaggerUi = require('@fastify/swagger-ui')
const fastify = require('fastify')({ logger: true })
const port = 8181
const initializeFastify = () => {
fastify.register(cors, {
origin: true,
methods: ['GET', 'POST', 'OPTIONS']
})
fastify.register(swagger, {
swagger: {
info: {
title: 'OpenAPI spec for the ICAT Upload Service',
description: 'The Upload service allows a DataGateway user to upload files to the ICAT archive by adding datafiles to an existing dataset. ' +
'It provides an endpoint to create a dataset',
version: 'beta',
consumes: ['application/json'],
produces: ['application/json']
}
}
})
fastify.register(swaggerUi, {
routePrefix: '/documentation'
})
fastify.addContentTypeParser('application/offset+octet-stream', (request, payload, done) => done(null))
addHealthCheckEndpoint()
fastify.listen({ port, host: '0.0.0.0' }, onServerStart)
}
const addHealthCheckEndpoint = () => {
fastify.route({
method: 'get',
url: '/',
schema: {
description: 'The following endpoint has been created to detect a running service',
tags: ['Hello World'],
response: {
201: {
description: 'Successful response',
type: 'object'
}
}
},
handler: async (request, reply) => {
reply.statusCode = 200
reply.send('Hello World')
}
})
}
const onServerStart = (err) => {
logger.info(`--> Starting Upload server, listening on port: ${port}`)
if (err) {
fastify.log.error(err)
process.exit(1)
}
}
module.exports = fastify.server
initializeFastify()
需要登记的路线:
fastify.register(async function (fastify) {
addHealthCheckEndpoint()
})