Skip to content

Commit

Permalink
added version endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
argl committed Dec 17, 2024
1 parent 8e2a31b commit dba4b64
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ USER node
COPY --chown=node:node . .
RUN npm install

ARG GIT_SHA=dev
ARG RUN_ID=unknown
# Get the current HSTS list
RUN npm run updateHsts

RUN env

ENV RUN_ID=${RUN_ID}
ENV GIT_SHA=${GIT_SHA}
ENV NODE_EXTRA_CA_CERTS=node_modules/extra_certs/ca_bundle/ca_intermediate_bundle.pem
EXPOSE 8080
CMD [ "node", "src/api/index.js" ]
2 changes: 2 additions & 0 deletions src/api/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import analyzeApiV2 from "./v2/analyze/index.js";
import scanApiV2 from "./v2/scan/index.js";
import statsApiV2 from "./v2/stats/index.js";
import recommendationMatrixApiV2 from "./v2/recommendations/index.js";
import version from "./v2/version/index.js";
import globalErrorHandler from "./global-error-handler.js";
import pool from "@fastify/postgres";
import { poolOptions } from "../database/repository.js";
Expand Down Expand Up @@ -76,6 +77,7 @@ export async function createServer() {
server.register(scanApiV2, { prefix: "/api/v2" }),
server.register(statsApiV2, { prefix: "/api/v2" }),
server.register(recommendationMatrixApiV2, { prefix: "/api/v2" }),
server.register(version),
]);

["SIGINT", "SIGTERM"].forEach((signal) => {
Expand Down
19 changes: 18 additions & 1 deletion src/api/v2/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ const gradeDistributionResponse = {
},
};

const versionResponse = {
type: "object",
properties: {
commit: { type: "string" },
version: { type: "string" },
source: { type: "string" },
build: { type: "string" },
},
required: ["commit", "version", "source", "build"],
};

const recommendationMatrixResponse = {
type: "array",
items: {
Expand Down Expand Up @@ -200,6 +211,12 @@ export const SCHEMAS = {
200: recommendationMatrixResponse,
},
},

version: {
response: {
200: versionResponse,
},
},
};

/**
Expand Down Expand Up @@ -318,4 +335,4 @@ export class PolicyResponse {
}

// import jsdoc from "json-schema-to-jsdoc";
// console.log(jsdoc(SCHEMAS.gradeDistribution.response["200"]));
// console.log(jsdoc(SCHEMAS.version.response["200"]));
40 changes: 40 additions & 0 deletions src/api/v2/version/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { version } from "tough-cookie";
import { SCHEMAS } from "../schemas.js";
import fs from "node:fs";
import path from "path";
import { fileURLToPath } from "url";

// Get the directory name of the current module
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const packageJson = JSON.parse(
fs.readFileSync(
path.join(__dirname, "..", "..", "..", "..", "package.json"),
"utf8"
)
);

/**
* Register the API - default export
* @param {import('fastify').FastifyInstance} fastify
* @returns {Promise<void>}
*/
export default async function (fastify) {
const pool = fastify.pg.pool;

fastify.get(
"/__version__",
{ schema: SCHEMAS.version },
async (request, reply) => {
/** @type {import("../../../types.js").VersionResponse} */
const ret = {
version: packageJson.version,
commit: process.env.GIT_SHA || "unknown",
source: "https://github.com/mdn/mdn-http-observatory",
build: process.env.RUN_ID || "unknown",
};
return ret;
}
);
}
8 changes: 8 additions & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,11 @@ export class Policy {
* @property {string} grade
* @property {number} count
*/

/**
* @typedef {object} VersionResponse
* @property {string} commit
* @property {string} version
* @property {string} source
* @property {string} build
*/
18 changes: 18 additions & 0 deletions test/apiv2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "../src/database/repository.js";
import { EventEmitter } from "events";
import { NUM_TESTS } from "../src/constants.js";
import fs from "node:fs";

const pool = createPool();
EventEmitter.defaultMaxListeners = 20;
Expand All @@ -25,6 +26,23 @@ describeOrSkip("API V2", function () {
await migrateDatabase("max", pool);
});

it("serves the version path", async function () {
process.env.RUN_ID = "buildinfo";
process.env.GIT_SHA = "commitinfo";
const app = await createServer();
const response = await app.inject({
method: "GET",
url: "/__version__",
});
assert.equal(response.statusCode, 200);
const j = response.json();
const p = JSON.parse(fs.readFileSync("package.json", "utf8"));
assert.equal(j.version, p.version);
assert.equal(j.commit, "commitinfo");
assert.equal(j.build, "buildinfo");
assert.equal(j.source, "https://github.com/mdn/mdn-http-observatory");
});

it("serves the root path with a greeting", async function () {
const app = await createServer();
const response = await app.inject({
Expand Down

0 comments on commit dba4b64

Please sign in to comment.