import { Controller, Get, NotFoundException, Param, Res, } from '@nestjs/common'; import type { Response } from 'express'; import { createReadStream, existsSync } from 'fs'; import { basename, extname, join } from 'path'; import { resolveWebsiteDocsRoot } from './website-docs.paths'; const ALLOWED_FILES = new Set([ 'index.html', 'openapi.json', 'AI_PROMPT.md', 'Meshkee-Website-API.postman_collection.json', 'Meshkee-Website-API.global.postman_environment.json', ]); const CONTENT_TYPES: Record = { '.html': 'text/html; charset=utf-8', '.json': 'application/json; charset=utf-8', '.md': 'text/markdown; charset=utf-8', }; @Controller('docs/website') export class WebsiteDocsController { private readonly root = resolveWebsiteDocsRoot(); @Get() getIndex(@Res() res: Response) { return this.sendFile(res, 'index.html'); } @Get(':fileName') getFile(@Param('fileName') fileName: string, @Res() res: Response) { const safe = basename(fileName); if (!ALLOWED_FILES.has(safe)) { throw new NotFoundException(`Unknown docs file: ${fileName}`); } return this.sendFile(res, safe); } private sendFile(res: Response, fileName: string) { const filePath = join(this.root, fileName); if (!existsSync(filePath)) { throw new NotFoundException( `Website docs not found on server (${fileName}). Deploy docs/website-api/ with the API.`, ); } const type = CONTENT_TYPES[extname(fileName)] ?? 'application/octet-stream'; res.setHeader('Content-Type', type); res.setHeader('Cache-Control', 'public, max-age=300'); createReadStream(filePath).pipe(res); } }