summaryrefslogtreecommitdiff
path: root/u/server/activity/health.ts
blob: 7ee66292cb078e1d6823b8face96b8f76b19c256 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import {
  type IEither,
  type ITraceable,
  LogLevel,
  type Mapper,
  TraceUtil,
} from "@emprespresso/pengueno";

export enum HealthCheckInput {
  CHECK,
}
export enum HealthCheckOutput {
  YAASQUEEN,
}

export const HealthCheckActivity = <Trace>(
  check: Mapper<
    ITraceable<HealthCheckInput, Trace>,
    Promise<IEither<Error, HealthCheckOutput>>
  >,
) =>
(req: ITraceable<Request, Trace>) =>
  req.bimap(TraceUtil.withFunctionTrace(HealthCheckActivity))
    .flatMap((r) => r.move(HealthCheckInput.CHECK))
    .map(check)
    .map(TraceUtil.promiseify(({ item: health, trace }) => {
      health.mapBoth((e) => {
        trace.addTrace(LogLevel.ERROR).trace(`${e}`);
        return new Response(
          "oh no, i need to eat more vegetables (。•́︿•̀。)...\n",
          { status: 500 },
        );
      }, (_healthy) => {
        const msg = `think im healthy!! (✿˘◡˘) ready to do work~`;
        trace.trace(msg);
        return new Response(
          msg + "\n",
          { status: 200 },
        );
      });
    }));