clonegur/backend/server/staticHandler.ts

22 lines
714 B
TypeScript

import RestResponse from "./RestResponse.ts";
import { Handler } from "./Router.ts";
export default function staticHandler(path: string): Handler {
return {
async handle(req) {
try {
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;
}
catch (e) {
console.log(e);
return undefined;
}
}
};
}