liveBook · Manning

Handler

As explained in Functional Design and Architecture: Examples in Haskell

A Handler is the Servant monad and request-handler type for implementing web operations and producing HTTP responses.

An event handler is the Servant-side function that processes an HTTP request and produces a response. Its type connects route inputs and request bodies to a Handler result, making the implementation match the declarative API. section 14.2.1

The basic Handler type is ExceptT ServerError IO: it permits real effects while converting failures into Servant-compatible HTTP errors. A direct handler can connect to a database, query it, and return a domain value.

Matching handlers to routes

For a partitioned AstroAPI, method handlers accept exactly the captured identifiers and request bodies declared by the routes. The server assembles them with :<|>: submitObjectTemplate :<|> getObject :<|> submitObjectOrbital :<|> submitObjectPhysical.

Keep the handler at the HTTP boundary when designing a maintainable service:

  • Avoid placing substantial business logic directly in Handler; that approach is described as difficult to test, easy to break, imperative, and impure. section 14.2.2
  • Move domain-aware operations into AppL, then let a Servant handler invoke the application scenario rather than coupling domain code to framework interfaces. section 14.2.2

How Functional Design and Architecture: Examples in Haskell covers it

How deep it goes
Defines Handler, demonstrates database-backed handlers, and extends them with ReaderT runtime execution; it stops short of Servant internals.
Examples
Uses AstroAPI routes, object retrieval, database queries, method-handler signatures, and handler composition with :<|>.
What it assumes
Assumes familiarity with typed Servant routes and request values while introducing Handler, AppHandler, runtime access, and layered execution.
What you can do after
Apply it via Servant to implement typed HTTP endpoints and delegate domain scenarios through a runtime-aware handler layer.
How it connects
Ties handlers to Servant, HTTP APIs, ExceptT, IO, ReaderT, AppL, Runtime, database effects, and business-logic separation.

Also explained in

Compare how 22 books explain handler →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Design and Architecture: Examples in Haskell. The section links point to the parts of the book it draws on.