clonegur/backend/server/staticHandler.ts

22 lines
714 B
TypeScript
Raw Normal View History

2023-06-29 19:18:52 +00:00
import RestResponse from "./RestResponse.ts";
import { Handler } from "./Router.ts";
export default function staticHandler(path: string): Handler {
return {
async handle(req) {
try {
2023-06-30 00:37:07 +00:00
const realPath = await Deno.realPath(`${path}/${req.url}`);
const stream = await Deno.open(realPath);
const i = realPath.lastIndexOf('.');
const res = new RestResponse().body(stream.readable);
if (i >= 0) res.contentType(realPath.substring(i + 1));
return res;
2023-06-29 19:18:52 +00:00
}
2023-06-30 00:37:07 +00:00
catch (e) {
console.log(e);
2023-06-29 19:18:52 +00:00
return undefined;
}
}
};
}