How to convert from happs -> happstack?

Viewed 141

Can anyone help me "translate" the below from happs to happstack:

module Main where

import HAppS.Server.AlternativeHTTP
import HAppS.Server.HTTP.AltFileServe
import Control.Monad.State
import Numeric

import Contracts

instance FromData ExContr where
  fromData = do c    <- look "contract"
                arg1 <- look "arg1"
                arg2 <- look "arg2"
                img  <- look "image"
                return $ ExContr (c, map fst $ readFloat arg1
                                            ++ readFloat arg2, read img)

main :: IO ()
main = do simpleHTTP [dir "contractEx"
                        [withData $ \(ExContr t) ->
                           [anyRequest $ liftIO $ liftM toResponse =<< renderEx (ExContr t)]
                        ,anyRequest $ ok $ toResponse renderExDefault]
                     ,fileServe ["Contracts.html"] "public" -- fileserving


   ]

Contracts.hs contains:

newtype ExContr = ExContr (String, [Double], Bool) deriving (Read,Show,Eq)

renderEx :: ExContr -> IO Html
 renderEx exSpec@(ExContr (contractId, args, lattice)) =
   let pr = evalEx exSpec
       expValChart = if contractId == "probs" then noHtml -- expected value is meaningless for the probabilities it relies on
                     else h3 << "Expected value" +++ image ! [src (chartUrl $ expectedValuePr pr)]
       imageType = "png"
   in if useLatticeImage exSpec
      then do baseName <- mkUniqueName baseDotFilename
              exitCode <- latticeImage pr (webPath ++ tmpImgPath ++ baseName) imageType
              let pageContents =
                    case exitCode of
                      ExitSuccess -> renderExampleForm exSpec (image ! [src latticeUrl, border 1]) expValChart
                                      where latticeUrl = "/" ++ tmpImgPath ++ baseName ++ "." ++ imageType
                      _ -> p << "renderEx: error generating lattice image"
              return $ renderExamplePage pageContents
      else return $ renderExamplePage $ renderExampleForm exSpec (prToTable pr) expValChart

renderExDefault = renderExamplePage $
                 renderExampleForm (ExContr ("zcb", [fromIntegral t1Horizon, 10], True))
                                   noHtml noHtml

Alternatively I would like to understand how to install an old version of HappS compatible with the above code. Needless to say I am very new to Haskell.

1 Answers
Related