Skip to content

Commit

Permalink
feat: serving files from 'public' folder (#23)
Browse files Browse the repository at this point in the history
* feat: setup create route for each static file

* feat: add mime detection

* test: add sample image

* feat: add file paths

* fix: removed unused file

* feat: add cli option for public folder

* feat: add static files path cli option

* Revert "feat: add static files path cli option"

This reverts commit 88306ce.

* fix: remove leftover
  • Loading branch information
bbtgnn authored Dec 6, 2023
1 parent ef76dc2 commit a48afe7
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 10 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added public/file_example_MP3_700KB.mp3
Binary file not shown.
Binary file added public/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions public/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
helo
12 changes: 12 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ program
}
})
)
.addOption(
new Option('--public-directory <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 <number>',
Expand Down
19 changes: 19 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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`);
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface Config {
openapiPath: string;
template: string;
logger: Logger;
publicDirectory: string | undefined;
}

export interface Endpoints {
Expand Down

0 comments on commit a48afe7

Please sign in to comment.