Skip to content

Instantly share code, notes, and snippets.

@tim-smart
Created March 27, 2023 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tim-smart/8e5d8239c1d03f331bf805b541880d22 to your computer and use it in GitHub Desktop.
Save tim-smart/8e5d8239c1d03f331bf805b541880d22 to your computer and use it in GitHub Desktop.
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