[Haskell-cafe] happstack simpleHTTP & state monad

Erik Hesselink hesselink at gmail.com
Thu Aug 30 21:47:29 CEST 2012


The way you wrote it, you run the state transformer once for each
request. So the state will be available within a single request, but
not between requests. If you want to persist state between requests,
you can use one the the mutable variables available (TVar or MVar are
good choices; IORefs also work, but you have to take care about
concurrency, using e.g. atomicModifyIORef). Create them before calling
simpleHTTP, then pass them in to use them in your handler. You can put
them in a ReaderT to avoid passing them, if you want.

Another choice is to use something like a database or the file system
for persistent state. Databases usually handle most concurrency
problems for you, while file systems don't.

Regards,

Erik

On Thu, Aug 30, 2012 at 7:29 PM, Corentin Dupont
<corentin.dupont at gmail.com> wrote:
> 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
> issued...
> 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
> \"http://localhost:8000/Login\""
>    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
> Response
> 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,
> Corentin
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>



More information about the Haskell-Cafe mailing list