generated from nhcarrigan/template
31 lines
724 B
TypeScript
31 lines
724 B
TypeScript
import type { FastifyInstance } from "fastify";
|
|
import { readFileSync } from "fs";
|
|
import { join } from "path";
|
|
|
|
interface PackageJson {
|
|
version: string;
|
|
}
|
|
|
|
let cachedVersion: string | null = null;
|
|
|
|
function getVersion(): string {
|
|
if (cachedVersion) {
|
|
return cachedVersion;
|
|
}
|
|
|
|
try {
|
|
const packageJsonPath = join(process.cwd(), "package.json");
|
|
const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf-8")) as PackageJson;
|
|
cachedVersion = packageJson.version;
|
|
return cachedVersion;
|
|
} catch {
|
|
return "unknown";
|
|
}
|
|
}
|
|
|
|
export default async function (app: FastifyInstance): Promise<void> {
|
|
app.get("/", async (_request, reply) => {
|
|
reply.send({ version: getVersion() });
|
|
});
|
|
}
|