diff --git a/package.json b/package.json index 5dba309..524d9f0 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "dotenv": "^16.3.1", "live-directory": "^3.0.3", "lodash": "^4.17.21", + "mime": "^4.0.0", "pino": "^8.16.2", "uWebSockets.js": "github:uNetworking/uWebSockets.js#semver:^20", "zenroom": "^4.2.2" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a76497d..1feb119 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1,4 +1,4 @@ -lockfileVersion: '6.1' +lockfileVersion: '6.0' settings: autoInstallPeers: true @@ -32,6 +32,9 @@ dependencies: lodash: specifier: ^4.17.21 version: 4.17.21 + mime: + specifier: ^4.0.0 + version: 4.0.0 pino: specifier: ^8.16.2 version: 8.16.2 @@ -4000,6 +4003,12 @@ packages: hasBin: true dev: true + /mime@4.0.0: + resolution: {integrity: sha512-pzhgdeqU5pJ9t5WK9m4RT4GgGWqYJylxUf62Yb9datXRwdcw5MjiD1BYI5evF8AgTXN9gtKX3CFLvCUL5fAhEA==} + engines: {node: '>=16'} + hasBin: true + dev: false + /mimic-fn@2.1.0: resolution: {integrity: sha512-OqbOk5oEQeAZ8WXWydlu9HJjz9WVdEIvamMCcXmuqUYjTknH/sqsWvhQ3vgwKFRR1HpjvNBKQ37nbJgYzGqGcg==} engines: {node: '>=6'} diff --git a/public/file_example_MP3_700KB.mp3 b/public/file_example_MP3_700KB.mp3 new file mode 100644 index 0000000..ab94045 Binary files /dev/null and b/public/file_example_MP3_700KB.mp3 differ diff --git a/public/screenshot.png b/public/screenshot.png new file mode 100644 index 0000000..40825c0 Binary files /dev/null and b/public/screenshot.png differ diff --git a/public/test.txt b/public/test.txt new file mode 100644 index 0000000..0bb5e4b --- /dev/null +++ b/public/test.txt @@ -0,0 +1 @@ +helo \ No newline at end of file diff --git a/src/cli.ts b/src/cli.ts index d2ad5d2..ae2f3a5 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -32,6 +32,18 @@ program } }) ) + .addOption( + new Option('--public-directory ', 'specify the static files directory') + .env('PUBLIC_DIR') + .argParser((d) => { + try { + if (statSync(d).isDirectory()) return d; + } catch (e) { + L.error(`${bad} ${d} is not a valid directory`); + process.exit(0); + } + }) + ) .addOption( new Option( '-p, --port ', diff --git a/src/index.ts b/src/index.ts index 7c35fe4..51b127c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -23,9 +23,13 @@ import { openapiTemplate } from './openapi.js'; import { getSchema, handleArrayBuffer, validateData } from './utils.js'; +import mime from 'mime'; +import path from 'path'; +import fs from 'fs'; const L = config.logger; const Dir = Directory.getInstance(); + Dir.ready(async () => { let listen_socket: us_listen_socket; @@ -59,6 +63,20 @@ Dir.ready(async () => { generateRoutes(app); + const { publicDirectory } = config; + if (publicDirectory) { + app.get('/*', (res, req) => { + let file = path.join(publicDirectory, req.getUrl()); + if (fs.existsSync(file)) { + const contentType = mime.getType(file) || 'application/octet-stream'; + res.writeHeader('Content-Type', contentType); + res.end(fs.readFileSync(file)); + } else { + res.writeStatus('404 Not Found').end('Not found'); + } + }); + } + app.listen(config.port, (socket) => { const port = us_socket_local_port(socket); listen_socket = socket; @@ -71,6 +89,7 @@ Dir.ready(async () => { us_listen_socket_close(listen_socket); const app = App(); generateRoutes(app); + // generatePublicFilesRoutes(app); app.listen(port, (socket) => { listen_socket = socket; L.info(`Swagger UI is running on http://${config.hostname}:${port}/docs`); diff --git a/src/types.ts b/src/types.ts index 031272b..1bba12e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -18,6 +18,7 @@ export interface Config { openapiPath: string; template: string; logger: Logger; + publicDirectory: string | undefined; } export interface Endpoints {