Some cleanup and add server timing

This commit is contained in:
Dominic Ferrando
2026-06-26 13:06:40 -04:00
parent df29d3f2f1
commit a689417aed
5 changed files with 22 additions and 17 deletions
+4 -4
View File
@@ -4,7 +4,7 @@ function requireEnv(key: string): string {
return val;
}
function optionEnv(key: string, fallback: string): string {
function optionEnv(key: string, fallback: string = ""): string {
const val = Bun.env[key];
if (!val) return fallback;
return val;
@@ -16,7 +16,7 @@ export const env = {
WEBFLOW_SITE_ID: requireEnv("WEBFLOW_SITE_ID"),
WEBFLOW_COLLECTION_ID: requireEnv("WEBFLOW_COLLECTION_ID"),
WEBFLOW_AUTH: requireEnv("WEBFLOW_AUTH"),
WEBFLOW_WEBHOOK_SECRET: requireEnv("WEBFLOW_WEBHOOK_SECRET"),
LOG_LEVEL: optionEnv("LOG_LEVEL", "info"),
NODE_ENV: optionEnv("NODE_ENV", "development")
WEBFLOW_WEBHOOK_SECRET: optionEnv("WEBFLOW_WEBHOOK_SECRET"),
NODE_ENV: optionEnv("NODE_ENV", "development"),
LOG_LEVEL: optionEnv("LOG_LEVEL", "info")
};
+12 -13
View File
@@ -23,10 +23,11 @@ import zipcodesUs from "zipcodes-us";
import z from "zod";
import pino from "pino";
import { env } from "./env";
import serverTiming from "@elysia/server-timing";
const log = pino({
level: env.LOG_LEVEL,
transport: env.NODE_ENV === "development"
transport: env.NODE_ENV != "production"
? { target: "pino-pretty" }
: undefined,
});
@@ -54,10 +55,7 @@ const productSyncer = new ProductSyncer(printful, webflow, log);
// ELYSIA
// -----------
export const app = new Elysia()
.derive(() => ({
startTime: Date.now(),
requestId: crypto.randomUUID(),
}))
.use(serverTiming())
.use(
cors({
origin: [
@@ -83,28 +81,29 @@ export const app = new Elysia()
WebflowError,
})
.onError(({ code, error, startTime, requestId }) => {
const duration = Date.now() - (startTime ?? Date.now());
.onError(({ code, error }) => {
switch (code) {
case "PrintfulError":
case "WebflowError":
log.error(
{ requestId, duration, upstreamStatus: error.status, payload: error.payload },
{ upstreamStatus: error.status, payload: error.payload },
error.message,
);
return status(502, { error: error.message });
case "NOT_FOUND":
return status(404, { error: "Not found" });
case "VALIDATION":
return status(400, { error: error.message });
default:
log.error({ requestId, duration, err: error }, "unhandled error");
log.error({ err: error }, "unhandled error");
}
})
.onAfterHandle(({ request, set, startTime, requestId }) => {
.onAfterHandle(({ request, set }) => {
log.info({
requestId,
method: request.method,
path: new URL(request.url).pathname,
status: set.status ?? 200,
duration: Date.now() - startTime,
status: set.status ?? 200
}, "request");
})