forked from epesooj/webring
Move to Deno+Bunny.net, add a way to test webring locally.
This commit is contained in:
parent
9f66f0b37b
commit
8670aa3412
33 changed files with 599 additions and 3360 deletions
50
src/main.ts
Normal file
50
src/main.ts
Normal file
|
@ -0,0 +1,50 @@
|
|||
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,
|
||||
});
|
||||
});
|
||||
}
|
15
src/next.ts
Normal file
15
src/next.ts
Normal file
|
@ -0,0 +1,15 @@
|
|||
import { next } from './webring.ts';
|
||||
|
||||
export function redirectNext(req: Request) {
|
||||
const id = new URL(req.url).searchParams.get('id') ?? 'main';
|
||||
const item = next(id);
|
||||
const url = item?.url ?? new URL(req.url).origin;
|
||||
|
||||
return new Response('', {
|
||||
status: 303,
|
||||
headers: {
|
||||
location: url,
|
||||
'cache-control': 'no-cache, no-store, no-transform',
|
||||
},
|
||||
});
|
||||
}
|
15
src/previous.ts
Normal file
15
src/previous.ts
Normal file
|
@ -0,0 +1,15 @@
|
|||
import { previous } from './webring.ts';
|
||||
|
||||
export function redirectPrevious(req: Request) {
|
||||
const id = new URL(req.url).searchParams.get('id') ?? 'main';
|
||||
const item = previous(id);
|
||||
const url = item?.url ?? new URL(req.url).origin;
|
||||
|
||||
return new Response('', {
|
||||
status: 303,
|
||||
headers: {
|
||||
location: url,
|
||||
'cache-control': 'no-cache, no-store, no-transform',
|
||||
},
|
||||
});
|
||||
}
|
13
src/random.ts
Normal file
13
src/random.ts
Normal file
|
@ -0,0 +1,13 @@
|
|||
import { randomEntry } from './webring.ts';
|
||||
|
||||
export function redirectRandom(req: Request) {
|
||||
const url = randomEntry()?.url ?? new URL(req.url).origin;
|
||||
|
||||
return new Response('', {
|
||||
status: 303,
|
||||
headers: {
|
||||
location: url,
|
||||
'cache-control': 'no-cache, no-store, no-transform',
|
||||
},
|
||||
});
|
||||
}
|
|
@ -1,15 +1,19 @@
|
|||
import { WebringData, WebringEntry } from './webring';
|
||||
import { readFileSync, writeFileSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { WebringData, WebringEntry } from './webring.ts';
|
||||
import { join } from '@std/path';
|
||||
|
||||
function generateEntryHTML(entry: WebringEntry) {
|
||||
return `<div><a href="${entry.url}">${entry.title}</a><span>by ${entry.author}</span></div>`;
|
||||
}
|
||||
|
||||
let index = readFileSync(
|
||||
join(import.meta.dirname, '../templates/index.html'),
|
||||
'utf8'
|
||||
);
|
||||
const renderedEntries = WebringData.map(generateEntryHTML).join('\n');
|
||||
index = index.replace('{{WEBRING_ENTRIES}}', renderedEntries);
|
||||
writeFileSync(join(import.meta.dirname, '../public/index.html'), index);
|
||||
export function generateIndex() {
|
||||
const decoder = new TextDecoder('utf-8');
|
||||
let index = decoder.decode(
|
||||
Deno.readFileSync(
|
||||
join(import.meta.dirname ?? '', '../templates/index.html')
|
||||
)
|
||||
);
|
||||
const renderedEntries = WebringData.map(generateEntryHTML).join('\n');
|
||||
index = index.replace('{{WEBRING_ENTRIES}}', renderedEntries);
|
||||
|
||||
return index;
|
||||
}
|
||||
|
|
|
@ -1,6 +0,0 @@
|
|||
{
|
||||
"extends": "../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"types": ["node"]
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
import WebringData from '../webring_data.json';
|
||||
import WebringData from '../webring_data.json' with { type: 'json' };
|
||||
export { WebringData };
|
||||
|
||||
export type WebringEntry = (typeof WebringData)[number];
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue