| 1 | import * as Fly from "alchemy/Fly"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 4 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 5 | import { API_PORT, Cache } from "./shared.ts"; |
| 6 | |
| 7 | /** |
| 8 | * HTTP Service that PINGs Upstash Redis via {@link Fly.ReadRedis}. |
| 9 | */ |
| 10 | export default class Api extends Fly.Service<Api>()( |
| 11 | "Api", |
| 12 | { |
| 13 | main: import.meta.url, |
| 14 | region: "iad", |
| 15 | port: API_PORT, |
| 16 | guest: { cpuKind: "shared", cpus: 1, memoryMb: 256 }, |
| 17 | }, |
| 18 | Effect.gen(function* () { |
| 19 | const cache = yield* Fly.ReadRedis(Cache); |
| 20 | |
| 21 | return { |
| 22 | fetch: Effect.gen(function* () { |
| 23 | const request = yield* HttpServerRequest; |
| 24 | const path = new URL(request.url, "http://service").pathname; |
| 25 | const pong = yield* cache.ping().pipe( |
| 26 | Effect.map((body) => /pong/i.test(body)), |
| 27 | Effect.orElseSucceed(() => false), |
| 28 | ); |
| 29 | if (path === "/health") { |
| 30 | return yield* HttpServerResponse.json({ ok: pong }); |
| 31 | } |
| 32 | return yield* HttpServerResponse.json({ pong }); |
| 33 | }), |
| 34 | }; |
| 35 | }).pipe(Effect.provide(Fly.ReadRedisHttp)), |
| 36 | ) {} |