Safe Haskell | Safe |
---|
Utility functions for routing.
- newtype ActionRoute b m s = ActionRoute (HttpReq s -> m (Maybe (Action s b m ())))
- runActionRoute :: Monad m => ActionRoute b m s -> Action s b m ()
- runIterAction :: Monad m => Action s ByteString m a -> HttpReq s -> Iter ByteString m (HttpResp m)
- runAction :: Monad m => Action s b m a -> HttpReq s -> b -> m (HttpResp m)
- routeAction :: Monad m => Action t b m () -> ActionRoute b m t
- routePattern :: Monad m => String -> ActionRoute b m t -> ActionRoute b m t
- routeName :: Monad m => String -> ActionRoute b m s -> ActionRoute b m s
- routeVar :: Monad m => ActionRoute b m s -> ActionRoute b m s
- routeTop :: Monad m => ActionRoute b m s -> ActionRoute b m s
- routeMethod :: Monad m => String -> ActionRoute b m s -> ActionRoute b m s
- routeFileSys :: (String -> ByteString) -> FilePath -> ActionRoute b IO t
Documentation
newtype ActionRoute b m s Source
An ActionRoute
either matches an HttpReq
and returns a
corresponding Action
that responds to it, or Nothing
signifying, no approriate Action
was found. ActionRoute
s can
be strung together with, e.g., mappend
such that each will be
searched in turn until an Action
responding to the HttpReq
is
found.
ActionRoute (HttpReq s -> m (Maybe (Action s b m ()))) |
Monad m => Monoid (ActionRoute b m s) |
runActionRoute :: Monad m => ActionRoute b m s -> Action s b m ()Source
Runs an ActionRoute
. If it satisfies the request, the
underlying Action
is returned, otherwise an Action
responding
with HTTP 404 (Not Found) is returned instead. Can be used at the
top of a request handler, for example:
httpHandler :: Action b m () httpHandler = runActionRoute $ mconcat [ routeTop $ routeAction homeAction , routeMethod "POST" $ routeAction handleForm , routeMethod "GET" $ routeAction showForm ]
But also can be nested inside of another action to created nested routes, or state dependant routes:
httpHandler :: Action b m () httpHandler = runActionRoute $ mconcat [ routeTop $ routeAction homeAction , routeName "foo" $ routeAction $ runActionRoute $ mconcat [ routeMethod "GET" $ runAction showForm , routeMethod "POST" $ runAction handleForm ] ]
or
handleForm = do day <- lift $ getDayOfWeek case mod day 2 of 0 -> runActionRoute $ routeName "stts" $ routeAction doRes 1 -> runActionRoute $ routeName "mwf" $ routeAction doRes
runIterAction :: Monad m => Action s ByteString m a -> HttpReq s -> Iter ByteString m (HttpResp m)Source
Like runAction
but consumes the rest of the request for the
body
routeAction :: Monad m => Action t b m () -> ActionRoute b m tSource
Routes an Action
routePattern :: Monad m => String -> ActionRoute b m t -> ActionRoute b m tSource
routeName :: Monad m => String -> ActionRoute b m s -> ActionRoute b m sSource
Routes a specific directory name, like routeMap
for a singleton
map.
routeVar :: Monad m => ActionRoute b m s -> ActionRoute b m sSource
Matches any directory name, but additionally pushes it onto the
front of the reqPathParams
list in the HttpReq
structure. This
allows the name to serve as a variable argument to the eventual
handling function.
routeTop :: Monad m => ActionRoute b m s -> ActionRoute b m sSource
Match request with path "/".
routeMethod :: Monad m => String -> ActionRoute b m s -> ActionRoute b m sSource
Match requests with the given method ("GET", "POST", etc).
routeFileSys :: (String -> ByteString) -> FilePath -> ActionRoute b IO tSource