[added more documentation Jeremy Shaw **20100516195753 Ignore-this: ae24a1102ccb3cf8dc13626cac25771f ] addfile ./Dir.lhs hunk ./Dir.lhs 1 + + +

Using dir to match on static path components

+ +

We can use dir to handle components of the URI path which are static. For example, we might have a site with the two URLS /hello and /goodbye.

+ +> module Main where +> +> import Control.Monad +> import Happstack.Server (nullConf, simpleHTTP, toResponse, ok, dir, path, nullDir, methodM) +> +> main :: IO () +> main = simpleHTTP nullConf $ msum [ dir "hello" $ ok "Hello, World!" +> , dir "goodbye" $ ok "Goodbye, World!" +> ] + +

Source code for the app is here.

+ +

If we start the app and point our browser at http://localhost:8000/hello we get the hello message, and if we point it at http://localhost:8000/goodbye, we get the goodbye message.

hunk ./HelloWorld.lhs 1 + + + + Crash Course in Happstack + + + + hunk ./HelloWorld.lhs 15 - +

Back to Table of Contents

hunk ./HelloWorld.lhs 100 -

ok is one of several combinators which can be used to set the HTTP response code. In this case, it will set the response code to 200 OK.

+

ok is one of several combinators which can be used to set the HTTP response code. In this case, it will set the response code to 200 OK. Happstack.Server.SimpleHTTP contains similar functions for the common HTTP respond codes including, notFound, seeOther, badRequest and more.

hunk ./HelloWorld.lhs 113 + + hunk ./Main.lhs 30 -#ifdef HsColour -#include "HelloWorld.lhs" -#endif - +
    +
  1. Hello World!
  2. +
  3. Routing URLS
  4. +
hunk ./Makefile 1 -BASENAME := HappstackCrashCourse +BASENAME := index +DESTDIR := html/ hunk ./Makefile 5 -DEPS := -DEMOS := HelloWorld.lhs +DEPS := RouteFilters.lhs HelloWorld.lhs +DEMOS := HelloWorld.lhs Dir.lhs hunk ./Makefile 9 -DESTDIR := html/ hunk ./Makefile 12 -all: $(DESTDIR) $(subst .lhs,.hs,$(addprefix $(DESTDIR),$(DEMOS))) $(addprefix $(DESTDIR),$(CSS)) $(DESTDIR)$(BASENAME).html +all: $(DESTDIR) $(subst .lhs,.hs,$(addprefix $(DESTDIR),$(DEMOS))) $(addprefix $(DESTDIR),$(CSS)) $(subst .lhs,.html,$(addprefix $(DESTDIR),$(DEPS))) $(DESTDIR)$(BASENAME).html addfile ./RouteFilters.lhs hunk ./RouteFilters.lhs 1 + + + + Crash Course in Happstack + + + + + +

Back to Table of Contents

+

Route Filters a.k.a Responding to different url paths

+ +

Happstack provides a variety ways to dissect the the url portion of +a request and respond appropriately. One simple system is by the use +of the guard functions in SimpleHTTP.

+ +

Let's look at some examples.

+ +#include "Dir.lhs" + + + +