41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
|
import { Handlers } from "$fresh/server.ts";
|
||
|
import { Status, STATUS_TEXT } from "https://deno.land/std@0.195.0/http/mod.ts";
|
||
|
import { fromFileUrl, join } from "$std/path/mod.ts";
|
||
|
|
||
|
|
||
|
export const handler: Handlers = {
|
||
|
async GET(req, ctx): Promise<Response> {
|
||
|
const headers = new Headers({
|
||
|
"content-type": "application/json"
|
||
|
});
|
||
|
const path = join(fromFileUrl(import.meta.url), "../../../../dist", `${ctx.params.name}.json`);
|
||
|
console.log("path : ",path)
|
||
|
let stat;
|
||
|
try {
|
||
|
stat = await Deno.stat(path);
|
||
|
}
|
||
|
catch(err){
|
||
|
if(err instanceof Deno.errors.NotFound){
|
||
|
return await ctx.renderNotFound();
|
||
|
}
|
||
|
else {
|
||
|
throw err;
|
||
|
}
|
||
|
}
|
||
|
const mtime = stat.mtime ?? new Date(0);
|
||
|
const body = await Deno.readTextFile(path);
|
||
|
headers.set("last-modified", mtime.toUTCString());
|
||
|
|
||
|
const ifModifiedSinceValue = req.headers.get("if-modified-since");
|
||
|
if ( ifModifiedSinceValue &&
|
||
|
mtime.getTime() != new Date(ifModifiedSinceValue).getTime()
|
||
|
){
|
||
|
return new Response(null, {
|
||
|
status: Status.NotModified,
|
||
|
statusText: STATUS_TEXT[Status.NotModified]
|
||
|
})
|
||
|
}
|
||
|
return new Response(body, {headers});
|
||
|
},
|
||
|
};
|