33 lines
970 B
JavaScript
33 lines
970 B
JavaScript
const http = require("http");
|
|
|
|
const port = Number(process.env.API_INTERNAL_PORT || 3001);
|
|
const serviceName = process.env.SERVICE_NAME || "video-games-api";
|
|
|
|
function sendJson(response, statusCode, payload) {
|
|
response.writeHead(statusCode, { "Content-Type": "application/json; charset=utf-8" });
|
|
response.end(JSON.stringify(payload));
|
|
}
|
|
|
|
const server = http.createServer((request, response) => {
|
|
const url = new URL(request.url || "/", `http://${request.headers.host || "localhost"}`);
|
|
|
|
if (request.method === "GET" && url.pathname === "/health") {
|
|
sendJson(response, 200, {
|
|
status: "ok",
|
|
service: serviceName,
|
|
timestamp: new Date().toISOString(),
|
|
});
|
|
return;
|
|
}
|
|
|
|
sendJson(response, 404, {
|
|
status: "not_found",
|
|
message: "Route not found",
|
|
});
|
|
});
|
|
|
|
server.listen(port, "0.0.0.0", () => {
|
|
// Keep startup logs minimal and readable in docker compose logs.
|
|
console.log(`${serviceName} listening on port ${port}`);
|
|
});
|