| 1 | import { Pool } from "pg"; |
| 2 | import { attachDatabasePool } from "@neon/functions"; |
| 3 | |
| 4 | const pool = new Pool({ connectionString: process.env.DATABASE_URL }); |
| 5 | attachDatabasePool(pool); |
| 6 | |
| 7 | export default { |
| 8 | async fetch(request: Request) { |
| 9 | const path = new URL(request.url).pathname; |
| 10 | if (path === "/health") return Response.json({ ok: true }); |
| 11 | // A public Function URL is not protected by functions:invoke credentials. |
| 12 | const token = process.env.APP_TOKEN; |
| 13 | if (!token || request.headers.get("authorization") !== `Bearer ${token}`) |
| 14 | return new Response("Unauthorized", { status: 401 }); |
| 15 | const { rows } = await pool.query("SELECT current_database() AS database"); |
| 16 | console.log("database queried"); |
| 17 | return Response.json(rows); |
| 18 | }, |
| 19 | }; |