From f2781e1803369f4c2abc350066ee0c1fa3dfec33 Mon Sep 17 00:00:00 2001 From: Dominic Ferrando Date: Wed, 1 Jul 2026 23:24:27 -0400 Subject: [PATCH] Move env and log into index --- apps/api/src/database/seed.ts | 2 +- apps/api/src/env.ts | 25 --------------------- apps/api/src/index.ts | 42 ++++++++++++++++++++++++++++++++++- apps/api/src/server.ts | 3 +-- 4 files changed, 43 insertions(+), 29 deletions(-) delete mode 100644 apps/api/src/env.ts diff --git a/apps/api/src/database/seed.ts b/apps/api/src/database/seed.ts index 89feab6..72886df 100644 --- a/apps/api/src/database/seed.ts +++ b/apps/api/src/database/seed.ts @@ -1,7 +1,7 @@ import { Kysely, PostgresDialect } from 'kysely'; import { type DB } from 'kysely-codegen'; import { Pool } from "pg"; -import { env } from '../env'; +import { env } from '../index'; const db = new Kysely({ dialect: new PostgresDialect({ diff --git a/apps/api/src/env.ts b/apps/api/src/env.ts deleted file mode 100644 index 5cf79e2..0000000 --- a/apps/api/src/env.ts +++ /dev/null @@ -1,25 +0,0 @@ -function requireEnv(key: string): string { - const val = Bun.env[key]; - if (!val) throw new Error(`Missing required environment variable: ${key}`); - return val; -} - -function optionEnv(key: string, fallback: string = ""): string { - const val = Bun.env[key]; - if (!val) return fallback; - return val; -} - -export const env = { - PRINTFUL_AUTH: requireEnv("PRINTFUL_AUTH"), - PRINTFUL_STORE_ID: requireEnv("PRINTFUL_STORE_ID"), - 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"), - AUTH_SECRET: requireEnv("AUTH_SECRET"), - ADMIN_PASSWORD: requireEnv("ADMIN_PASSWORD"), - DATABASE_URL: requireEnv("DATABASE_URL"), - NODE_ENV: optionEnv("NODE_ENV", "development"), - LOG_LEVEL: optionEnv("LOG_LEVEL", "info"), -}; diff --git a/apps/api/src/index.ts b/apps/api/src/index.ts index cad0896..ceee255 100644 --- a/apps/api/src/index.ts +++ b/apps/api/src/index.ts @@ -1,11 +1,51 @@ import cluster from 'node:cluster' import os from 'node:os' import process from 'node:process' +import { pino } from 'pino'; + +export const log = pino({ + level: Bun.env.LOG_LEVEL ?? "info", + transport: Bun.env.NODE_ENV != "production" + ? { target: "pino-pretty" } + : undefined, +}); + +export const env = { + PRINTFUL_AUTH: requireEnv("PRINTFUL_AUTH"), + PRINTFUL_STORE_ID: requireEnv("PRINTFUL_STORE_ID"), + 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"), + AUTH_SECRET: requireEnv("AUTH_SECRET"), + ADMIN_PASSWORD: requireEnv("ADMIN_PASSWORD"), + DATABASE_URL: requireEnv("DATABASE_URL"), + NODE_ENV: optionEnv("NODE_ENV", "development"), + LOG_LEVEL: optionEnv("LOG_LEVEL", "info"), +}; if (cluster.isPrimary) { for (let i = 0; i < os.availableParallelism(); i++) cluster.fork() } else { await import('./server') - console.log(`Worker ${process.pid} started`) + log.info(`Worker ${process.pid} started`) +} + +function requireEnv(key: string): string { + const val = Bun.env[key]; + if (!val) { + log.error({ name: key }, "Missing required environment variable"); + throw new Error(`Missing required environment variable: ${key}`) + }; + return val; +} + +function optionEnv(key: string, fallback: string = ""): string { + const val = Bun.env[key]; + if (!val) { + log.warn({ name: key }, `Missing optional environmental variable, falling back to "${fallback}"`) + return fallback + }; + return val; } diff --git a/apps/api/src/server.ts b/apps/api/src/server.ts index 0fc8b21..b9787a7 100644 --- a/apps/api/src/server.ts +++ b/apps/api/src/server.ts @@ -20,8 +20,7 @@ import { Webflow, } from "@blade-and-brawn/commerce"; import zipcodesUs from "zipcodes-us"; -import pino from "pino"; -import { env } from "./env"; +import { env, log } from "./index"; import serverTiming from "@elysia/server-timing"; import jwt from "@elysia/jwt";