A quasi-quoter to parse a string into a list of Resources. Checks for overlapping routes, failing if present; use parseRoutesNoCheck to skip the checking. See documentation site for details on syntax.
Moduleyesod-core-1.7.0.0Haskell2010
Yesod.Core.Dispatch
- 5 types
- 4 classes
- 39 values
- Packageyesod-core-1.7.0.0
- Exports48
- LanguageHaskell2010
- LicenceMIT
- SourceParse.hs
Quasi-quoted routing
7 declarationsSame as parseRoutes, but performs no overlap checking.
Same as parseRoutes, but uses an external file instead of quasiquotation.
The recommended file extension is .yesodroutes.
Same as parseRoutesNoCheck, but uses an external file instead of quasiquotation.
The recommended file extension is .yesodroutes.
Generates URL datatype and site function for the given Resources. This is used for creating sites, not subsites. See mkYesodSubData and mkYesodSubDispatch for the latter. Use parseRoutes to create the Resources.
Contexts and type variables in the name of the datatype are parsed.
For example, a datatype App a with typeclass constraint MyClass a can be written as "(MyClass a) => App a".
mkYesod but with custom options.
mkYesodWith Deprecated. Contexts and type variables are now parsed from the name in mkYesod. https://github.com/yesodweb/yesod/pull/1366
Similar to mkYesod, except contexts and type variables are not parsed.
Instead, they are explicitly provided.
You can write (MyClass a) => App a with mkYesodWith [["MyClass","a"]] "App" ["a"] ....
More fine-grained
Sometimes, you will want to declare your routes in one file and define your handlers elsewhere. For example, this is the only way to break up a monolithic file into smaller parts. Use this function, paired with mkYesodDispatch, to do just that.
mkYesodData but with custom options.
See mkYesodData.
See mkYesodDataOpts
mkYesodSubDispatchInstance :: StringFoundation type with optional context, e.g.
"(MyClass a) => MySub a"-> [ResourceTree String]Resources (e.g.
resourcesMySub)-> Q [Dec]
Generate both YesodSubDispatch and YesodSubDispatchNested instances
for a parameterized subsite. This is the subsite equivalent of using
mkYesod for top-level sites.
Usage:
mkYesodSubDispatchInstance "(MyClass a) => MySub a" resourcesMySub
This generates:
A YesodSubDispatch instance using mkYesodSubDispatch
YesodSubDispatchNested instances for any nested route fragments
The generated instances quantify over the master site and constrain it
only through the subsite's own class. For a parameterized subsite this
normally means the subsite's class carries a subsite -> master functional
dependency (so master is determined) and the using module enables
UndecidableInstances (the instance contexts mention type-family
applications and non-variable arguments), along with FlexibleContexts,
FlexibleInstances, MultiParamTypeClasses and TypeFamilies. The nested
route datatype must also declare exactly as many type parameters as the
subsite has type arguments (see the arity note below).
Like mkYesodSubDispatchInstance, but takes a RouteOpts so a subsite can
control nested-route generation — most usefully setNestedRouteFallthrough
(which mkYesodSubDispatchInstance leaves at its default of False). The
flag is threaded into both the yesodSubDispatch body (so the subsite's own
top-level parent clauses fall through to later siblings on an inner miss) and
the generated YesodSubDispatchNested fragment instances.
Splitting a subsite's nested routes across modules
These re-exports let a separately-compiled module generate a
subsite's YesodSubDispatchNested instance by hand (see the
subsite-route-splitting recipe in docs/split-route-compilation.md).
mkNestedSubDispatchInstance takes the resources as
[ (the parseRoutes quasi-quoter's output)
and parses them internally, so no manual type-parsing is needed.ResourceTree String]
Generate a YesodSubDispatchNested instance for a nested route within a
subsite, for hand-written split-route modules (see the subsite-splitting
recipe in docs/split-route-compilation.md).
Takes the resources as [ResourceTree String] — exactly the form the
parseRoutes quasi-quoter produces — and parses them to Type internally
via parseResourceTypes, so the caller never touches the partial
parseType/dropBracket. A malformed type fails the splice with an
attributed error instead.
The type arguments of a route datatype, paired with the source Name each
was derived from. A monomorphic site like App has NoTyArgs; a
parameterized one like MySub subsite has SomeTyArgs.
Carrying the (non-)emptiness in the type — rather than as a bare list with a
separate null check — keeps "are there type args?" and "what are they?"
a single fact, so the populated branch hands you the NonEmpty directly.
Constructors
Route generation options
General opts data type for generating yesod.
Contains options for customizing code generation for the router in
mkYesodData, including what type class instances will be derived for
the route datatype, whether to parameterize subroutes,
and whether or not to create the resources :: [ResourceTree String] value.
Use the setting functions on defaultOpts to set specific fields.
Default options for generating routes.
The defaults are:
setEqDerived, setShowDerived, setReadDerived: True — derive Eq, Show and Read for the route datatype.
setCreateResources: True — emit the
resourcesSite :: [ResourceTree String]term.setParameterizedSubroute: False — subroutes are unparameterized.
setFocusOnNestedRoute: unset — generate the whole route tree rather than focusing on a single nested route.
setNestedRouteFallthrough: False — a nested route that fails to match throws
notFoundinstead of falling through.
Use the set* functions to override individual fields.
Determine whether or not to generate the resourcesApp value.
Disabling this can be useful if you are creating the routes ::
[ResourceTree String] elsewhere in your module, and referring to it
here. The resourcesApp can become very large in large applications,
and duplicating it can result in signifiacntly higher compile times.
Default: True.
If you set this with routeName, then the code generation will
generate code for the routeName to be imported in the main dispatch
class. This allows you to generate code in separate modules.
Example:
First, you would put your route definitions into their own file.
module App.Routes.Resources where
import Yesod.Core
appResources :: [ResourceTree String]
appResources = [parseRoutes|
/ HomeR GET
/nest NestR:
/ NestIndexR GET POST
/#Int NestShowR GET POST
|]
We have defined a nested route called NestR here. A nested route is
created with the : after the route name.
Then, in a module for the route specifically, we can generate the route datatype and instances for later hooking in to the main instance.
module App.Routes.NestR where
import App.Routes.Resources
import Yesod.Core
mkYesodOpts (setFocusOnNestedRoute NestR defaultOpts) App appResources
If you only want to generate the datatypes, you can separate things
further using mkYesodDataOpts and mkYesodDispatchOpts.
Finally, import that type into your main yesod macro code.
module App where
import App.Routes.Resources (appResources)
import App.Routes.NestR (NestR(..))
mkYesod App appResources
The call to mkYesod will delegate to the generated code for NestR
rather than regenerating it.
By default, defaultOpts starts unfocused (the whole route tree is generated). Use unsetFocusOnNestedRoute to clear a previously-set focus.
For a full walkthrough — including subsites, linking to nested routes, and why split sites should enable setNestedRouteFallthrough — see the guide at https://github.com/yesodweb/yesod/blob/master/yesod-core/docs/split-route-compilation.md.
Clear any nested-route focus set by setFocusOnNestedRoute, so that the whole route tree is generated.
defaultOpts already starts unfocused, so this is only needed to undo a previous setFocusOnNestedRoute.
If True, generate nested-discovery code (separate subroute datatypes and
RenderRouteNested dispatch instances) for a parameterized/ site instead
of the backwards-compatible inline output. The subroute datatypes then carry
the parent site's type variables so that the ParentSite / ParentArgs
associated types stay well-scoped.
Because the generated nested instances are parameterized over the site's
type variables, the splice emits instance heads with non-variable arguments;
a module using this typically needs FlexibleContexts, FlexibleInstances,
MultiParamTypeClasses, TypeFamilies and, for a parameterized subsite
whose master is determined by the subsite (a subsite -> master
functional dependency on the user's class), UndecidableInstances.
Monomorphic sites always use nested discovery regardless of this flag; see
discoveryMode.
Default: False.
Helpers
Default constructor for yreGetMaxExpires field. Low level function for simple manual construction of YesodRunnerEnv.
Path pieces
Methods
fromPathPiece :: Text -> Maybe stoPathPiece :: s -> Text
Instances18PathPiece, …
PathPiece StringDefined in path-pieces-0.2.1 · Web.PathPiecesPathPiece IntegerDefined in path-pieces-0.2.1 · Web.PathPiecesPathPiece Int16Defined in path-pieces-0.2.1 · Web.PathPiecesPathPiece Int32Defined in path-pieces-0.2.1 · Web.PathPiecesPathPiece Int64Defined in path-pieces-0.2.1 · Web.PathPiecesPathPiece Int8Defined in path-pieces-0.2.1 · Web.PathPiecesPathPiece BoolDefined in path-pieces-0.2.1 · Web.PathPiecesPathPiece IntDefined in path-pieces-0.2.1 · Web.PathPiecesPathPiece WordDefined in path-pieces-0.2.1 · Web.PathPiecesPathPiece Word16Defined in path-pieces-0.2.1 · Web.PathPiecesPathPiece Word32Defined in path-pieces-0.2.1 · Web.PathPiecesPathPiece Word64Defined in path-pieces-0.2.1 · Web.PathPiecesPathPiece Word8Defined in path-pieces-0.2.1 · Web.PathPiecesPathPiece TextDefined in path-pieces-0.2.1 · Web.PathPiecesPathPiece TextDefined in path-pieces-0.2.1 · Web.PathPiecesPathPiece DayDefined in path-pieces-0.2.1 · Web.PathPiecesPathPiece ()Defined in path-pieces-0.2.1 · Web.PathPiecesPathPiece a => PathPiece (Maybe a)Defined in path-pieces-0.2.1 · Web.PathPieces
Methods
fromPathMultiPiece :: [Text] -> Maybe stoPathMultiPiece :: s -> [Text]
Instances1PathMultiPiece
PathPiece a => PathMultiPiece [a]Defined in path-pieces-0.2.1 · Web.PathPieces
Convert to WAI
12 declarationsSame as toWaiAppPlain, but provides a default set of middlewares. This set may change with future releases, but currently covers:
Logging
GZIP compression
Automatic HEAD method handling
Request method override with the _method query string parameter
Accept header override with the _accept query string parameter
Convert the given argument into a WAI application, executable with any WAI handler. This function will provide no middlewares; if you want commonly used middlewares, please use toWaiApp.
Build a WAI Application for site, indexed by a url type that can
be rendered to a route of site (via RedirectUrl).
Note that dispatch is purely path-based: the hand-written instances below
discard the url value (urlToDispatch _ = yesodDispatch) and route on the
request's pathInfo. The url argument exists only to select the instance
and to carry the RedirectUrl constraint — it is not consulted at runtime.
This is equivalent to YesodDispatch but allows us to vary on the
url type, which lets us demand additional constraints in test
contexts.
Methods
urlToDispatch :: url -> YesodRunnerEnv site -> Application
Instances5UrlToDispatch
YesodDispatch site => UrlToDispatch String siteDefined in yesod-core-1.7.0.0 · Yesod.Core.Class.DispatchYesodDispatch site => UrlToDispatch Text siteDefined in yesod-core-1.7.0.0 · Yesod.Core.Class.DispatchYesodDispatch site => UrlToDispatch (Route site) siteDefined in yesod-core-1.7.0.0 · Yesod.Core.Class.Dispatch(ParentSite route ~ site, YesodDispatchNested route, RedirectUrl site (WithParentArgs route), Yesod site, ToParentRoute route) => UrlToDispatch (WithParentArgs route) siteDefined in yesod-core-1.7.0.0 · Yesod.Core.Class.DispatchThis instance is the pattern upon which UrlToDispatch instances are generated for subsite fragments. Given the constraints, we are able to call toWaiAppYreNested at the
routetype with the correct arguments. This allows us to create an application for a fragment of a site.(YesodDispatch site, RedirectUrl site (Route site, a)) => UrlToDispatch (Route site, a) siteDefined in yesod-core-1.7.0.0 · Yesod.Core.Class.Dispatch
Construct a YesodRunnerEnv for a site, initializing its logger, session backend, and other runtime state. This is the environment threaded through dispatch; toWaiAppPlain and friends build one for you, but it is exposed so callers can construct and reuse a runner environment directly.
Convert the given argument into a WAI application, executable with any WAI handler. This function will provide no middlewares; if you want commonly used middlewares, please use toWaiApp.
This function allows you to create an Application that only responds
to a subset of the site routes. This is really only useful for writing
tests, but it does allow you to write tests that only depend on a subset
of the handlers instead of all handlers.
Pure low level function to construct WAI application. Usefull when you need not standard way to run your app, or want to embed it inside another app.
Build a WAI Application that dispatches starting at a nested route
fragment a (rather than the whole Route site), given a
YesodRunnerEnv for the parent site and the parent route's ParentArgs.
Handles cleanPath redirects and a terminal 404 just like toWaiAppYre.
A convenience method to run an application using the Warp webserver on the specified port. Automatically calls toWaiApp. Provides a default set of middlewares. This set may change at any point without a breaking version number. Currently, it includes:
Logging
GZIP compression
Automatic HEAD method handling
Request method override with the _method query string parameter
Accept header override with the _accept query string parameter
If you need more fine-grained control of middlewares, please use toWaiApp directly.
Since 1.2.0
Deprecated. Please use warp instead
Deprecated synonym for warp.
Runs your application using default middlewares (i.e., via toWaiApp). It reads port information from the PORT environment variable, as used by tools such as Keter and the FP Complete School of Haskell.
Note that the exact behavior of this function may be modified slightly over time to work correctly with external tools, without a change to the type signature.
A default set of middlewares.
Since 1.2.0
All of the default middlewares, excluding logging.
Since 1.2.12
Subsite nested dispatch
1 declarationLike YesodDispatchNested, but for subsites. This class enables class-based delegation for nested routes within subsites, allowing module separation and symmetry with YesodDispatchNested.
When a YesodSubDispatchNested instance exists for a nested route type,
mkYesodSubDispatch will delegate to it instead of inlining the dispatch.
Use mkYesodSubDispatchInstance to generate both YesodSubDispatch and
YesodSubDispatchNested instances in one splice.
Methods
yesodSubDispatchNested :: Proxy a -> ParentArgs a -> (a -> Route (ParentSite a)) -> YesodSubRunnerEnv (ParentSite a) master -> Request -> Maybe ((Response -> IO ResponseReceived) -> IO ResponseReceived)Dispatches a request for a nested route fragment within a subsite.
Similar to yesodDispatchNested, but works with YesodSubRunnerEnv instead of YesodRunnerEnv, making it suitable for subsites. The
mastertype is universally quantified since it's not determined by the nested route type.
WAI subsites
2 declarationsWrap up a normal WAI application as a Yesod subsite. Ignore parent site's middleware and isAuthorized.
Constructors
Instances8ParseRoute, RenderRoute, YesodSubDispatch, Eq, Ord, Read, …
ParseRoute WaiSubsiteDefined in yesod-core-1.7.0.0 · Yesod.Core.TypesRenderRoute WaiSubsiteDefined in yesod-core-1.7.0.0 · Yesod.Core.TypesYesodSubDispatch WaiSubsite masterDefined in yesod-core-1.7.0.0 · Yesod.Core.Class.DispatchEq (Route WaiSubsite)Defined in yesod-core-1.7.0.0 · Yesod.Core.TypesOrd (Route WaiSubsite)Defined in yesod-core-1.7.0.0 · Yesod.Core.TypesRead (Route WaiSubsite)Defined in yesod-core-1.7.0.0 · Yesod.Core.TypesShow (Route WaiSubsite)Defined in yesod-core-1.7.0.0 · Yesod.Core.Typesdata Route WaiSubsiteWaiSubsiteRoute [Text] [(Text, Text)]
Like WaiSubsite, but applies parent site's middleware and isAuthorized.
Constructors
Instances8ParseRoute, RenderRoute, YesodSubDispatch, Eq, Ord, Read, …
ParseRoute WaiSubsiteWithAuthDefined in yesod-core-1.7.0.0 · Yesod.Core.TypesRenderRoute WaiSubsiteWithAuthDefined in yesod-core-1.7.0.0 · Yesod.Core.TypesYesodSubDispatch WaiSubsiteWithAuth masterDefined in yesod-core-1.7.0.0 · Yesod.Core.Class.DispatchEq (Route WaiSubsiteWithAuth)Defined in yesod-core-1.7.0.0 · Yesod.Core.TypesOrd (Route WaiSubsiteWithAuth)Defined in yesod-core-1.7.0.0 · Yesod.Core.TypesRead (Route WaiSubsiteWithAuth)Defined in yesod-core-1.7.0.0 · Yesod.Core.TypesShow (Route WaiSubsiteWithAuth)Defined in yesod-core-1.7.0.0 · Yesod.Core.Typesdata Route WaiSubsiteWithAuthWaiSubsiteWithAuthRoute [Text] [(Text, Text)]