HAppS tutorial

From HaskellWiki
Revision as of 22:33, 13 January 2007 by ShaeErisson (talk | contribs) (add file serving example)

Jump to: navigation, search

HAppS 0.9.0 Tutorial


To install HAppS the following packages are needed:

The quick way to see what's missing is to get the darcs repository, change into that directory, and run runghc Setup.hs configure. If you don't get an error, try runghc Setup.hs build and then as root runghc Setup.hs install.


The application model in HAppS is to help separate state, application logic, wire formats, protocols, and presentation layer:


State is just a haskell data type you define. ACID [2] Consistency enforced by Haskell's type system. ACID Durability is handled by MACID write-ahead logging and checkpointing.


Incoming events are gathered in individual haskell threads and then pushed onto a single application queue for processing. The queue model gives you ACID Atomicity and Isolation and lets your app be simply a set of functions with types like:

SomeInputType -> MACID SomeOutputType

The MACID monad lets you update your state and *schedule* side-effects. To be clear, MACID is not in the IO monad so you cannot execute side effects, you can only schedule them. The framework takes care of making sure they are executed at-least-once (if they can be completed by a deadline you specify).

Wire Formats

Since your app consists of a set of functions with various haskell input and output types, somewhere you need a place to convert between those internal haskell types and external protocol event types; e.g. from URL Encoded HTTP requests to SomeInputType and from SomeOutputType to XML encoded HTTP responses.


HAppS currently provides support for HTTP Requests/Responses and SMTP Envelopes. To be clear HAppS provides ACID Atomicity at the protocol event level. So if you write a protocol with SMTP envelopes being the arriving event type then your app will have atomicity in processing incoming SMTP envelopes. If you write a protocol with SMTP commands being the arriving event type, then your app will have atomicity at the level of individual smtp commands.


If your application outputs XML as its wire format, HAppS provides a lot of support for using XSLT to transform it for presentation purposes. For example, you can send XML mail and HAppS will take care of applying the relevant XSLT stylesheet before it is delivered. If you output XML HTTP responses, HAppS takes care of applying the XSLT stylesheet server side for user-agents that don't support doing so on the client. The value here is that you can have designer types who know XSLT modify presentation stuff without touching your application code.

First Steps

This chapter will run you through some first simple programs written in HAppS. For other programs have a look at the directory named 'examples'.

First of all, default HAppS applications run their own webserver on port 8000, so you probably want to try out these examples at http://localhost:8000/

If you'd rather access these applications on some other port, use ./myapp --default-port=8001 obviously substituting the name of your binary for myapp.

Hello World

Cut'n'paste this into a file named Hello.hs and run ghc --make Hello.hs -o hello to compile and then ./hello to execute the resulting binary.

import HAppS

-- simplest HAppS app

main = stdHTTP $ debugFilter : -- we want to see debug messages in the console
       noState : -- our application has no state
        h () GET $ ok plain $ val "Hello" -- any GET request will return "Hello"

First you import HAppS, then you pass your list of request handlers to stdHTTP. Handlers are tried in order. Handlers can do one of two things, they can modify the request and pass it on to the next handler, or they can handle the request.

The h handler takes three arguments, the url it should handle, the request type it should handle and the action it should take if it matches.

This example uses two system handlers, debugFilter and noState, and one custom handler inside the list.

debugFilter does not modify the request, but it does log lots of information about the request to the console. If you don't want see gobs of debug information in your console, you can take this out.

noState tells HAppS that this application doesn't have any state.

For this custom handler, any GET request will return "Hello". The specifics here are that ok is shorthand for sending back an HTTP 200 response, and plain formats the response value as text rather than applying some sort of formatting.

Serve static files

Another common task is to serve files from the file system.

The HAppS code to do this is basicFileServe. This example sets clientPath and staticPath to the same values as they are by default, so those two lines are optional.

import HAppS hiding (clientPath, staticPath)

-- serve from the filesystem

clientPath = "/s/"
staticPath = "static/"

main = stdHTTP $ debugFilter :
       noState :
        hs clientPath  GET $ basicFileServe staticPath
       ,h () GET $ ok plain $ val "Hello" -- http://localhost:8000/hello