Created
March 27, 2023 08:02
-
-
Save tim-smart/8e5d8239c1d03f331bf805b541880d22 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { httpApp, response, router } from "@effect-http/core" | |
import { decodeParams } from "@effect-http/core/schema" | |
import { serve } from "@effect-http/node" | |
import { pipe } from "@effect/data/Function" | |
import * as Effect from "@effect/io/Effect" | |
import * as Schema from "@effect/schema/Schema" | |
import * as Http from "http" | |
const app = pipe( | |
router | |
.route("GET", "/health", Effect.succeed(response.json({ ok: true }))) | |
.route( | |
"GET", | |
"/params", | |
pipe( | |
decodeParams( | |
Schema.struct({ | |
id: Schema.string, | |
query: Schema.string, | |
}), | |
), | |
Effect.map(({ id, query }) => response.json({ id, query })), | |
), | |
) | |
.toHttpApp(), | |
httpApp.catchTags({ | |
DecodeSchemaError: () => | |
Effect.succeed(response.text("Bad request", { status: 400 })), | |
RouteNotFound: () => | |
Effect.succeed(response.text("Not found", { status: 404 })), | |
}), | |
) | |
pipe( | |
app, | |
serve(() => Http.createServer(), { port: 3000 }), | |
Effect.runPromise, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment