Algebraic datatypes (ADT) are well suited to define website routes in Haskell. For example, the following type represents routes in a particular weblog site:
-- An example of nested routes
-- Route's expected encoding is given as a comment.
data Route
= Route_Index -- index.html
| Route_About -- about.html
| Route_Contact -- contact.html
| Route_Blog BlogRoute -- blog/<BlogRoute>
data BlogRoute
= BlogRoute_Index -- index.html
| BlogRoute_Post Slug -- post/<Slug>
newtype Slug = Slug { unSlug :: String }
As you can see, routes can be nested. BlogRoute
is a subroute of the Route
type. When encoding Route
, it can delegate to the encoder for BlogRoute
(inductively).
Routes are central to Ema apps
An Ema app is defined by its route type. All routes must be an instance of the IsRoute
class, which provides a route encoder (see Route Prism) that is used to convert to and from the corresponding .html
filepaths. This instance can be hand-written or derived generically.