Typed routes in the Spock Web Framework

Alexander Thiemann & Tim Baumann

type Author = String

newtype PostId = PostId { getPostId :: Integer }
  deriving (PathPiece)

renderPostPage :: String -> Postid -> ActionT IO ()

get "/:author/blog/:postid" $ do
  mauthor <- param "author"
  mpostid <- param "postid"
  case (,) <$> mauthor <*> mpostid of
    Just (author, postid) -> renderPostPage author postid
    Nothing -> renderErrorPage "this should not happen"
    

Old style routing

...
<p>
  <a href="/alex/blog/42">The best monad tutorial ever</a>
</p>
...

New style routing

type Author = String

newtype PostId = PostId { getPostId :: Integer }
  deriving (PathPiece)

renderPostPage :: String -> Postid -> ActionT IO ()


get (var </> static "blog" </> var) renderPostPage

The type of Routes

postRoute :: Path '[String, PostId]
postRoute = var </> static "blog" </> var

(</>) :: Path ts -> Path ss -> Path (HAppendList ts ss)

-- Setting up the router
get postRoute renderPostPage

-- Rendering a link to a page
renderPath postRoute "alex" 42 :: Text
-- == "alex/blog/42"