Skip to content

Commit

Permalink
Merge pull request #18 from pagopa/add_healthcheck
Browse files Browse the repository at this point in the history
Add Healthcheck endpoint
  • Loading branch information
AleDore authored May 19, 2021
2 parents cdac9a5 + 70b3c6f commit 70f8d99
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 27 deletions.
6 changes: 2 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,8 @@ services:
- spid-net

storage:
image: azurite
build:
context: ./
dockerfile: docker/azurite/Dockerfile
container_name: storage
image: mcr.microsoft.com/azure-storage/azurite
ports:
- "10000:10000"
- "10001:10001"
Expand Down
23 changes: 0 additions & 23 deletions docker/azurite/Dockerfile

This file was deleted.

3 changes: 3 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
EntityType
} from "@pagopa/io-spid-commons/dist/utils/middleware";
import { AdeAPIClient } from "./clients/ade";
import { healthcheckHandler } from "./handlers/general";
import { logger } from "./utils/logger";
import { REDIS_CLIENT } from "./utils/redis";

Expand Down Expand Up @@ -255,6 +256,8 @@ export const createAppTask = withSpid({
});
});

withSpidApp.get("/healthcheck", healthcheckHandler(redisClient));

withSpidApp.post("/introspect", introspectHandler);

withSpidApp.post("/invalidate", invalidateHandler);
Expand Down
42 changes: 42 additions & 0 deletions src/handlers/general.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import * as express from "express";
import { toError } from "fp-ts/lib/Either";
import {
fromEither,
fromLeft,
taskEither,
tryCatch
} from "fp-ts/lib/TaskEither";
import { RedisClient } from "redis";
import { AdeAPIClient } from "../clients/ade";
import { getConfigOrThrow } from "../utils/config";
import { errorsToError } from "../utils/conversions";
import { pingTask } from "../utils/redis_storage";

const config = getConfigOrThrow();

export const healthcheckHandler = (redisClient: RedisClient) => (
_: express.Request,
res: express.Response
) =>
// first ping for redis
pingTask(redisClient)
.chain(() =>
// if Attribute Authority is enabled check for service is up&running
config.ENABLE_ADE_AA
? tryCatch(
() => AdeAPIClient(config.ADE_AA_API_ENDPOINT).ping({}),
toError
)
.chain(__ => fromEither(__).mapLeft(errs => errorsToError(errs)))
.chain(response =>
response.status === 200
? taskEither.of(true)
: fromLeft(new Error(response.value.detail))
)
: taskEither.of(true)
)
.fold(
err => res.status(500).json(err.message),
() => res.status(200).json("OK")
)
.run();
33 changes: 33 additions & 0 deletions src/utils/redis_storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@ import { fromNullable, Option } from "fp-ts/lib/Option";
import { fromEither, TaskEither, tryCatch } from "fp-ts/lib/TaskEither";
import { RedisClient } from "redis";

/**
* Parse a Redis given string reply.
*
* @see https://redis.io/topics/protocol#simple-string-reply.
*/
export const givenStringReply = (
err: Error | null,
reply: string | undefined,
message: string
): Either<Error, boolean> => {
if (err) {
return left<Error, boolean>(err);
}

return right<Error, boolean>(reply === message);
};

/**
* Parse a Redis single string reply.
*
Expand Down Expand Up @@ -162,3 +179,19 @@ export const existsKeyTask = (
),
toError
).chain(fromEither);

export const pingTask = (redisClient: RedisClient): TaskEither<Error, true> =>
tryCatch(
() =>
new Promise<Either<Error, true>>(resolve =>
redisClient.ping("ping message", (err, response) =>
resolve(
falsyResponseToError(
givenStringReply(err, response, "ping message"),
new Error("Error while pinging redis")
)
)
)
),
toError
).chain(fromEither);

0 comments on commit 70f8d99

Please sign in to comment.