Skip to content

Instantly share code, notes, and snippets.

@eterps
Last active December 14, 2023 16:08
Show Gist options
  • Save eterps/ac109f1d6a42b8c7d13c7c4afdbcc103 to your computer and use it in GitHub Desktop.
Save eterps/ac109f1d6a42b8c7d13c7c4afdbcc103 to your computer and use it in GitHub Desktop.
/** @jsx jsx */
import { Hono, Context } from "https://deno.land/x/hono/mod.ts";
import { basicAuth } from "https://deno.land/x/hono/middleware.ts";
import { serveStatic } from "https://deno.land/x/hono/middleware.ts";
import { jsx } from "https://deno.land/x/hono/middleware.ts";
import { env } from "https://deno.land/x/hono/helper.ts";
import { getProject, setProject } from './project.ts';
import { renderPrompt } from './prompt.ts';
import { getAIOutput } from './ai.ts';
import Index from "./Index.tsx";
const app = new Hono();
const db = await Deno.openKv(); // Uses FoundationDB for persistence
/* --- Routes --- */
app.use("/public/*", serveStatic({ root: "./" }));
app.use("/favicon.ico", serveStatic({ path: "./favicon.ico" }));
app.get(
"/project/:id",
basicAuth({ username: "test", password: "blendol" }),
async (context: Context) => {
const id = context.req.param("id");
const project = await getProject(db, id)
return context.html(<Index project={project} />);
}
);
app.post(
"/project/:id",
basicAuth({ username: "test", password: "blendol" }),
async (context: Context) => {
const id = context.req.param("id");
const formData = await context.req.parseBody<FormData>();
const prompt = renderPrompt(formData);
const { OPENAI_API_KEY } = env<{ OPENAI_API_KEY: string }>(context);
const aiOutput = await getAIOutput(OPENAI_API_KEY, prompt);
await setProject(db, id, {formData, aiOutput});
return context.redirect(context.req.path + "#output");
}
);
app.get("*", serveStatic({ path: "./public/fallback.txt" }));
/* --- Server --- */
Deno.serve(app.fetch);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment