Steve Klabnik

snap - a Haskell web framework

Yes, you heard me right. Yesterday, the Snap framework had its 0.3 release. If you haven’t heard of Haskell before, you’re in for a treat.

I’ll try to keep this short. Haskell is a functional programming language. This makes it a bit different than the procedural or object oriented languages you’re probably used to. The syntax looks much more like math notation than code:

f :: Int -> Int
f x = x + 1

This reads “f is a function that takes an Int and returns an Int, and f of x is equal to x + 1.” For more about Haskell, check out I could talk forever about how interesting and different Haskell is.

Enough about that. Let’s get back to Snap. To install Snap, use cabal, the Haskell package manager:

$ cabal install snap

Next, make a directory and create a project:

$ mkdir hello-snap
$ cd hello-snap
$ snap init

This gives you a directory with a .cabal file and a source directory. Then try this:

$ cabal install
$ hello-snap -p 8000
$ curl 'http://localhost:8000/'; echo

This’ll compile your website (yes, you read that correctly, Haskell is compiled), start a server on port 8000, and then request the homepage.

That’s all fine and dandy, but what’s the code look like? Well, if you look at the handler in Site.hs, you’ll find this:

site :: Application ()
site = route [ ("/",            index)
             , ("/echo/:stuff", echo)
       <|> fileServe "resources/static"

While the syntax may be foreign, you can get the gist of it: We route ‘/’ to index, and anything starting with ‘/echo/’ to an echo handler. Otherwise, serve a file from the resources/static directory.

What about one of these handlers? Here’s the echo handler:

echo :: Application ()
echo = do
    message <- decodedParam "stuff"
    heistLocal (bindString "message" message) $ render "echo"
    decodedParam p = fromMaybe "" <$> getParam p

Don’t be scared! It’s easy. Repeat after me: echo is an Application. When it’s called, we first decode the “stuff” parameter and bind it to message. We then use a Heist template and bind the “message” string inside to our message we created from the parameter, passing that along and using it to render the “echo” template. Oh, and that decodedParam method that we used to set up the message earlier gets the parameter we asked for, (p) and yields either it or an empty string. You can find a much more thorough explanation of this in the documentation, which is excellent.

So what’s new in v0.3? There are three big improvements, as far as I’m concerned:

  • SSL support
  • “Development Mode”, which means you don’t need to recompile your site every time you change your code. This is a big benefit to getting things done quickly.
  • an extensions mechanism for writing reusable components.

If you’d like to see an entire website written with Snap, the Snap website is itself written in Snap, which is also on GitHub.

[GitHub] [README] [Website]


Sign in or Join to comment or subscribe

Player art
  0:00 / 0:00