diff options
author | Elizabeth Hunt <me@liz.coffee> | 2025-06-29 17:31:30 -0700 |
---|---|---|
committer | Elizabeth Hunt <me@liz.coffee> | 2025-06-29 17:31:30 -0700 |
commit | 58be1809c46cbe517a18d86d0af52179dcc5cbf6 (patch) | |
tree | 9ccc678b3fd48c1a52fe501600dd2c2051740a55 /server/hono_proxy.ts | |
parent | d4791f3d357634daf506fb8f91cc5332a794c421 (diff) | |
download | ci-58be1809c46cbe517a18d86d0af52179dcc5cbf6.tar.gz ci-58be1809c46cbe517a18d86d0af52179dcc5cbf6.zip |
Move to nodejs and also lots of significant refactoring that should've been broken up but idgaf
Diffstat (limited to 'server/hono_proxy.ts')
-rw-r--r-- | server/hono_proxy.ts | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/server/hono_proxy.ts b/server/hono_proxy.ts new file mode 100644 index 0000000..f729819 --- /dev/null +++ b/server/hono_proxy.ts @@ -0,0 +1,71 @@ +import { + BaseRequest, + Either, + IEither, + LogMetricTraceable, + Metric, + PenguenoRequest, + Server, + Signals, + TraceUtil, +} from '@emprespresso/pengueno'; + +import { serve, ServerType } from '@hono/node-server'; +import { Hono } from 'hono'; + +const AppLifetimeMetric = Metric.fromName('HonoAppLifetime').asResult(); +const AppRequestMetric = Metric.fromName('HonoAppRequest'); + +export class HonoProxy { + private readonly app = LogMetricTraceable.of(new Hono()) + .flatMap(TraceUtil.withTrace(`AppId = ${crypto.randomUUID()}`)) + .flatMap(TraceUtil.withMetricTrace(AppLifetimeMetric)); + + constructor(private readonly server: Server) {} + + public async serve(port: number, hostname: string): Promise<IEither<Error, void>> { + return this.app + .map((tApp) => + Either.fromFailable<Error, ServerType>(() => { + const app = tApp.get(); + app.all('*', async (c) => + tApp + .flatMap(TraceUtil.withMetricTrace(AppRequestMetric)) + .move(<BaseRequest>c.req) + .flatMap((tRequest) => PenguenoRequest.from(tRequest)) + .map((req) => this.server.serve(req)) + .map( + TraceUtil.promiseify((tResponse) => { + tResponse.trace.trace(AppRequestMetric.count.withValue(1.0)); + return new Response(tResponse.get().body(), tResponse.get()); + }), + ) + .get(), + ); + return serve({ + fetch: (_r) => app.fetch(_r), + port, + hostname, + }); + }), + ) + .peek(TraceUtil.traceResultingEither()) + .peek((tServe) => + tServe + .get() + .mapRight(() => + tServe.trace.trace( + `haii im still listening at http://${hostname}:${port} ~uwu dont think i forgot`, + ), + ), + ) + .map((tEitherServer) => + tEitherServer + .get() + .mapRight((server) => tEitherServer.move(server)) + .flatMapAsync((tServer) => Signals.awaitClose(tServer)), + ) + .peek(TraceUtil.promiseify(TraceUtil.traceResultingEither(AppLifetimeMetric))) + .get(); + } +} |