feat: random endpoint
This commit is contained in:
parent
0fa86f4a81
commit
31c37792f6
1 changed files with 36 additions and 0 deletions
36
apps/blog/src/pages/random.ts
Normal file
36
apps/blog/src/pages/random.ts
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import { getCollection } from 'astro:content';
|
||||
import yaml from 'js-yaml';
|
||||
import bookmarksRaw from '../data/bookmarks.yaml?raw';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import type { APIContext } from 'astro';
|
||||
|
||||
export const prerender = false;
|
||||
|
||||
interface Bookmark {
|
||||
date: string;
|
||||
title: string;
|
||||
url: string;
|
||||
}
|
||||
|
||||
export async function GET(context: APIContext) {
|
||||
const site = context.site?.origin ?? 'https://wynne.rs';
|
||||
const posts = await getCollection('posts');
|
||||
const bookmarks = yaml.load(bookmarksRaw) as Bookmark[];
|
||||
|
||||
const txtDir = path.join(process.cwd(), 'public/txt');
|
||||
const txtFiles = fs.existsSync(txtDir)
|
||||
? fs.readdirSync(txtDir).filter(file => file.endsWith('.txt'))
|
||||
: [];
|
||||
|
||||
const urls = [
|
||||
...posts.map(post => `/blog/${post.id}`),
|
||||
...txtFiles.map(txt => `/txt/${txt}`),
|
||||
...bookmarks.map(b => b.url),
|
||||
];
|
||||
|
||||
const random = urls[Math.floor(Math.random() * urls.length)];
|
||||
const redirectUrl = random.startsWith('http') ? random : `${site}${random}`;
|
||||
|
||||
return Response.redirect(redirectUrl, 302);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue