mirror of
https://github.com/Redocly/redoc.git
synced 2024-11-10 19:06:34 +03:00
fix: add handle local files for serve command (#1810)
This commit is contained in:
parent
30b3b57b26
commit
117071ee83
36
cli/index.ts
36
cli/index.ts
|
@ -6,7 +6,7 @@ import { ServerStyleSheet } from 'styled-components';
|
||||||
|
|
||||||
import { compile } from 'handlebars';
|
import { compile } from 'handlebars';
|
||||||
import { createServer, IncomingMessage, ServerResponse } from 'http';
|
import { createServer, IncomingMessage, ServerResponse } from 'http';
|
||||||
import { dirname, join, resolve } from 'path';
|
import { dirname, join, resolve, extname as getExtName } from 'path';
|
||||||
|
|
||||||
import * as zlib from 'zlib';
|
import * as zlib from 'zlib';
|
||||||
|
|
||||||
|
@ -39,6 +39,24 @@ interface Options {
|
||||||
redocOptions?: any;
|
redocOptions?: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const mimeTypes = {
|
||||||
|
'.html': 'text/html',
|
||||||
|
'.js': 'text/javascript',
|
||||||
|
'.css': 'text/css',
|
||||||
|
'.json': 'application/json',
|
||||||
|
'.png': 'image/png',
|
||||||
|
'.jpg': 'image/jpg',
|
||||||
|
'.gif': 'image/gif',
|
||||||
|
'.svg': 'image/svg+xml',
|
||||||
|
'.wav': 'audio/wav',
|
||||||
|
'.mp4': 'video/mp4',
|
||||||
|
'.woff': 'application/font-woff',
|
||||||
|
'.ttf': 'application/font-ttf',
|
||||||
|
'.eot': 'application/vnd.ms-fontobject',
|
||||||
|
'.otf': 'application/font-otf',
|
||||||
|
'.wasm': 'application/wasm',
|
||||||
|
};
|
||||||
|
|
||||||
const BUNDLES_DIR = dirname(require.resolve('redoc'));
|
const BUNDLES_DIR = dirname(require.resolve('redoc'));
|
||||||
|
|
||||||
/* tslint:disable-next-line */
|
/* tslint:disable-next-line */
|
||||||
|
@ -197,9 +215,19 @@ async function serve(host: string, port: number, pathToSpec: string, options: Op
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
response.writeHead(404);
|
try {
|
||||||
response.write('Not found');
|
const filePath = join(dirname(pathToSpec), request.url || '');
|
||||||
response.end();
|
const extname = String(getExtName(filePath)).toLowerCase() as keyof typeof mimeTypes;
|
||||||
|
|
||||||
|
const contentType = mimeTypes[extname] || 'application/octet-stream';
|
||||||
|
respondWithGzip(createReadStream(filePath), request, response, {
|
||||||
|
'Content-Type': contentType,
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
response.writeHead(404);
|
||||||
|
response.write('Not found');
|
||||||
|
response.end();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.timeEnd('GET ' + request.url);
|
console.timeEnd('GET ' + request.url);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user