[added FromReqURI, tweaked theme. Jeremy Shaw **20100524232712 Ignore-this: 348770482360f3e903874f97c430c0d9 ] hunk ./Dir.lhs 10 +
hunk ./Dir.lhs 22 +
hunk ./Dir2.lhs 10 +
hunk ./Dir2.lhs 22 +
hunk ./Dirs.lhs 10 +
hunk ./Dirs.lhs 22 +
addfile ./FromReqURI.lhs hunk ./FromReqURI.lhs 1 + + +

FromReqURI: extending path

+ +

We can extend path so that we can extract our own types from the path components as well. We simply add an instance to the FromReqURI class:

+ +#ifdef HsColour +> class FromReqURI a where +> fromReqURI :: String -> Maybe a +#endif + +

For example, let's say that we want to create a type to represent subjects we can greet.

+
+ +> module Main where +> +> import Control.Monad (msum) +> import Data.Char (toLower) +> import Happstack.Server (FromReqURI(..), nullConf, simpleHTTP, ok, dir, path) +> +> data Subject = World | Haskell +> +> sayHello :: Subject -> String +> sayHello World = "Hello, World!" +> sayHello Haskell = "Greetings, Haskell!" +> + +
+

We simple add an instance such as:

+
+ +> instance FromReqURI Subject where +> fromReqURI sub = +> case map toLower sub of +> "haskell" -> Just Haskell +> "world" -> Just World +> _ -> Nothing +> + +
+

Now when we use path it will extract a value of type Subject.

+
+ +> main :: IO () +> main = simpleHTTP nullConf $ msum [ dir "hello" $ path $ \subject -> ok $ (sayHello subject) +> + +
+

[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 "Greetings, Haskell!".

hunk ./HelloWorld.lhs 22 +
hunk ./HelloWorld.lhs 31 +
hunk ./Main.lhs 37 -
  • Routing URLS
  • +
  • Routing URLS +
      +
    1. Variable Path Segments +
        +
      1. path
      2. +
      3. FromReqURI: extending path
      4. +
      +
    2. +
    +
  • hunk ./Makefile 6 -DEMOS := Dir.lhs Dir2.lhs Dirs.lhs Path.lhs +DEMOS := Dir.lhs Dir2.lhs Dirs.lhs Path.lhs FromReqURI.lhs hunk ./Makefile 13 -RouteFilters.html: MonadPlus.lhs Dir.lhs Dir2.lhs Dirs.lhs Path.lhs +RouteFilters.html: MonadPlus.lhs Dir.lhs Dir2.lhs Dirs.lhs Path.lhs FromReqURI.lhs hunk ./Makefile 65 - sed -n 's/^> \?//p' $^ > $@ + cpphs --noline $^ | grep -v '{-# LANGUAGE CPP #-}' | sed -n 's/^> \?//p' > $@ +# cpp -DExtract -traditional-cpp -P $^ | grep -v '{-# OPTIONS_GHC -cpp #-}' | sed -n 's/^> \?//p' > $@ hunk ./MonadPlus.lhs 15 -ServerPartT by using MonadPlus. - -

    In the following example we combine three ServerPartTs -together using the MonadPlus function. +ServerPartT by using MonadPlus. Typically +via the msum function: hunk ./MonadPlus.lhs 22 +

    In the following example we combine three ServerPartTs +together.

    + +
    + hunk ./MonadPlus.lhs 38 +
    hunk ./Path.lhs 7 -

    Matching on dynamic path segments

    +

    Matching on variable path segments

    hunk ./Path.lhs 9 -

    path

    +

    path

    hunk ./Path.lhs 26 +
    hunk ./Path.lhs 37 +
    hunk ./Path.lhs 39 + +

    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 ./Path.lhs 43 -

    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 28 +#include "FromReqURI.lhs" hunk ./blog.css 3 - font-family: Georgia, Serif; +/* font-family: Georgia, Serif; */ + font-family: Sans-serif; hunk ./blog.css 6 - width: 30em; + width: 50em; hunk ./blog.css 51 +.source +{ + background: #eeeeee; + padding: 1em; + } + +