diff options
Diffstat (limited to 'u/server/activity')
-rw-r--r-- | u/server/activity/fourohfour.ts | 27 | ||||
-rw-r--r-- | u/server/activity/health.ts | 77 | ||||
-rw-r--r-- | u/server/activity/mod.ts | 20 |
3 files changed, 73 insertions, 51 deletions
diff --git a/u/server/activity/fourohfour.ts b/u/server/activity/fourohfour.ts index 48740df..6449abd 100644 --- a/u/server/activity/fourohfour.ts +++ b/u/server/activity/fourohfour.ts @@ -1,4 +1,5 @@ import { + type IActivity, type ITraceable, JsonResponse, type PenguenoRequest, @@ -16,12 +17,20 @@ const messages = [ "ヽ(;▽;)ノ Eep! This route has ghosted you~", ]; const randomFourOhFour = () => messages[Math.random() * messages.length]; -export const FourOhFourActivity = ( - req: ITraceable<PenguenoRequest, ServerTrace>, -) => - req - .move( - new JsonResponse(req, randomFourOhFour(), { - status: 404, - }), - ); + +export interface IFourOhFourActivity { + fourOhFour: IActivity; +} + +export class FourOhFourActivityImpl implements IFourOhFourActivity { + public fourOhFour( + req: ITraceable<PenguenoRequest, ServerTrace>, + ) { + return req + .move( + new JsonResponse(req, randomFourOhFour(), { status: 404 }), + ) + .map((resp) => Promise.resolve(resp.get())) + .get(); + } +} diff --git a/u/server/activity/health.ts b/u/server/activity/health.ts index b9efa3a..0f54a99 100644 --- a/u/server/activity/health.ts +++ b/u/server/activity/health.ts @@ -1,4 +1,5 @@ import { + type IActivity, type IEither, type ITraceable, JsonResponse, @@ -14,39 +15,53 @@ export enum HealthCheckInput { CHECK, } export enum HealthCheckOutput { - YAASQUEEN, + YAASSSLAYQUEEN, +} + +export interface IHealthCheckActivity { + checkHealth: IActivity; } const healthCheckMetric = Metric.fromName("Health"); -export const HealthCheckActivity = ( - check: Mapper< +export interface HealthChecker extends + Mapper< ITraceable<HealthCheckInput, ServerTrace>, Promise<IEither<Error, HealthCheckOutput>> - >, -) => -(req: ITraceable<PenguenoRequest, ServerTrace>) => - req - .bimap(TraceUtil.withFunctionTrace(HealthCheckActivity)) - .bimap(TraceUtil.withMetricTrace(healthCheckMetric)) - .flatMap((r) => r.move(HealthCheckInput.CHECK).map(check)) - .map(TraceUtil.promiseify((h) => { - const health = h.get(); - health.mapBoth((e) => { - h.trace.trace(healthCheckMetric.failure); - h.trace.addTrace(LogLevel.ERROR).trace(`${e}`); - return new JsonResponse( - req, - "oh no, i need to eat more vegetables (。•́︿•̀。)...", - { status: 500 }, - ); - }, (_healthy) => { - h.trace.trace(healthCheckMetric.success); - const msg = `think im healthy!! (✿˘◡˘) ready to do work~`; - h.trace.trace(msg); - return new JsonResponse( - req, - msg, - { status: 200 }, - ); - }); - })); + > {} +export class HealthCheckActivityImpl implements IHealthCheckActivity { + constructor( + private readonly check: HealthChecker, + ) {} + + public checkHealth(req: ITraceable<PenguenoRequest, ServerTrace>) { + return req + .bimap(TraceUtil.withFunctionTrace(this.checkHealth)) + .bimap(TraceUtil.withMetricTrace(healthCheckMetric)) + .flatMap((r) => r.move(HealthCheckInput.CHECK).map(this.check)) + .peek(TraceUtil.promiseify((h) => + h.get().fold((err) => { + if (err) { + h.trace.trace(healthCheckMetric.failure); + h.trace.addTrace(LogLevel.ERROR).trace(`${err}`); + return; + } + h.trace.trace(healthCheckMetric.success); + }) + )) + .map(TraceUtil.promiseify((h) => + h.get() + .mapBoth( + () => "oh no, i need to eat more vegetables (。•́︿•̀。)...", + () => "think im healthy!! (✿˘◡˘) ready to do work~", + ) + .fold((errMsg, okMsg) => + new JsonResponse( + req, + errMsg ?? okMsg, + { status: errMsg ? 500 : 200 }, + ) + ) + )) + .get(); + } +} diff --git a/u/server/activity/mod.ts b/u/server/activity/mod.ts index 9bd512f..82d8ec4 100644 --- a/u/server/activity/mod.ts +++ b/u/server/activity/mod.ts @@ -1,15 +1,13 @@ -import type { PenguenoResponse, RequestFilter } from "@emprespresso/pengueno"; +import type { + ITraceable, + PenguenoRequest, + PenguenoResponse, + ServerTrace, +} from "@emprespresso/pengueno"; -export enum StatusOK { - FOLLOW = 300, - OK = 200, -} -export interface ActivityOk { - readonly status: StatusOK; -} - -export interface IActivity<Trace> - extends RequestFilter<ActivityOk, Trace, PenguenoResponse> { +export interface IActivity { + (req: ITraceable<PenguenoRequest, ServerTrace>): Promise<PenguenoResponse>; } export * from "./health.ts"; +export * from "./fourohfour.ts"; |