Baby Steps with HAppS v0.9.1
1. goal
2. installation
3. hello world
4. xhtml
5. xhtml.strict (custom response)
6. credits
1. GOAL
To document the steps taken as I am trying out
the new champion on the horizon -- HAppS.
I will start from the simplest server configuration without a state
system
to get going quickly.
2. INSTALLATION
The first obstacle, and the hardest one for newbies like me, is to
have a
working installation. Yampa has the same problem but that's a
different story.
While Haskell may be the best language on the planet right now, it
does lack
well developed tools like Maven. Let's hope this will be resolved
in the near future.
I had already unsuccessfully attempted to play around with Happs
earlier.
So i had older version of HAppS lying around from the already split
repositories. If you are in the same situation, simply
replace "darcs get xxxx" with "cd HAppS-xxx && darcs pull && cd .."
the first thing to do is get the dependencies:
hslogger, syb-with-class, NewBinary, HTTP, Crypto, and HaXml-1.13.2
and of course ghc-6.8.1
syb will not install from the Hackage on my ghc-6.8.1
i had to get it from darcs:
darcs get
http://happs.org/HAppS/syb-with-class
cd syb-with-class
runhaskell Setup.hs configure
runhaskell Setup.hs build
sudo runhaskell Setup.hs install
cd ..
hslogger wont install from Hackage too. use this:
darcs get --partial
http://darcs.complete.org/hslogger
Haxml must be below version 1.14:
http://hackage.haskell.org/cgi-bin/hackage-scripts/package/HaXml-1.13.3
wget
http://hackage.haskell.org/packages/archive/HaXml/1.13.3/HaXml-1.13.3.tar.gz
tar zxvf HaXml-1.13.3.tar.gz
cd HaXml-1.13.3
runhaskell Setup.hs configure
runhaskell Setup.hs build
sudo runhaskell Setup.hs install
cd ..
Before configuring NewBinary, make sure that NewBinary.cabal has this
line:
Build-Depends: base, array
Before configuring Crypto, make sure that Crypto.cabal has this line:
Build-Depends: base, mtl, QuickCheck, HUnit, NewBinary,
containers, old-time, array, random, pretty
The rest (HTTP) can be installed from Hackage using latest versions
without problems.
Follow the instructions on
http://happs.org/#cabal to get HAppS
installed.
You can skip installing HAppS-Begin,
but get it in any case to check out examples.
3. HELLO WORLD
Finally Happs is installed. Now let's make the simplest possible
webapp.
Create a file named Server.hs with the following:
{-# OPTIONS -fglasgow-exts #-}
import HAppS
main = do
simpleHTTP [method GET $ "Hello, World!"]
getLine
now to compile and run it:
ghc --make Server.hs
./Server
the application will wait for a newline to quit. visit your brand new
webapp at
http://localhost:8000/
4. XHTML
It's very simple to have an XHtml output instead of plain text with
Text.XHtml
You need to install xhtml package from Hackage.
Modify Server.hs to read:
{-# OPTIONS -fglasgow-exts #-}
import HAppS
import Text.XHtml.Transitional hiding (method)
main = do
simpleHTTP [method GET $ response1]
getLine
response1 = ok $
(header $ thetitle << "My First Page")
+++
(body $ h1 << "Hello, World!")
Compile and run again. we now have XHtml!
5. CUSTOM RESPONSE
Note that i am not calling any of the rendering functions.
HAppS polymorphs on the response type. In the case of Html,
it assumes Text.XHtml.Transitional and calls prettyHtml.
I would like to have a Strict XHtml and not have a pretty-print as it
can
cause problems in some cases. To achieve that we will create a new
response
type and implement ToMessage class like so:
{-# OPTIONS -fglasgow-exts #-}
import HAppS
import Text.XHtml.Strict hiding(method)
import qualified Data.ByteString.Char8 as B
import qualified Data.ByteString.Lazy.Char8 as L
newtype XhtmlStrict = XhtmlStrict Text.XHtml.Strict.Html
instance ToMessage XhtmlStrict where
toContentType _ = B.pack "application/xhtml+xml"
toMessage (XhtmlStrict html) = (L.pack . renderHtml) html
main = do
simpleHTTP [method GET $ response1]
getLine
response1 = ok $ XhtmlStrict $
(header $ thetitle << "My First Page")
+++
(body $ h1 << "Hello, World!")
That's it for now.
6. CREDITS
Lemmih from #happs@freenode for providing assistance.