Compare commits

...

6 Commits

4 changed files with 42 additions and 41 deletions

View File

@ -3,6 +3,9 @@
"module": "index.ts",
"type": "module",
"private": true,
"scripts": {
"start": "bun run src/index.ts"
},
"devDependencies": {
"@types/bun": "latest",
"@types/pg": "^8.15.6",

View File

@ -6,7 +6,7 @@ import { styleText } from "node:util";
const CORS_HEADERS = {
"Access-Control-Allow-Origin": ENV.FRONTEND_URL,
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Authorization",
"Access-Control-Allow-Headers": "*",
};
const allowCors = async (_: Request): Promise<Response> => {
@ -16,7 +16,7 @@ const allowCors = async (_: Request): Promise<Response> => {
type Handler = (req: Request) => Promise<Response>;
type Middleware = (fn: Handler) => Handler;
const withCors = (fn: Handler): Handler => {
const withCors: Middleware = (fn) => {
return async (req) => {
const res = await fn(req);
@ -42,7 +42,7 @@ const getColors = (status: number): Parameters<typeof styleText>[0] => {
}
};
const withLogger = (fn: Handler): Handler => {
const withLogger: Middleware = (fn) => {
return async (req) => {
const res = await fn(req);
@ -58,26 +58,14 @@ const withLogger = (fn: Handler): Handler => {
};
};
const withFrontendReferrer: Middleware = (fn) => {
return async (req) => {
const referrer = req.headers.get("referrer");
if (referrer !== ENV.FRONTEND_URL) {
return new Response(undefined, { status: 403 });
}
return fn(req);
};
};
const server = Bun.serve({
port: ENV.PORT,
routes: {
"/health": new Response("alive!"),
"/sign": {
GET: withFrontendReferrer(withLogger(withCors(getPetitions))),
POST: withFrontendReferrer(withLogger(withCors(signPetition))),
OPTIONS: withFrontendReferrer(withLogger(allowCors)),
GET: withLogger(withCors(getPetitions)),
POST: withLogger(withCors(signPetition)),
OPTIONS: withLogger(allowCors),
},
},
});

View File

@ -1,5 +1,6 @@
import { db } from "./database";
import { signaturesTable } from "./schema";
import { desc } from "drizzle-orm";
export const insertSignature = async (
signature: typeof signaturesTable.$inferInsert,
@ -15,5 +16,8 @@ export const insertSignature = async (
export const getSignatures = async (): Promise<
Array<typeof signaturesTable.$inferSelect>
> => {
return db.select().from(signaturesTable);
return db
.select()
.from(signaturesTable)
.orderBy(desc(signaturesTable.createdAt));
};

View File

@ -6,9 +6,15 @@ import { ArrowLeft, Users } from "lucide-react";
import { usePetitions } from "@/state";
const Testimonies = () => {
const { signatures } = usePetitions();
const { signatures: allSignatures } = usePetitions();
const signatures = allSignatures
.filter((s) => s.comment != null)
.map((s) => ({
...s,
name: s.name ?? "Anonymous",
}));
const totalCount = signatures.length;
const totalCount = allSignatures.length;
const navigate = useNavigate();