mirror of
https://github.com/BetterCorp/BetterFrame.git
synced 2026-05-26 17:56:34 +00:00
fix: use pluginCwd for static files + info log on request
Static file path now uses BSB pluginCwd instead of import.meta.dirname. Added info log with method+path on every request via per-request trace. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
57348b14ab
commit
35d184a6dd
3 changed files with 10 additions and 10 deletions
|
|
@ -200,10 +200,12 @@ export class Plugin extends BSBService<InstanceType<typeof Config>, typeof Event
|
|||
onRequest: (event) => {
|
||||
const method = event.req.method ?? "GET";
|
||||
const path = event.req.url ?? "/";
|
||||
event.context.obs = self.createTrace(`${method} ${path}`, {
|
||||
const reqObs = self.createTrace(`${method} ${path}`, {
|
||||
"http.method": method,
|
||||
"http.url": path,
|
||||
});
|
||||
reqObs.log.info("{method} {path}", { method, path });
|
||||
event.context.obs = reqObs;
|
||||
},
|
||||
onError: (error, event) => {
|
||||
const reqObs = event.context.obs ?? obs;
|
||||
|
|
@ -231,7 +233,7 @@ export class Plugin extends BSBService<InstanceType<typeof Config>, typeof Event
|
|||
});
|
||||
|
||||
registerMiddleware(app, deps);
|
||||
registerStaticRoutes(app);
|
||||
registerStaticRoutes(app, this.pluginCwd);
|
||||
registerSetupRoutes(app, deps);
|
||||
registerAuthRoutes(app, deps);
|
||||
registerAdminRoutes(app, deps);
|
||||
|
|
|
|||
|
|
@ -8,11 +8,6 @@ import { existsSync, readFileSync } from "node:fs";
|
|||
import { join, extname, resolve } from "node:path";
|
||||
import { type H3, getRouterParam, createError } from "h3";
|
||||
|
||||
const STATIC_DIR = resolve(
|
||||
import.meta.dirname ?? ".",
|
||||
"../../web-static",
|
||||
);
|
||||
|
||||
const MIME_TYPES: Record<string, string> = {
|
||||
".html": "text/html; charset=utf-8",
|
||||
".css": "text/css; charset=utf-8",
|
||||
|
|
@ -25,12 +20,13 @@ const MIME_TYPES: Record<string, string> = {
|
|||
".woff2": "font/woff2",
|
||||
};
|
||||
|
||||
export function registerStaticRoutes(app: H3): void {
|
||||
export function registerStaticRoutes(app: H3, pluginCwd: string): void {
|
||||
const STATIC_DIR = resolve(pluginCwd, "../../web-static");
|
||||
|
||||
app.get("/static/**:path", (event) => {
|
||||
const reqPath = getRouterParam(event, "path");
|
||||
if (!reqPath) throw createError({ statusCode: 404 });
|
||||
|
||||
// Prevent directory traversal
|
||||
const resolved = resolve(STATIC_DIR, reqPath);
|
||||
if (!resolved.startsWith(STATIC_DIR)) {
|
||||
throw createError({ statusCode: 403 });
|
||||
|
|
|
|||
|
|
@ -169,10 +169,12 @@ export class Plugin extends BSBService<InstanceType<typeof Config>, typeof Event
|
|||
onRequest: (event) => {
|
||||
const method = event.req.method ?? "GET";
|
||||
const path = event.req.url ?? "/";
|
||||
event.context.obs = self.createTrace(`${method} ${path}`, {
|
||||
const reqObs = self.createTrace(`${method} ${path}`, {
|
||||
"http.method": method,
|
||||
"http.url": path,
|
||||
});
|
||||
reqObs.log.info("{method} {path}", { method, path });
|
||||
event.context.obs = reqObs;
|
||||
},
|
||||
onError: (error, event) => {
|
||||
const reqObs = event.context.obs ?? obs;
|
||||
|
|
|
|||
Loading…
Reference in a new issue