export default async function server(request: Request): Promise<Response> {
const url = new URL(request.url);
const parts = url.pathname.split('/').filter(Boolean);
if (parts.length === 0) {
const welcomeHtml = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Kitten Image Generator</title>
<style>
body {
font-family: Arial, sans-serif;
line-height: 1.6;
color: #333;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #f4f4f4;
}
h1 {
color: #2c3e50;
text-align: center;
}
p {
margin-bottom: 20px;
}
.example-image {
display: block;
max-width: 100%;
height: auto;
margin: 20px auto;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}
.instructions {
background-color: #fff;
border-left: 4px solid #3498db;
padding: 15px;
margin-bottom: 20px;
border-radius: 0 5px 5px 0;
}
code {
background-color: #e7e7e7;
padding: 2px 4px;
border-radius: 4px;
}
</style>
</head>
<body>
<h1>Welcome to the Kitten Image Generator!</h1>
<div class="instructions">
<p>Use this service to generate cute kitten images of various sizes:</p>
<p>For square images: <code>/width</code> (e.g., <code>/400</code> for a 400x400 image)</p>
<p>For rectangular images: <code>/width/height</code> (e.g., <code>/400/300</code> for a 400x300 image)</p>
</div>
<p>Here's an example of a generated 400x400 kitten image:</p>
<img src="/400" alt="Example 400x400 kitten" class="example-image">
<p>Start generating your own kitten images by modifying the URL!</p>
</body>
</html>
`;
return new Response(welcomeHtml, { headers: { 'Content-Type': 'text/html' } });
}
let width: number, height: number;
if (parts.length === 1) {
width = height = parseInt(parts[0]);
} else if (parts.length === 2) {
width = parseInt(parts[0]);
height = parseInt(parts[1]);
} else {
return new Response('Invalid URL format. Use /width for square images or /width/height for rectangular images.',
{ status: 400, headers: { 'Content-Type': 'text/plain' } });
}
if (isNaN(width) || isNaN(height) || width <= 0 || height <= 0) {
return new Response('Invalid dimensions. Please use positive integers.',
{ status: 400, headers: { 'Content-Type': 'text/plain' } });
}
width = Math.min(Math.max(width, 64), 1024);
height = Math.min(Math.max(height, 64), 1024);
const prompt = `A cute kitten, high quality, detailed`;
const imageGenerationUrl = `https://maxm-imggenurl.web.val.run/${encodeURIComponent(prompt)}?width=${width}&height=${height}`;
try {
console.log(`Generating image with dimensions: ${width}x${height}`);
const imageResponse = await fetch(imageGenerationUrl);
console.log(`Response status: ${imageResponse.status}`);