From 9970036d203ba2d0a46b35ba6fad21d49441cdd4 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Sun, 27 Jul 2025 17:03:10 -0700 Subject: hai --- lib/server/activity/fourohfour.ts | 28 ++++++++++++++++++++++ lib/server/activity/health.ts | 49 +++++++++++++++++++++++++++++++++++++++ lib/server/activity/index.ts | 8 +++++++ 3 files changed, 85 insertions(+) create mode 100644 lib/server/activity/fourohfour.ts create mode 100644 lib/server/activity/health.ts create mode 100644 lib/server/activity/index.ts (limited to 'lib/server/activity') diff --git a/lib/server/activity/fourohfour.ts b/lib/server/activity/fourohfour.ts new file mode 100644 index 0000000..cd90ba0 --- /dev/null +++ b/lib/server/activity/fourohfour.ts @@ -0,0 +1,28 @@ +import { + type IActivity, + type ITraceable, + JsonResponse, + type PenguenoRequest, + type ServerTrace, +} from '@emprespresso/pengueno'; + +const messages = [ + 'D: meow-t found! your api call ran away!', + '404-bidden! but like...in a cute way >:3 !', + ':< your data went on a paw-sible vacation!', + 'uwu~ not found, but found our hearts instead!', +]; +const randomFourOhFour = () => messages[Math.floor(Math.random() * messages.length)]!; + +export interface IFourOhFourActivity { + fourOhFour: IActivity; +} + +export class FourOhFourActivityImpl implements IFourOhFourActivity { + public fourOhFour(req: ITraceable) { + return req + .move(new JsonResponse(req, randomFourOhFour(), { status: 404 })) + .map((resp) => Promise.resolve(resp.get())) + .get(); + } +} diff --git a/lib/server/activity/health.ts b/lib/server/activity/health.ts new file mode 100644 index 0000000..9396490 --- /dev/null +++ b/lib/server/activity/health.ts @@ -0,0 +1,49 @@ +import { + type IActivity, + type IEither, + IMetric, + type ITraceable, + JsonResponse, + LogLevel, + type Mapper, + Metric, + type PenguenoRequest, + type ServerTrace, + TraceUtil, +} from '@emprespresso/pengueno'; + +export enum HealthCheckInput { + CHECK, +} +export enum HealthCheckOutput { + YAASSSLAYQUEEN, +} + +export interface IHealthCheckActivity { + checkHealth: IActivity; +} + +const healthCheckMetric = Metric.fromName('Health').asResult(); +export interface HealthChecker + extends Mapper, Promise>> {} +export class HealthCheckActivityImpl implements IHealthCheckActivity { + constructor(private readonly check: HealthChecker) {} + + public checkHealth(req: ITraceable) { + return req + .flatMap(TraceUtil.withFunctionTrace(this.checkHealth)) + .flatMap(TraceUtil.withMetricTrace(healthCheckMetric)) + .flatMap((r) => r.move(HealthCheckInput.CHECK).map((input) => this.check(input))) + .peek(TraceUtil.promiseify(TraceUtil.traceResultingEither(healthCheckMetric))) + .map( + TraceUtil.promiseify((h) => { + const { status, message } = h.get().fold( + () => ({ status: 500, message: 'err' }), + () => ({ status: 200, message: 'ok' }), + ); + return new JsonResponse(req, message, { status }); + }), + ) + .get(); + } +} diff --git a/lib/server/activity/index.ts b/lib/server/activity/index.ts new file mode 100644 index 0000000..fc7c990 --- /dev/null +++ b/lib/server/activity/index.ts @@ -0,0 +1,8 @@ +import type { ITraceable, PenguenoRequest, PenguenoResponse, ServerTrace } from '@emprespresso/pengueno'; + +export interface IActivity { + (req: ITraceable): Promise; +} + +export * from './health'; +export * from './fourohfour'; -- cgit v1.2.3-70-g09d2