[add WebRoutesHSP Jeremy Shaw **20120203221427 Ignore-this: a65977bc00ca50e0173186728788a39a ] hunk ./Makefile 14 -WEBROUTES_DEMOS := WebRoutesDemo.lhs WebRoutesBoomerang.lhs +WEBROUTES_DEMOS := WebRoutesDemo.lhs WebRoutesBoomerang.lhs WebRoutesHSP.lhs hunk ./RouteFilters.lhs 19 -

Happstack provides a variety of ways to match on parts of the -request and respond appropriately. One simple system is the use of the -guard functions in SimpleHTTP.

+

Happstack provides a variety of ways to match on parts of the Request (such as the path or request method) and respond appropriately.

+ +

Happstack provides two different systems for mapping the request path to a handler. In this section we will cover a simple, untyped routing system. Later in the crash course we will look at fancier, type-safe routing sytem known as web-routes.

hunk ./WebRoutes.lhs 41 +#include "WebRoutesHSP.lhs" addfile ./WebRoutesHSP.markdown.lhs hunk ./WebRoutesHSP.markdown.lhs 1 + +

web-routes and HSP

+ +

You will need to install the optional web-routes, web-routes-th, web-routes-hsp and happstack-hsp packages for this section.

+ +
+ +> {-# LANGUAGE TemplateHaskell #-} +> {-# OPTIONS_GHC -F -pgmFtrhsx #-} +> module Main where +> +> import Control.Applicative ((<$>)) +> import Happstack.Server +> import Happstack.Server.HSP.HTML +> import qualified HSX.XMLGenerator as HSX +> import Web.Routes +> import Web.Routes.TH +> import Web.Routes.XMLGenT +> import Web.Routes.Happstack + +
+ + +If you are using web-routes and HSP then inserting URLs is especially clean and easy. If we have the URL: + +
+ +> data SiteURL = Monkeys Int deriving (Eq, Ord, Read, Show) +> +> $(derivePathInfo ''SiteURL) +> + +
+ +Now we can define a template like this: + +
+ +> monkeys :: Int -> RouteT SiteURL (ServerPartT IO) Response +> monkeys n = +> do html <- defaultTemplate "monkeys" () $ +> <%> +>

You have <% show n %> monkeys.

+>

Click here for more.

+> +> ok $ (toResponse html) + +
+ +Notice that in particular this bit: + +
+#ifdef HsColour +> here +#endif +
+ + + +
+ +> route :: SiteURL -> RouteT SiteURL (ServerPartT IO) Response +> route url = +> case url of +> (Monkeys n) -> monkeys n + +
+ +

[Source code for the app is here.]