http4s

http4s

http4s typeful HTTP library patterns for building pure functional Scala services with cats-effect

Details

Language / Topic
scalaScala
Category
framework
Compatible Frameworks
http4s

Rules

balanced
- Define routes as `HttpRoutes[F]` with the DSL: `case GET -> Root / "users" / IntVar(id) => Ok(svc.find(id).map(_.asJson))`.
- Parameterise on `F[_]: Async` not `IO` — keeps routes runtime-agnostic and unit-testable without a running server.
- Start the server with `EmberServerBuilder.default[F].withHost(host).withPort(port).withHttpApp(router).build.useForever`.
- Decode JSON request bodies with `req.as[T]` using an implicit `EntityDecoder[F, T]` via `circe` or `jsoniter`.
- Build the app: `Router("/api" -> authedRoutes, "/" -> publicRoutes).orNotFound` — wrap with middleware before `orNotFound`.
- Add authentication middleware: `AuthMiddleware[F, User](authStore).apply(authedService)` — keep authed and public routes separate.
- Use `Client[F]` from `http4s-ember-client` for outbound calls — always `use` the client resource: `EmberClientBuilder.default[F].build.use(client => ...)`.
- Test routes with a mock request: `routes.orNotFound.run(Request[IO](GET, uri"/users/1")).unsafeRunSync()`.