A Cloudflare Worker which works in conjunction with https://github.com/indexxing/bsky-alt-text
at main 1.7 kB view raw
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: (origin) => { 20 const allowedOrigins = [ 21 "https://indexx.dev", 22 "chrome-extension://", 23 "safari-web-extension://", 24 "moz-extension://", 25 ]; 26 27 if ( 28 origin && 29 allowedOrigins.some((allowed) => origin.startsWith(allowed)) 30 ) { 31 return origin; 32 } 33 return null; 34 }, 35 allowMethods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"], 36 allowHeaders: ["*"], 37 maxAge: 600, 38 credentials: true, 39 }), 40); 41 42const openapi = fromHono(app, { 43 schema: { 44 info: { 45 title: "Bluesky Alt Text", 46 description: 47 "Endpoints for https://github.com/indexxing/Bluesky-Alt-Text", 48 version: "1.0", 49 }, 50 }, 51 docs_url: rootPath, 52 openapi_url: rootPath + "openapi.json", 53}); 54 55openapi.registry.registerComponent("securitySchemes", "bearerAuth", { 56 type: "http", 57 scheme: "bearer", 58 bearerFormat: "API key", 59}); 60 61// Define Middlewares 62app.use("*", geminiMiddleware); 63app.use(rootPath + "generate", authMiddleware); 64app.use(rootPath + "condense_text", authMiddleware); 65 66// Define Routes 67openapi.post(rootPath + "generate", GenerateEndpoint); 68openapi.post(rootPath + "condense_text", CondenseTextEndpoint); 69 70export default app;