iterio-server-0.3: Library for building servers with IterIO

Safe HaskellSafe

Data.IterIO.Http.Support.Routing

Description

Utility functions for routing.

Synopsis

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. ActionRoutes 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.

Constructors

ActionRoute (HttpReq s -> m (Maybe (Action s b m ()))) 

Instances

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

runAction :: Monad m => Action s b m a -> HttpReq s -> b -> m (HttpResp m)Source

Runs an Action given an HttpReq and body. Returns the HttpResp generated by the Action.

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

Routes an Action to the given URL pattern. Patterns can include directories as well as variable patterns (prefixed with :) to be passed into the Action as extra Params. Some examples of URL patters:

  • /posts/:id
  • /posts/:id/new
  • /:date/posts/:category/new

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).