<?xml version="1.0"?>
<rss version="2.0"
     xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:dcterms="http://purl.org/dc/terms/" >
<channel>
<title>Pylons - Justin&#x27;s Ramblings</title>
<link>http://bouncybouncy.net//ramblings/tags/pylons/</link>
<description>BB.Net</description>
<item>
	
	<title>dynamic ikiwiki pages</title>
	
	<guid>http://bouncybouncy.net//ramblings/posts/dynamic_ikiwiki_pages/</guid>
	<link>http://bouncybouncy.net//ramblings/posts/dynamic_ikiwiki_pages/</link>
	
	
	<category>tags/ikiwiki</category>
	
	<category>tags/meta</category>
	
	<category>tags/pylons</category>
	
	<category>tags/python</category>
	
	<category>tags/tech</category>
	
	
	<pubDate>Fri, 15 Feb 2008 20:57:58 -0500</pubDate>
	<dcterms:modified>2008-02-16T18:36:26Z</dcterms:modified>
	
	<description><![CDATA[<p>The static pages that <a href="http://ikiwiki.info">ikiwiki</a>
generates are great, but I want to have some dynamic content here
as well.</p>
<p>If this works, this page should include the servers uptime.</p>
<!--# include virtual="/dyn/demo/uptime" -->
<p>yay <img src="http://bouncybouncy.net//ramblings/tags/pylons/../../../smileys/smile.png" alt=":-)" /></p>
<p>So how does that work?</p>
<p>first configure nginx as follows</p>
<div class="syntax">
<pre>
server {
    listen       80;
    server_name  bouncybouncy.net  *.bouncybouncy.net web;

    location / {
        root   /home/justin/bbdotnet/static/;
        index  index.html index.htm;
        ssi on;
    }
    location /dyn {
        # All POST requests go to pylons directly
        include /usr/local/nginx/conf/proxy.conf;
        proxy_redirect  default; 
        if ($request_method = POST) {
            proxy_pass  http://127.0.0.1:5000;
            break;
        }
        default_type text/html; 

        set $memcached_key "$uri";
        memcached_pass localhost:11211;

        proxy_intercept_errors  on;

        # If no info would be found in memcache or memecache would be dead, go to real dynamic location
        error_page 404 502 = @dynamic_request;
    }
    location @dynamic_request{
        # This means, that we can't get to this location from outside - only by internal redirect
        internal;

        include /usr/local/nginx/conf/proxy.conf;
        proxy_redirect  default; 
        proxy_pass  http://127.0.0.1:5000;
    }

}

</pre></div>
<p>Pylons is setup to run on port 5000 as usual, nothing fancy
there.</p>
<p>Then anywhere we want some dynamic content we can simply do</p>
<div class="syntax">
<pre>
&lt;!--# include virtual="/dyn/demo/uptime" --&gt;

</pre></div>
<p>For now, you have to disable the htmlscrubber plugin for this to
work. There is probably a better solution. I think this would
simply involve a plugin that could run after htmlscrubber to insert
the include, then you would only need to have something like
[[include virtual="/dyn/demo/uptime"]] in your pages.</p>
<p>If you did not mind requring javscript, you could use <a href=
"http://www.mnot.net/javascript/hinclude/">HInclude</a> instead of
SSI.</p>
<p>To keep things running fast, we enable to caching on the pylons
controller. using a modified version of the beaker<em>cache
decorator. The following lines are inserted at the end of the
create</em>func method, which causes the page result to be cached
in memcache as well as in beaker.</p>
<div class="syntax">
<pre>
url = pylons.request.path_info
<span class="synStatement">if</span> pylons.request.params:
    url += "<span class=
"synConstant">?</span>" + pylons.request.environ['<span class=
"synConstant">QUERY_STRING</span>']

mc = memcache.Client(['<span class=
"synConstant">localhost</span>'])
mc.set(url, result, cache_expire)

</pre></div>
<p>The only remaining problem I see is a small race condition. If
the cache expires, and 20 concurrent requests all come in for the
page, most of them will end up hitting python instead of waiting
for the memcache key to appear. This might actually work better
using varnish or apache2 with <code>mod_disk_cache</code>, but the
last time I tried I could not get varnish to work at all, and
apache2 (I think) still does not support PURGE.</p>

]]></description>
	
</item>

</channel>
</rss>

