Chapter 29. Route Attributes
Route attributes allow you to set some metadata on each of your routes, in the route description itself. The syntax is trivial: just an exclamation point followed by a value. Using it is also trivial: just use the routeAttrs
function.
It’s easiest to understand how all this fits together, and when you might want to use it, with a motivating example. The case I personally most use this for is annotating administrative routes. Imagine having a website with about 12 different admin actions. You could manually add a call to requireAdmin
or some such at the beginning of each action, but:
-
It’s tedious.
-
It’s error prone: you could easily forget one.
-
Worse yet, it’s not easy to notice that you’ve missed one.
Modifying your isAuthorized
method with an explicit list of administrative
routes is a bit better, but it’s still difficult to see at a glance when you’ve
missed one.
This is why I like to use route attributes for this: you add a single word to
each relevant part of the route definition, and then you just check for that
attribute in isAuthorized
. Let’s see the code!
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
import
Data.Set
(
member
)
import
Data.Text
(
Text
)
import
Yesod
import
Yesod.Auth
import
Yesod.Auth.Dummy
data
App
=
App
mkYesod
"App"
[
parseRoutes
|
/
HomeR
GET
/
unprotected
UnprotectedR
GET
/
admin1
Admin1R
GET
!
admin
/
Get Developing Web Apps with Haskell and Yesod, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.