FincH12 commited on
Commit
36eefaf
·
verified ·
1 Parent(s): 4dfb1a2

fix: add PORT env var fallback for health server

Browse files
Files changed (1) hide show
  1. health-server.js +9 -62
health-server.js CHANGED
@@ -2,7 +2,7 @@ const http = require("http");
2
  const fs = require("fs");
3
  const net = require("net");
4
 
5
- const PORT = Number(process.env.PUBLIC_PORT || 7860);
6
  const TARGET_PORT = Number(process.env.N8N_PORT || 5678);
7
  const TARGET_HOST = "127.0.0.1";
8
  const SYNC_STATUS_FILE = "/tmp/hugging8n-sync-status.json";
@@ -64,20 +64,6 @@ function probeN8nHealth(timeoutMs = 1500) {
64
  });
65
  }
66
 
67
- let cachedN8nReady = false;
68
- function startN8nHealthCheck() {
69
- async function check() {
70
- try {
71
- cachedN8nReady = await probeN8nHealth(1000);
72
- } catch {
73
- cachedN8nReady = false;
74
- }
75
- setTimeout(check, 5000).unref();
76
- }
77
- check();
78
- }
79
- startN8nHealthCheck();
80
-
81
  function escapeHtml(value) {
82
  return String(value)
83
  .replace(/&/g, "&")
@@ -247,16 +233,15 @@ function renderDashboard(data) {
247
  const server = http.createServer(async (req, res) => {
248
  const url = parseRequestUrl(req.url);
249
  const pathname = url.pathname;
250
- console.log(`[health-server] Request: ${req.method} ${req.url} (User-Agent: ${req.headers["user-agent"] || "none"})`);
251
 
252
  // 1. Dashboard Routes
253
  if (pathname === "/health") {
254
- console.log(`[health-server] Responding to /health with 200 OK`);
255
- res.writeHead(200, { "Content-Type": "application/json" });
256
  return res.end(
257
  JSON.stringify({
258
- status: "ok",
259
- n8nReady: true,
260
  ...getStatus(),
261
  keepalive: getKeepaliveStatus(),
262
  }),
@@ -264,12 +249,12 @@ const server = http.createServer(async (req, res) => {
264
  }
265
  if (pathname === "/status") {
266
  const uptime = Math.floor((Date.now() - startTime) / 1000);
267
- console.log(`[health-server] Responding to /status with 200 OK`);
268
  res.writeHead(200, { "Content-Type": "application/json" });
269
  return res.end(
270
  JSON.stringify({
271
  uptime: `${Math.floor(uptime / 3600)}h ${Math.floor((uptime % 3600) / 60)}m`,
272
- n8nReady: cachedN8nReady,
273
  sync: getStatus(),
274
  keepalive: getKeepaliveStatus(),
275
  }),
@@ -277,57 +262,19 @@ const server = http.createServer(async (req, res) => {
277
  }
278
  if (pathname === "/" || pathname === "/dashboard") {
279
  const uptime = Math.floor((Date.now() - startTime) / 1000);
280
- console.log(`[health-server] Responding to / with 200 OK`);
281
  res.writeHead(200, { "Content-Type": "text/html" });
282
  return res.end(
283
  renderDashboard({
284
  uptimeHuman: `${Math.floor(uptime / 3600)}h ${Math.floor((uptime % 3600) / 60)}m`,
285
- n8nReady: cachedN8nReady,
286
  sync: getStatus(),
287
  keepalive: getKeepaliveStatus(),
288
  }),
289
  );
290
  }
291
 
292
- if (pathname === "/test-telegram") {
293
- const https = require("https");
294
- const reqTelegram = https.get(
295
- "https://api.telegram.org/bot8996470324:AAHgRhFRW9w4eroBd0sYL5fFpfe7Laj3q9s/getMe",
296
- (resTelegram) => {
297
- let data = "";
298
- resTelegram.on("data", (chunk) => data += chunk);
299
- resTelegram.on("end", () => {
300
- res.writeHead(200, { "Content-Type": "application/json" });
301
- res.end(JSON.stringify({
302
- statusCode: resTelegram.statusCode,
303
- headers: resTelegram.headers,
304
- body: data
305
- }));
306
- });
307
- }
308
- );
309
- reqTelegram.on("error", (errTelegram) => {
310
- res.writeHead(500, { "Content-Type": "application/json" });
311
- res.end(JSON.stringify({ error: errTelegram.message }));
312
- });
313
- return;
314
- }
315
-
316
- if (pathname === "/test-db") {
317
- const { exec } = require("child_process");
318
- exec("sqlite3 /home/node/.n8n/database.sqlite \"SELECT id, name, type FROM credentials_entity;\"", (err, stdout, stderr) => {
319
- res.writeHead(200, { "Content-Type": "application/json" });
320
- res.end(JSON.stringify({
321
- error: err ? err.message : null,
322
- stdout: stdout.trim(),
323
- stderr: stderr.trim()
324
- }));
325
- });
326
- return;
327
- }
328
-
329
  // 2. n8n Proxy Logic
330
- console.log(`[health-server] Proxying to n8n: ${req.method} ${pathname}`);
331
  const proxyHeaders = {
332
  ...req.headers,
333
  host: `127.0.0.1:${TARGET_PORT}`,
 
2
  const fs = require("fs");
3
  const net = require("net");
4
 
5
+ const PORT = Number(process.env.PUBLIC_PORT || process.env.PORT || 7861);
6
  const TARGET_PORT = Number(process.env.N8N_PORT || 5678);
7
  const TARGET_HOST = "127.0.0.1";
8
  const SYNC_STATUS_FILE = "/tmp/hugging8n-sync-status.json";
 
64
  });
65
  }
66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  function escapeHtml(value) {
68
  return String(value)
69
  .replace(/&/g, "&")
 
233
  const server = http.createServer(async (req, res) => {
234
  const url = parseRequestUrl(req.url);
235
  const pathname = url.pathname;
 
236
 
237
  // 1. Dashboard Routes
238
  if (pathname === "/health") {
239
+ const n8nReady = await probeN8nHealth();
240
+ res.writeHead(n8nReady ? 200 : 503, { "Content-Type": "application/json" });
241
  return res.end(
242
  JSON.stringify({
243
+ status: n8nReady ? "ok" : "degraded",
244
+ n8nReady,
245
  ...getStatus(),
246
  keepalive: getKeepaliveStatus(),
247
  }),
 
249
  }
250
  if (pathname === "/status") {
251
  const uptime = Math.floor((Date.now() - startTime) / 1000);
252
+ const n8nReady = await probeN8nHealth();
253
  res.writeHead(200, { "Content-Type": "application/json" });
254
  return res.end(
255
  JSON.stringify({
256
  uptime: `${Math.floor(uptime / 3600)}h ${Math.floor((uptime % 3600) / 60)}m`,
257
+ n8nReady,
258
  sync: getStatus(),
259
  keepalive: getKeepaliveStatus(),
260
  }),
 
262
  }
263
  if (pathname === "/" || pathname === "/dashboard") {
264
  const uptime = Math.floor((Date.now() - startTime) / 1000);
265
+ const n8nReady = await probeN8nHealth();
266
  res.writeHead(200, { "Content-Type": "text/html" });
267
  return res.end(
268
  renderDashboard({
269
  uptimeHuman: `${Math.floor(uptime / 3600)}h ${Math.floor((uptime % 3600) / 60)}m`,
270
+ n8nReady,
271
  sync: getStatus(),
272
  keepalive: getKeepaliveStatus(),
273
  }),
274
  );
275
  }
276
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
277
  // 2. n8n Proxy Logic
 
278
  const proxyHeaders = {
279
  ...req.headers,
280
  host: `127.0.0.1:${TARGET_PORT}`,