Skip to content

Commit ce9a178

Browse files
committed
ADD - db setup
1 parent 98cb4a6 commit ce9a178

5 files changed

Lines changed: 265 additions & 3 deletions

File tree

.gitignore

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,45 @@ coverage/
2929
Thumbs.db
3030
.idea/
3131
.vscode/
32+
# See https://help.github.com/articles/ignoring-files for more about ignoring files.
33+
#
34+
# Temporary files generated by your text editor or operating system
35+
# belong in git's global ignore instead:
36+
# `$XDG_CONFIG_HOME/git/ignore` or `~/.config/git/ignore`
37+
38+
# Ignore bundler config.
39+
/.bundle
40+
41+
# Ignore all environment files (commit .env.example as a template).
42+
/.env*
43+
!.env.example
44+
45+
# Ignore all logfiles and tempfiles.
46+
/log/*
47+
/tmp/*
48+
!/log/.keep
49+
!/tmp/.keep
50+
51+
# Ignore pidfiles, but keep the directory.
52+
/tmp/pids/*
53+
!/tmp/pids/
54+
!/tmp/pids/.keep
55+
56+
# Ignore storage (uploaded files in development and any SQLite databases).
57+
/storage/*
58+
!/storage/.keep
59+
/tmp/storage/*
60+
!/tmp/storage/
61+
!/tmp/storage/.keep
62+
.DS_Store
63+
64+
/public/assets
65+
66+
# Ignore key files for decrypting credentials and more.
67+
/config/*.key
68+
*.keep
69+
vendor
70+
app/.DS_Store
71+
app/assets/.DS_Store
72+
app/assets/images/.DS_Store
73+
app/assets/images/RSVP_comp/.DS_Store

package-lock.json

Lines changed: 161 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/db.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import pg from "pg";
2+
3+
const { Pool } = pg;
4+
5+
const connectionString = process.env.DATABASE_URL;
6+
7+
const getSslConfig = () => {
8+
const sslMode = process.env.PGSSLMODE;
9+
10+
if (sslMode === "require" || sslMode === "no-verify") {
11+
return { rejectUnauthorized: false };
12+
}
13+
14+
return undefined;
15+
};
16+
17+
export const hasDatabaseConfig = Boolean(connectionString);
18+
19+
export const pool = hasDatabaseConfig
20+
? new Pool({
21+
connectionString,
22+
ssl: getSslConfig(),
23+
})
24+
: null;
25+
26+
export async function checkDatabaseConnection() {
27+
if (!pool) {
28+
return {
29+
ok: false,
30+
configured: false,
31+
message: "DATABASE_URL is not set.",
32+
};
33+
}
34+
35+
const result = await pool.query("select now() as now");
36+
37+
return {
38+
ok: true,
39+
configured: true,
40+
now: result.rows[0].now,
41+
};
42+
}

server/index.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import "dotenv/config";
12
import express from "express";
23
import path from "path";
34
import { fileURLToPath } from "url";
5+
import { checkDatabaseConnection } from "./db.js";
46

57
const __dirname = path.dirname(fileURLToPath(import.meta.url));
68
const app = express();
@@ -16,9 +18,23 @@ app.get("/api/health", (req, res) => {
1618
res.json({ ok: true, service: "stack-api" });
1719
});
1820

21+
app.get("/api/db/health", async (req, res) => {
22+
try {
23+
const health = await checkDatabaseConnection();
24+
res.status(health.ok ? 200 : 503).json(health);
25+
} catch (error) {
26+
console.error("Database health check failed:", error);
27+
res.status(500).json({
28+
ok: false,
29+
configured: true,
30+
message: "Database health check failed.",
31+
});
32+
}
33+
});
34+
1935
if (siteLockEnabled) {
2036
app.use((req, res, next) => {
21-
if (req.path === "/api/health") {
37+
if (req.path === "/api/health" || req.path === "/api/db/health") {
2238
next();
2339
return;
2440
}

server/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
"dev": "node index.js"
88
},
99
"dependencies": {
10-
"express": "^4.21.2"
10+
"dotenv": "^17.4.2",
11+
"express": "^4.21.2",
12+
"pg": "^8.20.0"
1113
}
1214
}

0 commit comments

Comments
 (0)