50 lines
1,009 B
TypeScript
50 lines
1,009 B
TypeScript
import { generateIndex } from './render_templates.ts';
|
|
import { redirectNext } from './next.ts';
|
|
import { redirectPrevious } from './previous.ts';
|
|
import { redirectRandom } from './random.ts';
|
|
|
|
export function handleFunctions(req: Request) {
|
|
const url = new URL(req.url);
|
|
|
|
switch (url.pathname) {
|
|
case '/next': {
|
|
return redirectNext(req);
|
|
}
|
|
|
|
case '/previous': {
|
|
return redirectPrevious(req);
|
|
}
|
|
|
|
case '/random': {
|
|
return redirectRandom(req);
|
|
}
|
|
}
|
|
|
|
return undefined;
|
|
}
|
|
|
|
if (import.meta.main) {
|
|
const index = generateIndex();
|
|
|
|
Deno.serve({ hostname: '127.0.0.1' }, (req) => {
|
|
const funcResp = handleFunctions(req);
|
|
|
|
if (funcResp !== undefined) {
|
|
return funcResp;
|
|
}
|
|
|
|
const url = new URL(req.url);
|
|
if (url.pathname === '/') {
|
|
return new Response(index, {
|
|
status: 200,
|
|
headers: {
|
|
'content-type': 'text/html',
|
|
},
|
|
});
|
|
}
|
|
|
|
return new Response(null, {
|
|
status: 404,
|
|
});
|
|
});
|
|
}
|