Skip to content

Commit 8d0c14f

Browse files
committed
Enhance environment variable handling and add health check endpoints
- Updated env.ts to improve environment variable validation using Zod. - Introduced a new database module in index.ts, establishing a connection to PostgreSQL. - Added health check endpoints for server and database status in the application.
1 parent dfdee1d commit 8d0c14f

3 files changed

Lines changed: 55 additions & 3 deletions

File tree

‎backend/env.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
import { z } from "zod"
22

3-
export const env = z.object({
4-
DATABASE_URL: z.string().url()
5-
}).parse(process.env)
3+
const envSchema = z.object({
4+
DATABASE_URL: z.url()
5+
})
6+
7+
export const env = envSchema.parse(process.env)
8+
9+
const variableKeys = Object.keys(env).length
10+
const variableValues = Object.values(env).length
11+
console.log(
12+
"✅ Environment variables loaded successfully: "
13+
+ variableValues + "/" + variableKeys
14+
)

‎backend/src/database/index.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
import { Pool } from "pg"
3+
import { env } from '@/env';
4+
import { drizzle } from 'drizzle-orm/node-postgres';
5+
6+
export const pool = new Pool({
7+
connectionString: env.DATABASE_URL
8+
})
9+
10+
export const db = drizzle(pool)

‎backend/src/index.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,45 @@
11
import "dotenv/config"
22
import "@/env" // load environment variables and match with zod schema defined in env.ts
33
import { Elysia } from "elysia";
4+
import { sql } from "drizzle-orm";
5+
import { db } from "@/src/database";
46
import { openapi } from '@elysiajs/openapi'
57
import { quote } from "@/src/module/quote/controller";
8+
import { logger } from "@bogeychan/elysia-logger";
69

710
const app = new Elysia()
811
.use(openapi())
912
.get("/", () => "Hello Elysia")
13+
.use(logger({
14+
level: "error"
15+
}))
16+
.group("/health", (app) => {
17+
return app
18+
.get("/", () => {
19+
return {
20+
status: "ok"
21+
}
22+
}, {
23+
detail: {
24+
summary: "Check server health",
25+
description: "Check server health",
26+
tags: ["health"],
27+
}
28+
})
29+
.get("/db", async () => {
30+
await db.execute(sql`select 1`)
31+
console.log("✅ Database connected successfully")
32+
return {
33+
status: "ok"
34+
}
35+
}, {
36+
detail: {
37+
summary: "Check database connection",
38+
description: "Check database connection",
39+
tags: ["health"],
40+
}
41+
})
42+
})
1043
.use(quote)
1144
.listen(3000);
1245

0 commit comments

Comments
 (0)