feat: initial lunarfront-manager app

This commit is contained in:
Ryan Moon
2026-04-03 06:23:56 -05:00
commit 8287fbf5b8
16 changed files with 793 additions and 0 deletions

46
src/routes/customers.ts Normal file
View File

@@ -0,0 +1,46 @@
import type { FastifyInstance } from "fastify";
import { z } from "zod";
import { createDatabase, createDatabaseUser, deleteDatabase, deleteDatabaseUser } from "../services/do";
import { addCustomerToPool, removeCustomerFromPool } from "../services/pgbouncer";
import { addCustomerChart, removeCustomerChart } from "../services/git";
const ProvisionSchema = z.object({
name: z.string().min(2).max(32).regex(/^[a-z0-9-]+$/, "lowercase letters, numbers, and hyphens only"),
appVersion: z.string().default("latest"),
});
export async function customerRoutes(app: FastifyInstance) {
app.post("/customers", async (req, reply) => {
const body = ProvisionSchema.parse(req.body);
const slug = body.name;
app.log.info({ slug }, "provisioning customer");
const [, user] = await Promise.all([
createDatabase(slug),
createDatabaseUser(slug),
]);
await addCustomerToPool(slug, user.password);
addCustomerChart(slug, body.appVersion);
app.log.info({ slug }, "customer provisioned");
return reply.code(201).send({ slug, status: "provisioned" });
});
app.delete("/customers/:slug", async (req, reply) => {
const { slug } = req.params as { slug: string };
app.log.info({ slug }, "deprovisioning customer");
removeCustomerChart(slug);
await removeCustomerFromPool(slug);
await Promise.all([
deleteDatabase(slug),
deleteDatabaseUser(slug),
]);
app.log.info({ slug }, "customer deprovisioned");
return reply.code(204).send();
});
}