A Cloudflare Worker which works in conjunction with https://github.com/indexxing/bsky-alt-text
1import { fromHono } from "chanfana";
2import { cors } from "hono/cors";
3import { Hono } from "hono";
4
5import { CondenseTextEndpoint } from "./endpoints/condense_text";
6import { GenerateEndpoint } from "./endpoints/generate";
7import { geminiMiddleware } from "./middleware/gemini";
8import { authMiddleware } from "./middleware/auth";
9import { Env, Variables } from "./types";
10
11const app = new Hono<{ Bindings: Env; Variables: Variables }>();
12
13// The path which all routes are served from, for easy use with Worker Routes on custom domains.
14const rootPath = "/api/altText/";
15
16app.use(
17 "*",
18 cors({
19 origin: [
20 "https://indexx.dev",
21 "chrome-extension://",
22 "safari-web-extension://",
23 "moz-extension://",
24 ],
25 allowMethods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
26 allowHeaders: ["*"],
27 maxAge: 600,
28 credentials: true,
29 }),
30);
31
32const openapi = fromHono(app, {
33 schema: {
34 info: {
35 title: "Bluesky Alt Text",
36 description:
37 "Endpoints for https://github.com/indexxing/Bluesky-Alt-Text",
38 version: "1.0",
39 },
40 },
41 docs_url: rootPath,
42 openapi_url: rootPath + "openapi.json",
43});
44
45openapi.registry.registerComponent("securitySchemes", "bearerAuth", {
46 type: "http",
47 scheme: "bearer",
48 bearerFormat: "API key",
49});
50
51// Define Middlewares
52app.use("*", geminiMiddleware);
53app.use(rootPath + "generate", authMiddleware);
54app.use(rootPath + "condense_text", authMiddleware);
55
56// Define Routes
57openapi.post(rootPath + "generate", GenerateEndpoint);
58openapi.post(rootPath + "condense_text", CondenseTextEndpoint);
59
60export default app;