[Added path.lhs Jeremy Shaw **20100522173721 Ignore-this: 37a94f8916fb8543d459f65d4d9b8c50 ] hunk ./Makefile 6 -DEMOS := Dir.lhs Dir2.lhs Dirs.lhs +DEMOS := Dir.lhs Dir2.lhs Dirs.lhs Path.lhs hunk ./Makefile 13 -RouteFilters.html: MonadPlus.lhs Dir.lhs Dir2.lhs Dirs.lhs +RouteFilters.html: MonadPlus.lhs Dir.lhs Dir2.lhs Dirs.lhs Path.lhs addfile ./Path.lhs hunk ./Path.lhs 1 + + +

Matching on dynamic path segments

+ +

path

+ +

Often times a path segment will contain a variable value we want to +extract and use, such as a number or a string. We can use the +path combinator to do that.

+ +#ifdef HsColour +> path :: (FromReqURI a, MonadPlus m, ServerMonad m) => (a -> m b) -> m b +#endif + +

path will attempt to extract and decode a path +segment, and if it succeeds, it will pass the decode value the nested +server part.

+ +

Let's start with the most basic example, extracting a +String value. We will extend the Hello World server so +that we can say hello to anyone.

+ +> module Main where +> +> import Control.Monad (msum) +> import Happstack.Server (nullConf, simpleHTTP, ok, dir, path) +> +> main :: IO () +> main = simpleHTTP nullConf $ msum [ dir "hello" $ path $ \s -> ok $ "Hello, " ++ s +> ] + +

[Source code for the app is here.]

+ +

Now, if we start the app and point our browser at: http://localhost:8000/hello/World we get the "Hello, World".

if we point it at http://localhost:8000/hello/Haskell, we get "Hello, Haskell".

hunk ./RouteFilters.lhs 27 - +#include "Path.lhs"