Web Development

Includes Ember client/server, middleware (CORS, logging, auth), circe integration (http4s-circe).

42.1 http4s (Typelevel, Cats Effect)

Includes Ember client/server, middleware (CORS, logging, auth), circe integration (http4s-circe).

import cats.effect.*
import org.http4s.*, org.http4s.dsl.io.*, org.http4s.ember.server.*, org.http4s.implicits.*
import com.comcast.ip4s.*

val routes = HttpRoutes.of[IO] {
  case GET -> Root / "hello" / name => Ok(s"Hello, $name!")
  case req @ POST -> Root / "echo" => req.as[String].flatMap(Ok(_))
}

object Server extends IOApp.Simple:
  def run = EmberServerBuilder.default[IO]
    .withHost(ipv4"0.0.0.0").withPort(port"8080")
    .withHttpApp(routes.orNotFound)
    .build.useForever

42.2 Tapir — describe endpoints as values

import sttp.tapir.*
val hello = endpoint.get.in("hello" / path[String]("name")).out(stringBody)
// Interpret as http4s/ZIO HTTP/Netty/Pekko routes, generate OpenAPI docs, or clients

42.3 Play Framework

MVC framework (Pekko-based in Play 3): routes file, controllers, Twirl templates, JSON, forms, hot reload — popular for traditional web apps.

class HomeController @Inject()(cc: ControllerComponents) extends AbstractController(cc):
  def index() = Action { Ok("Hello from Play") }

42.4 ZIO HTTP

import zio.*, zio.http.*
val app = Routes(Method.GET / "hello" -> handler(Response.text("Hello!")))
object Main extends ZIOAppDefault:
  def run = Server.serve(app).provide(Server.default)

42.5 Cask & Scalatra (lightweight)

object App extends cask.MainRoutes:
  @cask.get("/")
  def hello() = "Hello, World!"
  @cask.postJson("/sum")
  def sum(a: Int, b: Int) = ujson.Obj("result" -> (a + b))
  initialize()

42.6 HTTP clients

sttp client (basicRequest.get(uri"...").send(backend)), http4s client, ZIO HTTP client, requests-scala (requests.get(url).text()).

42.7 Other notable frameworks

Pekko HTTP / Akka HTTP (routing DSL), Finagle/Finatra (Twitter), Lagom (deprecated), Smithy4s (IDL-first services), Caliban (GraphQL), Sangria (GraphQL), gRPC via fs2-grpc/ZIO gRPC/Pekko gRPC.