diff options
Diffstat (limited to 'lib/server/activity')
-rw-r--r-- | lib/server/activity/fourohfour.ts | 28 | ||||
-rw-r--r-- | lib/server/activity/health.ts | 49 | ||||
-rw-r--r-- | lib/server/activity/index.ts | 8 |
3 files changed, 85 insertions, 0 deletions
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<PenguenoRequest, ServerTrace>) { + 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<ITraceable<HealthCheckInput, ServerTrace>, Promise<IEither<Error, HealthCheckOutput>>> {} +export class HealthCheckActivityImpl implements IHealthCheckActivity { + constructor(private readonly check: HealthChecker) {} + + public checkHealth(req: ITraceable<PenguenoRequest, ServerTrace>) { + 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<PenguenoRequest, ServerTrace>): Promise<PenguenoResponse>; +} + +export * from './health'; +export * from './fourohfour'; |