diff --git a/Dockerfile b/Dockerfile index a9d4a0a..b65c5c8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -45,3 +45,5 @@ WORKDIR /app USER appuser ENTRYPOINT ["/tini", "--", "/app/invidious_companion"] + +HEALTHCHECK --interval=5s --timeout=5s --start-period=10s --retries=3 CMD ["/tini", "--", "/app/invidious_companion", "healthcheck"] diff --git a/src/main.ts b/src/main.ts index 2b58465..9ff9f73 100644 --- a/src/main.ts +++ b/src/main.ts @@ -14,10 +14,28 @@ if (Deno.env.get("GET_FETCH_CLIENT_LOCATION")) { ) as string; } } + +const args = Deno.args; +const konfigStore = await konfigLoader(); +const host = Deno.env.get("HOST") || konfigStore.get("server.host") as string; +const port = Deno.env.get("PORT") || konfigStore.get("server.port") as string; + +if (args?.[0] == "healthcheck") { + try { + const response = await fetch(`http://${host}:${port}/healthz`); + if (response.status === 200) { + Deno.exit(0); + } else { + Deno.exit(1); + } + } catch (_) { + Deno.exit(1); + } +} + const { getFetchClient } = await import(getFetchClientLocation); const app = new Hono(); -const konfigStore = await konfigLoader(); let innertubeClient: Innertube; let innertubeClientFetchPlayer = true; @@ -116,7 +134,6 @@ app.use("*", async (c, next) => { routes(app, konfigStore); Deno.serve({ - port: Number(Deno.env.get("PORT")) || - konfigStore.get("server.port") as number, - hostname: Deno.env.get("HOST") || konfigStore.get("server.host") as string, + port: Number(port), + hostname: host, }, app.fetch);