summaryrefslogtreecommitdiff
path: root/hooks/server/ci.ts
blob: cdb837265866b9a289adac94635e91a2fd9dffb4 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import {
  FourOhFourActivityImpl,
  getRequiredEnv,
  HealthCheckActivityImpl,
  type HealthChecker,
  type IFourOhFourActivity,
  type IHealthCheckActivity,
  type ITraceable,
  PenguenoRequest,
  type ServerTrace,
  TraceUtil,
} from "@emprespresso/pengueno";
import type { Job } from "@emprespresso/ci-model";
import {
  healthCheck as _healthCheck,
  type IJobHookActivity,
  type IJobQueuer,
  JobHookActivityImpl,
  LaminarJobQueuer,
} from "@emprespresso/ci-hooks";

export class LizCIServer {
  constructor(
    healthCheck: HealthChecker = _healthCheck,
    jobQueuer: IJobQueuer<ITraceable<Job, ServerTrace>> = new LaminarJobQueuer(
      getRequiredEnv("LAMINAR_URL").fold((err, val) =>
        err ? "https://ci.liz.coffee" : val
      ),
    ),
    private readonly healthCheckActivity: IHealthCheckActivity =
      new HealthCheckActivityImpl(healthCheck),
    private readonly jobHookActivity: IJobHookActivity =
      new JobHookActivityImpl(jobQueuer),
    private readonly fourOhFourActivity: IFourOhFourActivity =
      new FourOhFourActivityImpl(),
  ) {}

  private route(req: ITraceable<PenguenoRequest, ServerTrace>) {
    const url = new URL(req.get().url);
    if (url.pathname === "/health") {
      return this.healthCheckActivity.checkHealth(req);
    }
    if (url.pathname === "/job") {
      return this.jobHookActivity.processHook(req);
    }
    return this.fourOhFourActivity.fourOhFour(req);
  }

  public serve(req: Request): Promise<Response> {
    return PenguenoRequest.from(req)
      .bimap(TraceUtil.withClassTrace(this))
      .map(this.route)
      .get();
  }
}