Haskell DSLs for Interactive Web Services

Andrew Farmer
(joint work with Andy Gill and Jan Bracker)
University of Kansas

April 15, 2013

Motivation

So, we want a web service.

Yesod/Snap/Happstack

Haskell has a growing web ecosystem.

Scotty

A declarative DSL for RESTful web applications.

Scotty's design taken from that of the Ruby language's Sinatra.

Scotty

{-# LANGUAGE OverloadedStrings #-}

import Web.Scotty

import Data.Monoid (mconcat)

main = scotty 3000 $ do
  get "/:word" $ do
    beam <- param "word"
    html $ mconcat ["<h1>Scotty, ", beam, " me up!</h1>"]

Some types:

scotty :: Port -> ScottyM () -> IO ()

get :: RoutePattern -> ActionM () -> ScottyM ()

param :: Parsable a => Text -> ActionM a

html :: Text -> ActionM ()

See http://hackage.haskell.org/package/scotty for more!

Scotty - Typed Captures

Routes only match if the capture can be parsed at the correct type.

{-# LANGUAGE OverloadedStrings #-}

import Web.Scotty

import Data.Text.Lazy (pack)

main = scotty 3000 $ do
    get "/:x" $ do
        x <- param "x"
        text $ pack $ show $ x + (54::Int)

    get "/:word" $ do
        word <- param "word"
        text $ pack $ "you said: " ++ word

Try it out: cabal install scotty

Kansas Comet

Comet is a web design pattern allowing the server to push data to the client.

Kansas Comet:

Kansas Comet - Request/Response Cycle

Kansas Comet Request/Response Ladder Diagram

Sunroof

Sunroof compiles monadic Haskell code into Javascript.

Implemented as two packages:

Allows offloading of computation from server to client.