Hi Erik,<br>yes you&#39;re right, I&#39;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">&lt;<a href="mailto:hesselink@gmail.com" target="_blank">hesselink@gmail.com</a>&gt;</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&#39;t.<br>
<br>
Regards,<br>
<br>
Erik<br>
<div><div><br>
On Thu, Aug 30, 2012 at 7:29 PM, Corentin Dupont<br>
&lt;<a href="mailto:corentin.dupont@gmail.com" target="_blank">corentin.dupont@gmail.com</a>&gt; wrote:<br>
&gt; Hi all,<br>
&gt; I&#39;m trying to make a web server that manages its own state. The user can<br>
&gt; issue commands that modifies the state.<br>
&gt; I did like below but unfortunatly the state is not keep after a command is<br>
&gt; issued...<br>
&gt; What is the right way to do it? Is there any example sites with an internal<br>
&gt; state with happstack?<br>
&gt;<br>
&gt; data Game = (the state of my game)<br>
&gt; type NomicServer             = ServerPartT (StateT Game IO)<br>
&gt;<br>
&gt; launchWebServer :: Game -&gt; IO ()<br>
&gt; launchWebServer initialState = do<br>
&gt;    putStrLn &quot;Starting web server...\nTo connect, drive your browser to<br>
&gt; \&quot;<a href="http://localhost:8000/Login%5C" target="_blank">http://localhost:8000/Login\</a>&quot;&quot;<br>
&gt;    d &lt;- getDataDir<br>
&gt;    simpleHTTP&#39; unpackStateT nullConf $ server d<br>
&gt;<br>
&gt;<br>
&gt; server :: FilePath -&gt; ServerPartT (StateT Game IO) Response<br>
&gt; server d sh = mconcat [fileServe [] d, do<br>
&gt;                                    decodeBody (defaultBodyPolicy &quot;/tmp/&quot;<br>
&gt; 4096 4096 4096)<br>
&gt;                                    html &lt;- implSite &quot;<a href="http://localhost:8000/" target="_blank">http://localhost:8000/</a>&quot;<br>
&gt; &quot;&quot; nomicSite<br>
&gt;                                    return $ toResponse html]<br>
&gt;<br>
&gt; unpackStateT:: Game -&gt; UnWebT (StateT Game IO) Response -&gt; UnWebT IO<br>
&gt; Response<br>
&gt; unpackStateT g w = evalStateT w g<br>
&gt;<br>
&gt; --handler for web routes<br>
&gt; nomicSite :: Site PlayerCommand (NomicServer Html)<br>
&gt; nomicSite = setDefault (Noop 0) Site {<br>
&gt;       handleSite         = \f url -&gt; unRouteT (routedNomicCommands url) f<br>
&gt;     , formatPathSegments = \u -&gt; (toPathSegments u, [])<br>
&gt;     , parsePathSegments  = parseSegments fromPathSegments<br>
&gt; }<br>
&gt;<br>
&gt; Thanks a lot,<br>
&gt; Corentin<br>
</div></div>&gt; _______________________________________________<br>
&gt; Haskell-Cafe mailing list<br>
&gt; <a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
&gt; <a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
&gt;<br>
</blockquote></div><br>