summaryrefslogtreecommitdiff
path: root/app/src/lib/server
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/lib/server')
-rw-r--r--app/src/lib/server/db.ts14
-rw-r--r--app/src/lib/server/s3.ts14
2 files changed, 24 insertions, 4 deletions
diff --git a/app/src/lib/server/db.ts b/app/src/lib/server/db.ts
index db001b7..b35f853 100644
--- a/app/src/lib/server/db.ts
+++ b/app/src/lib/server/db.ts
@@ -1,5 +1,6 @@
import pg from "pg";
import { env } from "$env/dynamic/private";
+import type { Handle } from "@sveltejs/kit";
const pool = new pg.Pool({
database: env.POSTGRES_DB || "postgres",
@@ -15,6 +16,13 @@ pool.on("error", (err, client) => {
console.error("Database error: ", err, client);
});
-export function getDbClient(): Promise<pg.PoolClient> {
- return pool.connect();
-}
+export const dbHandle = (async ({ event, resolve }) => {
+ const dbClient = await pool.connect();
+ event.locals.dbClient = dbClient;
+
+ try {
+ return await resolve(event);
+ } finally {
+ dbClient.release();
+ }
+}) satisfies Handle;
diff --git a/app/src/lib/server/s3.ts b/app/src/lib/server/s3.ts
index a1a28fa..5d0dec9 100644
--- a/app/src/lib/server/s3.ts
+++ b/app/src/lib/server/s3.ts
@@ -1,7 +1,8 @@
import { S3Client } from "@aws-sdk/client-s3";
+import type { Handle } from "@sveltejs/kit";
// We would obviously read from .env in prod, but it's an annoying indirection for this demo.
-export function getS3Client(): S3Client {
+function getS3Client(): S3Client {
const client = new S3Client({
endpoint: "http://localhost:9000",
region: "us-east-1", // Required, but ignored for local usage.
@@ -14,3 +15,14 @@ export function getS3Client(): S3Client {
return client;
}
+
+export const s3Handle = (async ({ event, resolve }) => {
+ const s3Client = getS3Client();
+ event.locals.s3Client = s3Client;
+
+ try {
+ return await resolve(event);
+ } finally {
+ s3Client.destroy();
+ }
+}) satisfies Handle;