Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/lib/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import pkg from "../../package.json" with { type: "json" };

export const version = `v${pkg.version}`;
13 changes: 11 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
import { Hono } from "hono";
import { extractClientIp } from "./utils/ip.js";
import { lookupIp, type LookupResult } from "./services/ipLookup.js";
import pkg from "../package.json" with { type: "json" };
import { version } from "./lib/version.js";

type LookupFn = (ip: string | null) => Promise<LookupResult | null>;

const attribution =
"IP2Region data provided by https://ip2region.net (Apache-2.0).";

const bootedAt = Date.now();

export function createApp(
lookup: LookupFn = lookupIp,
apiKey: string | undefined = process.env.ECHO_API_KEY,
) {
const app = new Hono();

app.get("/api/live", (c) => {
return c.json({ status: "ok", version: `v${pkg.version}` });
c.header("Cache-Control", "no-store");
return c.json({
status: "ok",
version,
component: "echo",
timestamp: new Date().toISOString(),
uptime: Math.round((Date.now() - bootedAt) / 1000),
});
});

app.get("/", (c) => {
Expand Down
13 changes: 10 additions & 3 deletions tests/unit/health.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import { describe, expect, test } from "bun:test";
import { createApp } from "../../src/server.js";
import pkg from "../../package.json" with { type: "json" };
import { version } from "../../src/lib/version.js";

const testApiKey = "test-secret-key";

describe("GET /api/live", () => {
test("returns ok status and version", async () => {
test("returns surety-standard health response", async () => {
const app = createApp();
const res = await app.request("/api/live");

expect(res.status).toBe(200);
expect(res.headers.get("cache-control")).toBe("no-store");

const body = await res.json();
expect(body).toEqual({ status: "ok", version: `v${pkg.version}` });
expect(body.status).toBe("ok");
expect(body.version).toBe(version);
expect(body.component).toBe("echo");
expect(typeof body.timestamp).toBe("string");
expect(new Date(body.timestamp).toISOString()).toBe(body.timestamp);
expect(typeof body.uptime).toBe("number");
expect(body.uptime).toBeGreaterThanOrEqual(0);
});
});

Expand Down
Loading