export default async function (req: Request): Promise<Response> {
const { searchParams } = new URL(req.url);
const title = searchParams.get('title') || 'Hello from val.town';
const bgColor = searchParams.get('bgColor') || '#e15a24';
const textColor = searchParams.get('textColor') || '#1A202C';
const svg = `
<svg width="1200" height="630" xmlns="http://www.w3.org/2000/svg">
<defs>
<style>
@import url('https://fonts.googleapis.com/css2?family=Aleo:wght@700&display=swap');
</style>
</defs>
<rect width="1200" height="630" fill="${bgColor}" />
<text x="600" y="315" font-family="Aleo, serif" font-size="60" font-weight="bold" fill="${textColor}" text-anchor="middle" dominant-baseline="middle">${title}</text>
</svg>
`;
const apiUrl = 'https://hcti.io/v1/image';
const apiKey = Deno.env.get("HTCI");
if (!apiKey) {
console.error('HTCI API key is missing');
return new Response('Internal Server Error', { status: 500 });
}
console.log('HTCI API Key:', apiKey);
try {
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
html: svg,
device: { width: 1200, height: 630 },
}),
});
if (!response.ok) {
throw new Error(`Conversion failed: ${response.statusText}`);
}
const jsonResponse = await response.json();
const pngUrl = jsonResponse.url;
const pngResponse = await fetch(pngUrl);
const pngBuffer = await pngResponse.arrayBuffer();
return new Response(pngBuffer, {
headers: {
'Content-Type': 'image/png',
'Cache-Control': 'public, max-age=604800, immutable'
},
});
} catch (error) {
console.error('Error converting SVG to PNG:', error);
return new Response('Error generating image', { status: 500 });
}
}