const HTML = `
Just Ask Claude, Bro.
Just ask Claude, bro.
Because sometimes people just need a little nudge.
Generate a passive-aggressive helpful link 👇
Someone thought you might want Claude's help with…
claude.ai › search ›
`;
const CORS = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
};
function slugify() {
return Math.random().toString(36).substring(2, 8);
}
export default {
async fetch(req, env) {
const url = new URL(req.url);
// CORS preflight
if (req.method === 'OPTIONS') {
return new Response(null, { headers: CORS });
}
// Serve frontend
if (req.method === 'GET' && url.pathname === '/') {
return new Response(HTML, { headers: { 'Content-Type': 'text/html' } });
}
// POST /create — store a new slug
if (req.method === 'POST' && url.pathname === '/create') {
const { question } = await req.json();
if (!question?.trim()) {
return Response.json({ error: 'Missing question' }, { status: 400, headers: CORS });
}
let slug, exists;
do {
slug = slugify();
const row = await env.DB.prepare('SELECT slug FROM links WHERE slug = ?').bind(slug).first();
exists = !!row;
} while (exists);
await env.DB.prepare(
'INSERT INTO links (slug, question, created_at) VALUES (?, ?, ?)'
).bind(slug, question.trim(), Date.now()).run();
return Response.json(
{ slug, url: `https://askclaude.trypennie.dev/${slug}` },
{ headers: CORS }
);
}
// GET /:slug — look up and serve demo page with q param
const slug = url.pathname.replace('/', '');
if (slug) {
const row = await env.DB.prepare('SELECT question FROM links WHERE slug = ?').bind(slug).first();
if (!row) return new Response('Not found', { status: 404 });
const dest = `https://askclaude.trypennie.dev/?q=${encodeURIComponent(row.question)}`;
return Response.redirect(dest, 302);
}
return new Response('Not found', { status: 404 });
}
};