Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: serving files from 'public' folder #23

Merged
merged 9 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
16 changes: 16 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,17 @@ Dir.ready(async () => {

generateRoutes(app);

app.get('/*', (res, req) => {
let file = path.join('public', 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 +86,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