-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
36 lines (29 loc) · 773 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { Hono } from "hono";
import { generateAltText } from "./utils";
const app = new Hono();
app.post("/api/v1/image", async (c) => {
try {
const { url } = await c.req.json();
// Validate the provided URL
if (!url || !/^https?:\/\/.+\.(jpg|jpeg|png|gif)$/i.test(url)) {
return c.text(
"Invalid image URL. Please provide a valid image URL.",
400
);
}
const altText = await generateAltText(url, c.env);
return c.json({
imageUrl: url,
altText: altText
});
} catch (error) {
console.error("Error processing the image:", error);
return c.text("Error processing the image.", 500);
}
});
app.notFound((c) => {
return c.text("Not Found", 404);
});
export default {
fetch: app.fetch
};