Watch Chris Learn!

Writings about the things I learn (Ruby / Haskell / Web / Other Stuff)

Spock Basics

Spock Intro – Minimal Web Framework

Spock is a slick little web framework in Haskell that builds off of Scotty’s legacy – although it apparently doesn’t share code any more.

It supports middleware, and a nice routing api.

For instance, to setup logging and static file serving, you just wire up two Wai middlewares.

1
2
3
4
appMiddleware :: SpockT IO ()
appMiddleware = do
  middleware logStdoutDev
  middleware $ staticPolicy (noDots >-> addBase "static")

Then the routes get built up (referencing the actual handler functions defined elsewhere)

1
2
3
4
5
appRoutes :: SpockT IO ()
appRoutes = do
  get "/"          $ Static.root
  get "/users"     $ Users.index
  get "/users/:id" $ Users.show

Then connect the pieces up and run on port 3000.

1
main = runSpock 3000 (appMiddleware >> appRoutes)

Handlers

I found myself repeating the specific ActionT type (the Spock route handler), so I type aliased it to be specific to my app (wrapping IO). This has the benefit of letting me change it in only one spot if/when I decide that I need a different monad transformer stack.

1
type HandlerM = ActionT IO

Then the actual handlers just have HandlerM and the return value (mostly just unit)

1
2
root :: HandlerM ()
root = text "Hello!"

There are a TON of helper functions to use in the context of a handler – redirect, json, html, setHeader, etc, etc.

More and More

Spock claims to support sessions, database connection pooling and more, but I haven’t had a chance to dive into that integration.

Comments