107 lines
2.2 KiB
JavaScript
107 lines
2.2 KiB
JavaScript
|
|
|
|
import Fastify from 'fastify'
|
|
import fastifySwagger from '@fastify/swagger'
|
|
import fastifySwaggerUi from '@fastify/swagger-ui'
|
|
import { readFileSync } from 'node:fs'
|
|
import { fileURLToPath } from 'url'
|
|
import { dirname, join } from 'node:path'
|
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
|
|
|
|
const swaggerDarkCss = readFileSync(join(__dirname, './css/SwaggerDark.css'), { encoding: 'utf-8' })
|
|
|
|
|
|
async function fastifySetup(configs) {
|
|
|
|
const fastify = Fastify({
|
|
logger: true
|
|
})
|
|
|
|
|
|
await fastify.register(fastifySwagger)
|
|
await fastify.register(fastifySwaggerUi, {
|
|
theme: {
|
|
title: '@futureporn/scout',
|
|
css: [
|
|
{ filename: 'SwaggerDark.css', content: swaggerDarkCss }
|
|
]
|
|
},
|
|
routePrefix: '/',
|
|
uiConfig: {
|
|
docExpansion: 'full',
|
|
deepLinking: false
|
|
},
|
|
uiHooks: {
|
|
onRequest: function (request, reply, next) { next() },
|
|
preHandler: function (request, reply, next) { next() }
|
|
},
|
|
staticCSP: true,
|
|
transformStaticCSP: (header) => header,
|
|
transformSpecification: (swaggerObject, request, reply) => { return swaggerObject },
|
|
transformSpecificationClone: true
|
|
})
|
|
|
|
|
|
fastify.put('/some-route/:id', {
|
|
schema: {
|
|
description: 'post some data',
|
|
tags: ['vtuber'],
|
|
summary: 'qwerty',
|
|
params: {
|
|
type: 'object',
|
|
properties: {
|
|
id: {
|
|
type: 'string',
|
|
description: 'user id'
|
|
}
|
|
}
|
|
},
|
|
body: {
|
|
type: 'object',
|
|
properties: {
|
|
hello: { type: 'string' },
|
|
obj: {
|
|
type: 'object',
|
|
properties: {
|
|
some: { type: 'string' }
|
|
}
|
|
}
|
|
}
|
|
},
|
|
response: {
|
|
201: {
|
|
description: 'Successful response',
|
|
type: 'object',
|
|
properties: {
|
|
hello: { type: 'string' }
|
|
}
|
|
},
|
|
default: {
|
|
description: 'Default response',
|
|
type: 'object',
|
|
properties: {
|
|
foo: { type: 'string' }
|
|
}
|
|
}
|
|
},
|
|
}
|
|
}, (req, reply) => {
|
|
reply.send('HAHAHAHAHAH')
|
|
})
|
|
|
|
fastify.listen({ port: configs.port }, function (err, address) {
|
|
console.log(`@futureporn/scout listening on ${address}`)
|
|
if (err) {
|
|
fastify.log.error(err)
|
|
process.exit(1)
|
|
}
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
export default fastifySetup
|