summaryrefslogtreecommitdiff
path: root/hooks/server/mod.ts
blob: b635b05c4001465c68a9f79b715d88863b810175 (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
import {
  getRequiredEnv,
  getStdout,
  type HealthCheckInput,
  HealthCheckOutput,
  type IEither,
  type ITraceable,
  LogTraceable,
  TraceUtil,
} from "@emprespresso/pengueno";

export class LizCIServer {
  private constructor(
    private readonly healthCheckActivity = HealthCheckActivity(healthCheck),
    private readonly jobHookActivity = JobHookActivity(jobQueuer),
    private readonly fourOhFourActivity = FourOhFourActivity(),
  ) {}

  private async route(req: LogTraceable<Request>) {
    return req.flatMap((req) => {
      const { item: request } = req;
      const url = new URL(request.url);
      if (url.pathname === "/health") {
        return this.healthCheckActivity.healthCheck(req);
      }
      if (url.pathname === "/job") {
        return this.jobHookActivity.processHook(req);
      }
    });
  }

  public async serve(req: Request): Promise<Response> {
    return LogTraceable(req).bimap(TraceUtil.withClassTrace(this)).map(
      this.route,
    );
  }
}