[Haskell-cafe] happstack simpleHTTP & state monad

Corentin Dupont corentin.dupont at gmail.com
Thu Aug 30 19:29:20 CEST 2012

Hi all,
I'm trying to make a web server that manages its own state. The user can
issue commands that modifies the state.
I did like below but unfortunatly the state is not keep after a command is
What is the right way to do it? Is there any example sites with an internal
state with happstack?

*data Game = (the state of my game)
type NomicServer             = ServerPartT (StateT Game IO)

launchWebServer :: Game -> IO ()
launchWebServer initialState = do
   putStrLn "Starting web server...\nTo connect, drive your browser to \"
   d <- getDataDir
   simpleHTTP' unpackStateT nullConf $ server d

server :: FilePath -> ServerPartT (StateT Game IO) Response
server d sh = mconcat [fileServe [] d, do
                                   decodeBody (defaultBodyPolicy "/tmp/"
4096 4096 4096)
                                   html <- implSite "http://localhost:8000/"
"" nomicSite
                                   return $ toResponse html]

unpackStateT:: Game -> UnWebT (StateT Game IO) Response -> UnWebT IO
unpackStateT g w = evalStateT w g

--handler for web routes
nomicSite :: Site PlayerCommand (NomicServer Html)
nomicSite = setDefault (Noop 0) Site {
      handleSite         = \f url -> unRouteT (routedNomicCommands url) f
    , formatPathSegments = \u -> (toPathSegments u, [])
    , parsePathSegments  = parseSegments fromPathSegments

Thanks a lot,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20120830/0892d2c4/attachment.htm>

More information about the Haskell-Cafe mailing list