usethetypes#2: Your First Snap App

Now you have working Haskell Stack and VSCode installations, we can
create our first Snap application in Haskell. This
application will show you how to respond to the top-level route and how
to use alternation to handle named routes as well as a default route.

Voilà—we have pretty much the simplest possible web site in
about five lines of Haskell.

Also try out http://localhost:8000/foo.html. This errors out. This is
because the application explicitly handles only the top-level route
using ifTop.

Step 4: Clean up

Let’s go back and address the warning we see when we run the program:

stack exec snap-hello-world

What we’re seeing is a function of the default configuration used by
quickHttpServe: it expects a log directory under the current
directory in order to log to. Let’s create such a directory and ignore
it in Git:

Note that this is relative to the current working directory. Thus if you
run snap-hello-world in a subdirectory, the program will expect a
log directory there instead. Most likely as the program develops in
sophistication, you’ll end up logging to a location determined by an
environment variable or command-line option. We’ll explore this kind of
configuration in later videos.

Step 6: Alternation

Originally, we responded to only the top-level route using ifTop and
errored out for everything else. Then we removed this restrictions to
return “Hello World” for all paths. Let’s add an alternative route:

Now we’re using ifTop again plus <|> to provide alternatives. Snap
will evaluate the path conditions in the order they’re defined. ifTop
matches / while the last writeText "Bad path" matches anything not
already matched.