52 lines
1.4 KiB
TypeScript
52 lines
1.4 KiB
TypeScript
import { createRequestHandler } from "@react-router/express";
|
|
import { drizzle } from "drizzle-orm/postgres-js";
|
|
import express from "express";
|
|
import postgres from "postgres";
|
|
import "react-router";
|
|
|
|
import { DatabaseContext } from "~/database/context";
|
|
import * as schema from "~/database/schema";
|
|
|
|
declare module "react-router" {
|
|
interface AppLoadContext {
|
|
VALUE_FROM_EXPRESS: string;
|
|
}
|
|
}
|
|
|
|
export const app = express();
|
|
|
|
if (!process.env.DATABASE_URL) throw new Error("DATABASE_URL is required");
|
|
|
|
const client = postgres(process.env.DATABASE_URL);
|
|
const db = drizzle(client, { schema });
|
|
app.use((_, __, next) => DatabaseContext.run(db, next));
|
|
|
|
// API endpoint to record castration
|
|
app.post("/api/castration", express.json(), async (req, res) => {
|
|
try {
|
|
const { gender } = req.body;
|
|
|
|
if (!gender || !["male", "female"].includes(gender)) {
|
|
return res.status(400).json({ error: "Invalid gender" });
|
|
}
|
|
|
|
const result = await db.insert(schema.castrations).values({ gender });
|
|
|
|
return res.json({ success: true, result });
|
|
} catch (error) {
|
|
console.error("Error recording castration:", error);
|
|
return res.status(500).json({ error: "Failed to record castration" });
|
|
}
|
|
});
|
|
|
|
app.use(
|
|
createRequestHandler({
|
|
build: () => import("virtual:react-router/server-build"),
|
|
getLoadContext() {
|
|
return {
|
|
VALUE_FROM_EXPRESS: "Hello from Express",
|
|
};
|
|
},
|
|
}),
|
|
);
|