[added MonadPlus Jeremy Shaw **20100519173931 Ignore-this: 5c0b6491d003a7cff508bd59d41d40a1 ] hunk ./Makefile 13 -RouteFilters.html: Dir.lhs Dir2.lhs Dirs.lhs +RouteFilters.html: MonadPlus.lhs Dir.lhs Dir2.lhs Dirs.lhs addfile ./MonadPlus.lhs hunk ./MonadPlus.lhs 1 + + +

Choosing between multiple ServerPartTs

+ +

In the first example, we had only one ServerPartT. All +Requests were handled by the same part and returned the +same Response. + +

In general, our applications will have many +ServerPartTs. We combine them into a single top-level +ServerPartT by using MonadPlus. + +

In the following example we combine three ServerPartTs +together using the MonadPlus function. + +#ifdef HsColour +> msum :: (MonadPlus m) => [m a] -> m a +#endif + +> module Main where +> +> import Control.Monad +> import Happstack.Server (nullConf, simpleHTTP, ok, dir) +> +> main :: IO () +> main = simpleHTTP nullConf $ msum [ mzero +> , ok "Hello, World!" +> , ok "Unreachable ServerPartT" +> ] + +

[Source code for the app is here.]

+ +

The behaviour of MonadPlus is to try each ServerPartT in succession, until one succeeds.

+ +

In the example above, the first part is mzero, so it will always fail. The second part will always succeed. That means the third part will never be reachable.

+ +

Alas, that means this application will appear to behave exactly like the first application. What we need are some ways to have parts match or fail depending on the contents of the http Request. hunk ./RouteFilters.lhs 23 -

Let's look at some examples.

- +#include "MonadPlus.lhs"