UNPKG

@opengis/bi

Version:

BI data visualization module

49 lines (39 loc) 1.34 kB
import path, { dirname } from 'path'; import { fileURLToPath } from 'url'; import fs from 'fs'; const dir = dirname(fileURLToPath(import.meta.url)); const root = `${dir}/../../`; async function plugin(fastify, opts) { fastify.get('/docs*', async (req, reply) => { if (!fs.existsSync(path.join(root, 'docs/.vitepress/dist/'))) { return reply.status(404).send('docs not exists'); } const { params } = req; const url = params['*']; const filePath = url && url[url.length - 1] !== '/' ? path.join(root, 'docs/.vitepress/dist/', url) : path.join(root, 'docs/.vitepress/dist/', url, 'index.html'); if (!fs.existsSync(filePath)) { return reply.status(404).send('File not found'); } const ext = path.extname(filePath); const mime = { '.js': 'text/javascript', '.css': 'text/css', '.woff2': 'application/font-woff', '.png': 'image/png', '.svg': 'image/svg+xml', '.jpg': 'image/jpg', '.html': 'text/html', '.json': 'application/json', '.pdf': 'application/pdf', }[ext]; const stream = fs.createReadStream(filePath); stream.on('error', (err) => { reply.status(500).send('Error reading file'); }); return mime ? reply.type(mime).send(stream) : reply.send(stream); }); } export default plugin;