summaryrefslogtreecommitdiff
path: root/hooks/server/mod.ts
blob: c484af4b7f866f4c761f7202fb74d99d66f61b6d (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
import { Either, type ITraceable } from "@emprespresso/pengueno";

const healthCheck = async <Trace>(in: ITraceable<"healthy?", Trace>) => {
  return getRequiredEnv("LAMINAR_HOST").flatMap((_host) => Either.fromFailableAsync(
    getStdout(in.move(["laminarc", "show-jobs"]))
  ))
}

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

  private async route(req: ITraceable<req: Request, LogTraceable>) {
    return req.flatMap((req) => {
      const { logger, item: { method, pathname } } = req;
      if (pathname === "/health") {
        return this.healthCheckActivity.healthCheck(req);
      }
      return this.jobHookActivity.processHook(req);
    });
  }

  public async serve(req: Request): Promise<Response> {
    return LogTraceable(req).bimap(TraceUtil.withClassTrace(this)).map(this.route)
  }
}
private route(
  req: Traceable<Request & { pathname: string }>,
): Traceable<Promise<Response>> {
  return req.flatMap((req) => {
    const { logger, item: { method, pathname } } = req;
    if (pathname === "/health") {
      return this.healthCheckActivity.healthCheck(req);
    }
    return this.jobHookActivity.processHook(req);
  });
}