Copyright | (c) Anupam Jain 2013 |
---|---|
License | MIT (see the file LICENSE) |
Maintainer | ajnsit@gmail.com |
Stability | experimental |
Portability | non-portable (uses ghc extensions) |
Safe Haskell | None |
Language | Haskell2010 |
This package provides typesafe URLs for Wai applications.
- parseRoutes :: QuasiQuoter
- parseRoutesFile :: FilePath -> Q Exp
- parseRoutesNoCheck :: QuasiQuoter
- parseRoutesFileNoCheck :: FilePath -> Q Exp
- mkRoute :: String -> [ResourceTree String] -> Q [Dec]
- routeDispatch :: Routable master => master -> Middleware
- showRoute :: RenderRoute master => Route master -> Text
- readRoute :: ParseRoute master => Text -> Maybe (Route master)
- type Handler master = master -> App master
- class Routable master where
- dispatcher :: Handler master
- class Eq (Route a) => RenderRoute a where
- class RenderRoute a => ParseRoute a where
- class RenderRoute a => RouteAttrs a where
- routeAttrs :: Route a -> Set Text
- data RequestData master
- waiReq :: RequestData master -> Request
- nextApp :: RequestData master -> Application
- runNext :: App master
- data RouteM a
- defaultAction :: Application -> RouteM ()
- middleware :: Middleware -> RouteM ()
- route :: Routable master => master -> RouteM ()
- toWaiApp :: RouteM () -> IO Application
- type HandlerM master a = HandlerMI master IO a
- runHandlerM :: HandlerM master () -> Handler master
- request :: HandlerM master Request
- maybeRoute :: HandlerM master (Maybe (Route master))
- routeAttrSet :: RouteAttrs master => HandlerM master (Set Text)
- master :: HandlerM master master
- header :: HeaderName -> ByteString -> HandlerM master ()
- status :: Status -> HandlerM master ()
- raw :: ByteString -> HandlerM master ()
- json :: ToJSON a => a -> HandlerM master ()
- text :: Text -> HandlerM master ()
- html :: ByteString -> HandlerM master ()
- next :: HandlerM master ()
Quasi Quoters
parseRoutes :: QuasiQuoter Source
A quasi-quoter to parse a string into a list of Resource
s. Checks for
overlapping routes, failing if present; use parseRoutesNoCheck
to skip the
checking. See documentation site for details on syntax.
Parse Routes declared inline
parseRoutesFile :: FilePath -> Q Exp Source
Parse routes declared in a file
parseRoutesNoCheck :: QuasiQuoter Source
Same as parseRoutes
, but performs no overlap checking.
Parse routes declared inline, without checking for overlaps
parseRoutesFileNoCheck :: FilePath -> Q Exp Source
Parse routes declared in a file, without checking for overlaps
Template Haskell methods
mkRoute :: String -> [ResourceTree String] -> Q [Dec] Source
Generates all the things needed for efficient routing,
including your application's Route
datatype, and
RenderRoute
, ParseRoute
, and RouteAttr
instances
Dispatch
routeDispatch :: Routable master => master -> Middleware Source
Generates the application middleware from a Routable
master datatype
URL rendering and parsing
readRoute :: ParseRoute master => Text -> Maybe (Route master) Source
Read a route from Text Returns Nothing if Route reading failed. Just route otherwise
Application Handlers
type Handler master = master -> App master Source
A Handler
generates an App from the master datatype
Generated Datatypes
class Routable master where Source
A Routable
instance can be used in dispatching.
An appropriate instance for your site datatype is
automatically generated by mkRoute
dispatcher :: Handler master Source
Used internally. However needs to be exported for TH to work.
class Eq (Route a) => RenderRoute a where Source
The type-safe URLs associated with a site argument.
A RenderRoute
instance for your site datatype is automatically generated by mkRoute
class RenderRoute a => ParseRoute a where Source
A ParseRoute
instance for your site datatype is automatically generated by mkRoute
class RenderRoute a => RouteAttrs a where Source
:: Route a | |
-> Set Text | A set of attributes associated with the route. |
A RouteAttrs
instance for your site datatype is automatically generated by mkRoute
Accessing Raw Request Data
data RequestData master Source
An abstract representation of the request data. You can get the wai request object by using waiReq
waiReq :: RequestData master -> Request Source
Extract the wai Request
object from RequestData
nextApp :: RequestData master -> Application Source
Extract the next Application in the stack
Run the next application in the stack
Route Monad makes it easy to compose routes together
defaultAction :: Application -> RouteM () Source
Set the default action of the Application. You should only call this once in an application. Subsequent invocations override the previous settings.
Set the default action for a app
middleware :: Middleware -> RouteM () Source
Add a middleware to the application. Middleware is nested so the one declared earlier is outer.
Add another middleware to the app
route :: Routable master => master -> RouteM () Source
Add a route to the application. Routes are ordered so the one declared earlier is matched first.
Add another routed middleware to the app
toWaiApp :: RouteM () -> IO Application Source
Convert a RouteM Monadic value into a wai application.
Convert a RouteM to a wai Application
HandlerM Monad makes it easy to build a handler
runHandlerM :: HandlerM master () -> Handler master Source
Run HandlerM, resulting in a Handler
Run a HandlerM to get a Handler
Access the request data
maybeRoute :: HandlerM master (Maybe (Route master)) Source
Get the current route
Access the current route
routeAttrSet :: RouteAttrs master => HandlerM master (Set Text) Source
Get the current route attributes
Access the current route attributes as a set
Access the master datatype
header :: HeaderName -> ByteString -> HandlerM master () Source
Add a header to the application response TODO: Differentiate between setting and adding headers
Add a header to the response
Set the response status
raw :: ByteString -> HandlerM master () Source
Set the response body TODO: Add functions to append to body, and also to flush body contents
Set the raw response body
json :: ToJSON a => a -> HandlerM master () Source
Set the body of the response to the JSON encoding of the given value. Also sets "Content-Type" header to "application/json".
Set the json response body
text :: Text -> HandlerM master () Source
Set the body of the response to the given Text
value. Also sets "Content-Type"
header to "text/plain".
Set the text response body
html :: ByteString -> HandlerM master () Source
Set the body of the response to the given Text
value. Also sets "Content-Type"
header to "text/html".
Set the html response body
Run the next application in the stack