Skip to content

Commit

Permalink
#7 Also include application version in response headers
Browse files Browse the repository at this point in the history
  • Loading branch information
danielemery committed Jul 31, 2024
1 parent a573b75 commit 03c8a8e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
14 changes: 9 additions & 5 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ if (environment.SENTRY_DSN) {
});
}

start(environment.PORT, {
filterIncludesKey,
parseParameters,
keys,
});
start(
environment.PORT,
{
filterIncludesKey,
parseParameters,
keys,
},
environment.KEYS_VERSION,
);
7 changes: 5 additions & 2 deletions src/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Deno.test(
const response = await handleRequest(
new Request(`${TEST_URL}/not_found`),
emptyDependencies,
"unit_tests",
);

assertEquals(response.status, 404);
Expand All @@ -51,6 +52,7 @@ Deno.test("handleRequest: must return pgp key for /pgp", async () => {
const response = await handleRequest(
new Request(`${TEST_URL}/pgp`),
emptyDependencies,
"unit_tests",
);

assertEquals(response.status, 200);
Expand All @@ -67,6 +69,7 @@ Deno.test(
const response = await handleRequest(
new Request(`${TEST_URL}/pgp/daniel_emery.pub.asc`),
emptyDependencies,
"unit_tests",
);

assertEquals(response.status, 200);
Expand Down Expand Up @@ -94,7 +97,7 @@ Deno.test(
parseParameters: parseParametersSpy,
filterIncludesKey: filterIncludesKeySpy,
keys: fakeKeys,
});
}, "unit_tests");

assertSpyCalls(parseParametersSpy, 1);
assertSpyCall(parseParametersSpy, 0, {
Expand Down Expand Up @@ -127,7 +130,7 @@ Deno.test(
parseParameters: parseParametersStub,
filterIncludesKey: filterIncludesKeyStub,
keys: fakeKeys,
});
}, "unit_tests");

assertSpyCalls(parseParametersStub, 1);
assertSpyCall(parseParametersStub, 0, {
Expand Down
17 changes: 14 additions & 3 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,23 @@ export interface ServerDependencies {
* parameters.
* @param port The port to listen on.
*/
export default function start(port: number, dependencies: ServerDependencies) {
export default function start(
port: number,
dependencies: ServerDependencies,
version: string,
) {
console.log(`Server listening at :${port}/api`);
Deno.serve({
port,
handler: (req) => handleRequest(req, dependencies),
handler: (req) => handleRequest(req, dependencies, version),
});
}

export function handleRequest(req: Request, dependencies: ServerDependencies) {
export function handleRequest(
req: Request,
dependencies: ServerDependencies,
version: string,
) {
const { filterIncludesKey, parseParameters, keys } = dependencies;
try {
const url = new URL(req.url);
Expand Down Expand Up @@ -72,6 +80,9 @@ export function handleRequest(req: Request, dependencies: ServerDependencies) {
return new Response(responseData, {
status: STATUS_CODE.OK,
statusText: STATUS_TEXT[STATUS_CODE.OK],
headers: {
"X-Keys-Version": version,
},
});
} catch (err) {
console.error(err);
Expand Down

0 comments on commit 03c8a8e

Please sign in to comment.