Initial stage
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
const mariadb = require("mariadb");
|
||||
|
||||
const pool = mariadb.createPool({
|
||||
host: process.env.DB_HOST || "db",
|
||||
port: Number(process.env.DB_PORT || 3306),
|
||||
user: process.env.DB_USER || "app_user",
|
||||
password: process.env.DB_PASSWORD || "app_password",
|
||||
database: process.env.DB_NAME || "app_db",
|
||||
connectionLimit: 5
|
||||
});
|
||||
|
||||
async function executeQuery(sql, params = []) {
|
||||
let connection;
|
||||
|
||||
try {
|
||||
connection = await pool.getConnection();
|
||||
return await connection.query(sql, params);
|
||||
} finally {
|
||||
if (connection) {
|
||||
connection.release();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function closePool() {
|
||||
await pool.end();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
closePool,
|
||||
executeQuery
|
||||
};
|
||||
@@ -0,0 +1,73 @@
|
||||
const express = require("express");
|
||||
const { closePool, executeQuery } = require("./db");
|
||||
|
||||
const app = express();
|
||||
const port = Number(process.env.APP_PORT || 3000);
|
||||
|
||||
app.get("/", (_request, response) => {
|
||||
response.json({
|
||||
service: "clproject-env-test",
|
||||
status: "running",
|
||||
endpoints: ["/health", "/db/check"]
|
||||
});
|
||||
});
|
||||
|
||||
app.get("/health", async (_request, response) => {
|
||||
try {
|
||||
const rows = await executeQuery("SELECT 1 AS connection_ok");
|
||||
|
||||
response.json({
|
||||
status: "ok",
|
||||
app: "reachable",
|
||||
database: "reachable",
|
||||
probe: rows[0].connection_ok,
|
||||
timestamp: new Date().toISOString()
|
||||
});
|
||||
} catch (error) {
|
||||
response.status(503).json({
|
||||
status: "degraded",
|
||||
app: "reachable",
|
||||
database: "unreachable",
|
||||
error: error.message,
|
||||
timestamp: new Date().toISOString()
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
app.get("/db/check", async (_request, response) => {
|
||||
try {
|
||||
const rows = await executeQuery(
|
||||
"SELECT id, name, created_at FROM environment_checks ORDER BY id"
|
||||
);
|
||||
|
||||
response.json({
|
||||
status: "ok",
|
||||
records: rows
|
||||
});
|
||||
} catch (error) {
|
||||
response.status(503).json({
|
||||
status: "error",
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
const server = app.listen(port, () => {
|
||||
console.log(`Server listening on port ${port}`);
|
||||
});
|
||||
|
||||
async function shutdown(signal) {
|
||||
console.log(`Received ${signal}, shutting down`);
|
||||
server.close(async () => {
|
||||
await closePool();
|
||||
process.exit(0);
|
||||
});
|
||||
}
|
||||
|
||||
process.on("SIGINT", () => {
|
||||
shutdown("SIGINT");
|
||||
});
|
||||
|
||||
process.on("SIGTERM", () => {
|
||||
shutdown("SIGTERM");
|
||||
});
|
||||
Reference in New Issue
Block a user