Hi Erik,<br>yes you're right, I'm experimenting with an IORef now (a TVar would be a better idea).<br>I also tried the idea expressed here, to keep the state:<br><a href="https://groups.google.com/forum/?fromgroups=#!msg/happs/_JSpaJKub0k/oa0K01IBlh0J" target="_blank">https://groups.google.com/forum/?fromgroups=#!msg/happs/_JSpaJKub0k/oa0K01IBlh0J</a><br>
But without success so far. The server is returning me the state in the Response type but I cannot manage to reinject it for the next call :)<br><br>Best,<br>Corentin<br><br><div class="gmail_quote">On Thu, Aug 30, 2012 at 9:47 PM, Erik Hesselink <span dir="ltr"><<a href="mailto:hesselink@gmail.com" target="_blank">hesselink@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">The way you wrote it, you run the state transformer once for each<br>
request. So the state will be available within a single request, but<br>
not between requests. If you want to persist state between requests,<br>
you can use one the the mutable variables available (TVar or MVar are<br>
good choices; IORefs also work, but you have to take care about<br>
concurrency, using e.g. atomicModifyIORef). Create them before calling<br>
simpleHTTP, then pass them in to use them in your handler. You can put<br>
them in a ReaderT to avoid passing them, if you want.<br>
<br>
Another choice is to use something like a database or the file system<br>
for persistent state. Databases usually handle most concurrency<br>
problems for you, while file systems don't.<br>
<br>
Regards,<br>
<br>
Erik<br>
<div><div><br>
On Thu, Aug 30, 2012 at 7:29 PM, Corentin Dupont<br>
<<a href="mailto:corentin.dupont@gmail.com" target="_blank">corentin.dupont@gmail.com</a>> wrote:<br>
> Hi all,<br>
> I'm trying to make a web server that manages its own state. The user can<br>
> issue commands that modifies the state.<br>
> I did like below but unfortunatly the state is not keep after a command is<br>
> issued...<br>
> What is the right way to do it? Is there any example sites with an internal<br>
> state with happstack?<br>
><br>
> data Game = (the state of my game)<br>
> type NomicServer = ServerPartT (StateT Game IO)<br>
><br>
> launchWebServer :: Game -> IO ()<br>
> launchWebServer initialState = do<br>
> putStrLn "Starting web server...\nTo connect, drive your browser to<br>
> \"<a href="http://localhost:8000/Login%5C" target="_blank">http://localhost:8000/Login\</a>""<br>
> d <- getDataDir<br>
> simpleHTTP' unpackStateT nullConf $ server d<br>
><br>
><br>
> server :: FilePath -> ServerPartT (StateT Game IO) Response<br>
> server d sh = mconcat [fileServe [] d, do<br>
> decodeBody (defaultBodyPolicy "/tmp/"<br>
> 4096 4096 4096)<br>
> html <- implSite "<a href="http://localhost:8000/" target="_blank">http://localhost:8000/</a>"<br>
> "" nomicSite<br>
> return $ toResponse html]<br>
><br>
> unpackStateT:: Game -> UnWebT (StateT Game IO) Response -> UnWebT IO<br>
> Response<br>
> unpackStateT g w = evalStateT w g<br>
><br>
> --handler for web routes<br>
> nomicSite :: Site PlayerCommand (NomicServer Html)<br>
> nomicSite = setDefault (Noop 0) Site {<br>
> handleSite = \f url -> unRouteT (routedNomicCommands url) f<br>
> , formatPathSegments = \u -> (toPathSegments u, [])<br>
> , parsePathSegments = parseSegments fromPathSegments<br>
> }<br>
><br>
> Thanks a lot,<br>
> Corentin<br>
</div></div>> _______________________________________________<br>
> Haskell-Cafe mailing list<br>
> <a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
> <a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
><br>
</blockquote></div><br>