[a new tutorial on happstack Jeremy Shaw **20100409155857 Ignore-this: e99212237732f4ecbf4954d2bd866a17 ] addfile ./HelloWorld.lhs addfile ./Main.lhs addfile ./Makefile addfile ./blog.css addfile ./hscolour.css hunk ./HelloWorld.lhs 1 - +

Hello, World!

+ +

Our first happstack application is a simple server that response to +all requests with the string, Hello, World!.

+ +> module Main where +> +> import Happstack.Server (nullConf, simpleHTTP, toResponse, ok) +> +> main :: IO () +> main = simpleHTTP nullConf $ ok (toResponse "Hello, World!") + +

Source code for the app is here.

+ +

Run this app and point your browser at http://localhost:8000/. (assuming +you are building the program on your local machine.)

+ +

If we point curl at the app we get the following output:

+ +
+ happstack-crashcourse $ curl -v http://localhost:8000/
+ * About to connect() to localhost port 8000 (#0)
+ *   Trying ::1... Connection refused
+ *   Trying 127.0.0.1... connected
+ * Connected to localhost (127.0.0.1) port 8000 (#0)
+ > GET / HTTP/1.1
+ > User-Agent: curl/7.19.5 (i486-pc-linux-gnu) libcurl/7.19.5 OpenSSL/0.9.8g zlib/1.2.3.3 libidn/1.10 libssh2/0.18
+ > Host: localhost:8000
+ > Accept: */*
+ > 
+ < HTTP/1.1 200 OK
+ < Connection: Keep-Alive
+ < Content-Length: 13
+ < Content-Type: text/plain; charset=UTF-8
+ < Date: Fri, 09 Apr 2010 14:47:10 GMT
+ < Server: Happstack/0.4.3
+ < 
+ * Connection #0 to host localhost left intact
+ * Closing connection #0
+ Hello, World!
+
+ +#ifdef HsColour +> simpleHTTP :: (ToMessage a) => Conf -> ServerPartT IO a -> IO () +#endif hunk ./Main.lhs 1 - + + + + Data Serialization, Deserialization, and Migration using HAppS-Data + + + + + + + +

Happstack is the Haskell application server +stack. It is primarily used for web application development, +but it can be used for any type of application server.

+ +

Happstack's mission is to enable developers to prototype quickly, +deploy painlessly, scale massively, operate reliably, and change +easily.

+ +

This tutorial assumes that you will be deploying your application +on a VPS or dedicated server where you can have a server process that +is always running. However, many components of Happstack can be used +in a CGI / shared-server environment.

+ +#ifdef HsColour +#include "HelloWorld.lhs" +#endif + + + + hunk ./Makefile 1 +BASENAME := HappstackCrashCourse + +MAIN := Main.lhs +DEPS := +DEMOS := HelloWorld.lhs +EXTRA_DEPS := Makefile +SRC := $(MAIN) $(DEPS) $(DEMOS) +DESTDIR := html/ +CSS := blog.css hscolour.css + + +all: $(DESTDIR) $(subst .lhs,.hs,$(addprefix $(DESTDIR),$(DEMOS))) $(addprefix $(DESTDIR),$(CSS)) $(DESTDIR)$(BASENAME).html + +$(DESTDIR): + mkdir -p $(DESTDIR) + +$(DESTDIR)$(BASENAME).html: Main.html + #tidy -q -o $@ Main.html + cp $< $@ + @echo "" + @echo "********************************************************************************************" + @echo "Open the following file in your browser, or run 'make open' to automatically open in firefox" + @(echo file://$$(realpath $@)) + @echo "********************************************************************************************" + +$(DESTDIR)%: % + cp $^ $@ + +# open the .html file in firefox +# probably will not work if path contains spaces, etc +open: $(BASENAME).html + (firefox -new-tab "file://$$(realpath $<)") + +Main.html : $(SRC) $(EXTRA_DEPS) + +EXTRA := template/App.cabal template/Setup.hs + +template/% : extra/% + cp $^ $@ + +TEMPLATE_FILES := $(subst .lhs,.hs,$(addprefix template/,$(SRC))) $(EXTRA) +template: $(TEMPLATE_FILES) + +template/%.hs: %.lhs + (mkdir -p `dirname $@`) + cpp -DExtract -traditional-cpp -P $^ | grep -v '{-# OPTIONS_GHC -cpp #-}' | sed -n 's/^> \?//p' > $@ + +template/%: % + cp $^ $@ + +test: + runhaskell -cpp -ipages Main.lhs + +%.html: %.cpphs + HsColour -css -partial $< -o$@ -lit + validate -w --verbose --emacs $@ + + +%.cpphs: %.lhs +# cpp -traditional-cpp -DHsColour -P $< $@ + cpphs --noline -DHsColour $< -O$@ + +$(DESTDIR)%.hs: %.lhs + sed -n 's/^> \?//p' $^ > $@ + +clean: + -rm -f $(BASENAME).html Main.html + -rm -f $(TEMPLATE_FILES) + (if test -d template/pages ; then rmdir template/pages ; fi) + (if test -d template ; then rmdir template ; fi) + +.PHONY: test all clean hunk ./blog.css 1 +body +{ + margin: auto; + width: 40em; +} + +h1 +{ + /* font-family: Georgia, Serif; */ + font-family: 'Trebuchet MS',Trebuchet,Arial,Verdana,Sans-serif; + font-weight: normal; + color: #c60; + letter-spacing:.2em; + } + +h4 +{ color: #666; + font-family: 'Trebuchet MS',Trebuchet,Arial,Verdana,Sans-serif; + font-weight: normal; + font-size: 140%; + letter-spacing:.2em; + } hunk ./hscolour.css 1 +.keyglyph { color: DarkGoldenrod; } +.layout { color: Black;} +.keyword { color: PaleGreen;} +.comment, .comment a {color: Firebrick;} +.str { color: RosyBrown; } +.chr { color: RosyBrown;} +.keyword { color: BlueViolet; } +.conid { color: ForestGreen; } +.varid { color: black; } +.num { color: black; } +.varop { color: DarkGoldenrod; } +.conop { color: DarkGoldenrod; } +.sel { color: Red; } +.cpp { color: Red; } +.definition { color: Blue; }