#!/usr/bin/env node
/**
* View Swagger documentation in browser
* Opens ReDoc documentation for the generated swagger-output.json
*/
const http = require('http')
const fs = require('fs')
const path = require('path')
const swaggerPath = path.join(__dirname, 'docs', 'swagger-output.json')
const port = 3004
if (!fs.existsSync(swaggerPath)) {
console.error('swagger-output.json not found. Run "node swagger.js" first.')
process.exit(1)
}
const html = `
Audiobookshelf API Documentation
`
const server = http.createServer((req, res) => {
if (req.url === '/') {
res.writeHead(200, { 'Content-Type': 'text/html' })
res.end(html)
} else if (req.url === '/swagger.json') {
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(fs.readFileSync(swaggerPath))
} else {
res.writeHead(404)
res.end('Not found')
}
})
server.listen(port, () => {
console.log(`ReDoc API documentation: http://localhost:${port}`)
})