<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Tupil Code Blog &#187; library</title>
	<atom:link href="http://blog.tupil.com/tag/library/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.tupil.com</link>
	<description>(Get up early, code often)</description>
	<lastBuildDate>Fri, 27 Aug 2010 10:50:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Running Happstack applications with FastCGI</title>
		<link>http://blog.tupil.com/running-happstack-applications-with-fastcgi/</link>
		<comments>http://blog.tupil.com/running-happstack-applications-with-fastcgi/#comments</comments>
		<pubDate>Sun, 19 Apr 2009 14:20:46 +0000</pubDate>
		<dc:creator>Eelco Lempsink</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[FastCGI]]></category>
		<category><![CDATA[hackathon]]></category>
		<category><![CDATA[Happstack]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[library]]></category>

		<guid isPermaLink="false">http://blog.tupil.com/?p=57</guid>
		<description><![CDATA[This weekend  the 5th Haskell Hackathon happened.  It was hosted by Utrecht University and organized by people from the university and us. Together with Galois and Microsoft Research we sponsored the event. Between running around, listening to interesting presentations and talking to cool people we were able to code up a module enabling us to [...]]]></description>
			<content:encoded><![CDATA[<p>This weekend  the <a href="http://haskell.org/haskellwiki/Hac5">5th Haskell Hackathon</a> happened.  It was hosted by <a href="http://www.uu.nl">Utrecht University</a> and organized by people from the university and us. Together with <a href="http://www.galois.com">Galois</a> and <a href="http://research.microsoft.com">Microsoft Research</a> we sponsored the event.</p>
<p>Between running around, listening to interesting presentations and talking to cool people we were able to code up a module enabling us to use <a href="http://www.happstack.com">Happstack</a> through <a href="http://www.fastcgi.com">FastCGI</a>. This scratches an itch we&#8217;ve had for some time: we&#8217;re running a couple of HAppS/Happstack servers on our server but they&#8217;re configured to work through mod_proxy, which means having to choose an unique port for each application and doing (re)starting the servers by hand.</p>
<p>We just released <a href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/happstack-fastcgi">happstack-fastcgi on Hackage</a>. This post gives an example of how to use it with Apache and mod_fastcgi.  We assume you configured the extension &#8216;.fcgi&#8217; for FastCGI scripts.</p>
<h2>Converting your Happstack code</h2>
<p>Let&#8217;s look at a &#8216;normal&#8217; Happstack application. Usually, it has a <code>main</code> function that looks something like this&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">import</span> Happstack<span style="color: #339933; font-weight: bold;">.</span>Server
&nbsp;
main <span style="color: #339933; font-weight: bold;">=</span> simpleHTTP nullConf server
&nbsp;
<span style="color: #339933; font-weight: bold;">...</span></pre></div></div>

<p>Where the &#8220;server&#8221; part is something of the type <code>ServerPart a</code>.  To make this work with FastCGI, we need to convert the server parts so that they talk FastCGI.</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">import</span> Happstack<span style="color: #339933; font-weight: bold;">.</span>Server
<span style="color: #06c; font-weight: bold;">import</span> Happstack<span style="color: #339933; font-weight: bold;">.</span>Server<span style="color: #339933; font-weight: bold;">.</span>FastCGI
&nbsp;
simpleCGI <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span>ToMessage a<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=&gt;</span> ServerPartT <span style="color: #cccc00; font-weight: bold;">IO</span> a <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">IO</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>
simpleCGI <span style="color: #339933; font-weight: bold;">=</span> runFastCGIConcurrent <span style="color: red;">10</span> <span style="color: #339933; font-weight: bold;">.</span> serverPartToCGI
&nbsp;
main <span style="color: #339933; font-weight: bold;">=</span> simpleCGI server
&nbsp;
<span style="color: #339933; font-weight: bold;">...</span></pre></div></div>

<p>Assuming the above code is placed in a script called Main.hs, you can compile it with</p>
<pre>
ghc -threaded --make Main.hs -o main.fcgi
</pre>
<p>Notice the &#8216;-threaded&#8217;, because else we won&#8217;t be able to serve request concurrently (other than having FastCGI start an extra instance of our program, which we don&#8217;t want when we&#8217;re working with happstack-state, for example).</p>
<p>So, let&#8217;s fill something in for <code>server</code>, for example</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">server <span style="color: #339933; font-weight: bold;">=</span> withRequest <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">return</span> <span style="color: #339933; font-weight: bold;">.</span> <span style="font-weight: bold;">show</span></pre></div></div>

<p>This will simple dump the request as it has been translated from a CGI request to a Happstack request.</p>
<h2>Running it with Apache</h2>
<p>Just for reference, the <a href='http://www.fastcgi.com/drupal/node/25'>FastCGI documentation</a> is your friend, but he might be a bit long-winded, so I will do a short recap of the basic things.</p>
<p>Let&#8217;s first do the easy thing. If you set up FastCGI correctly and made it the handler for .fcgi scripts, you can just throw the .fcgi file (like we just compiled) anywhere within your document root (provided that the ExecCGI option is enabled for that directory) and go to the correct URL with your browser.</p>
<p>Using scripts in this way is called &#8216;dynamic&#8217; in FastCGI-speak. With a bit more configuration (unfortunately on server/virtual host config level) we can run the script &#8216;statically&#8217;, which means it will be started when Apache is started and (by default) gets one process.  This is particularly important when using happstack-state, things might very well go wrong otherwise. To set up a &#8216;static&#8217; FastCGI script, you must use FastCGI&#8217;s <code>FastCgiServer</code> configuration directive.</p>
<p>Make sure you restart Apache when you change the script (or use FastCGI&#8217;s autoreload setting, which introduces a bit of overhead but is much more convenient).</p>
<h2>Nice URLs and serving static files</h2>
<p>Now you have the FastCGI file running, you might want to do something about the URLs, because doing requests to http://example.com/main.fcgi/some/url doesn&#8217;t really look that nice. As an added advantage, you can now use Apache to serve your static files, reducing the work your Happstack server needs to do.</p>
<p>The traditional .htaccess trickery looks something like this:</p>
<pre>
Options ExecCGI
DirectoryIndex main.fcgi

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) main.fcgi/$1 [PT,L]
</pre>
<p>(If your application is running in a subdirectory, you also need the <code>RewriteBase</code> configuration directive.)</p>
<h2>Closing words</h2>
<p>We did some really simple benchmarks that show that using Happstack through FastCGI, either dynamic or static, is about as fast as using Happstack directly or even mod_proxy (which is pretty fast, but a hassle).  That means the overhead is (as expected) quite small.</p>
<p>At first, we wanted to demo it with Gitit, but unfortunately Gitit or some library that uses Gitit tries to get the PATH environment variable and gives an error when it doesn&#8217;t succeed&#8230; So, if your Happstack application doesn&#8217;t (seem to) work with FastCGI, keep an eye on your logs. </p>
<p>Patches and bugreports are very welcome. You can clone <a href="http://github.com/chriseidhof/happstack-fastcgi">the repository</a> from Github, enjoy.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tupil.com/running-happstack-applications-with-fastcgi/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Stemming with Haskell</title>
		<link>http://blog.tupil.com/stemming-with-haskell/</link>
		<comments>http://blog.tupil.com/stemming-with-haskell/#comments</comments>
		<pubDate>Mon, 14 Jul 2008 14:55:41 +0000</pubDate>
		<dc:creator>Eelco Lempsink</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[Snowball]]></category>
		<category><![CDATA[stemmer]]></category>

		<guid isPermaLink="false">http://blog.tupil.com/?p=22</guid>
		<description><![CDATA[Last week we worked on building a small search engine with Haskell. As you might know, when searching you&#8217;ll need some index you&#8217;ll search and possibly stemming to allow people to search for variants of a word and still come up with accurate results. Fortunately for us, there are already good libraries and tools out [...]]]></description>
			<content:encoded><![CDATA[<p>Last week we worked on building a small search engine with Haskell. As you might know, when searching you&#8217;ll need some <em>index</em> you&#8217;ll search and possibly <a href='http://en.wikipedia.org/wiki/Stemming'>stemming</a> to allow people to search for variants of a word and still come up with accurate results.</p>
<p>Fortunately for us, there are already good libraries and tools out there to help us. So instead of trying to write everything from scratch, we made a small library based on <a href='http://snowball.tartarus.org/'>Snowball&#8217;s libstemmer_c</a> and a very (very!) rough start of a <a href='http://www.sphinxsearch.com/'>Sphinx</a> client (more about that in a later post).</p>
<p>We&#8217;ve released the library on <a href='http://hackage.haskell.org/'>Hackage</a> so check out <a href='http://hackage.haskell.org/cgi-bin/hackage-scripts/package/stemmer'>stemmer 0.1</a></p>
<p>A small code example to give you a taste&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">module</span> Main <span style="color: #06c; font-weight: bold;">where</span>
&nbsp;
<span style="color: #06c; font-weight: bold;">import</span> <span style="color: #06c; font-weight: bold;">qualified</span> NLP<span style="color: #339933; font-weight: bold;">.</span>Stemmer <span style="color: #06c; font-weight: bold;">as</span> Stemming
<span style="color: #06c; font-weight: bold;">import</span> Control<span style="color: #339933; font-weight: bold;">.</span><span style="color: #cccc00; font-weight: bold;">Monad</span> <span style="color: green;">&#40;</span>unless<span style="color: green;">&#41;</span>
<span style="color: #06c; font-weight: bold;">import</span> System<span style="color: #339933; font-weight: bold;">.</span><span style="color: #cccc00; font-weight: bold;">IO</span> <span style="color: green;">&#40;</span>hSetBuffering<span style="color: #339933; font-weight: bold;">,</span> stdout<span style="color: #339933; font-weight: bold;">,</span> BufferMode<span style="color: green;">&#40;</span>NoBuffering<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
&nbsp;
main <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">IO</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>
main <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span>
    stemmer <span style="color: #339933; font-weight: bold;">&lt;-</span> Stemming<span style="color: #339933; font-weight: bold;">.</span>new Stemming<span style="color: #339933; font-weight: bold;">.</span>English
    <span style="font-weight: bold;">putStrLn</span> <span style="">&quot;Enter a sentence to stem, an empty line to stop.&quot;</span>
    hSetBuffering stdout NoBuffering <span style="color: #5d478b; font-style: italic;">-- to print a prompt</span>
    stemUserInput stemmer
    Stemming<span style="color: #339933; font-weight: bold;">.</span>delete stemmer
&nbsp;
stemUserInput <span style="color: #339933; font-weight: bold;">::</span> Stemming<span style="color: #339933; font-weight: bold;">.</span>Stemmer <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">IO</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>
stemUserInput stemmer <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span>
    <span style="font-weight: bold;">putStr</span> <span style="">&quot;&gt; &quot;</span>
    string <span style="color: #339933; font-weight: bold;">&lt;-</span> <span style="font-weight: bold;">getLine</span>
    unless <span style="color: green;">&#40;</span>string <span style="color: #339933; font-weight: bold;">==</span> <span style="">&quot;&quot;</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="color: #06c; font-weight: bold;">do</span> 
        string' <span style="color: #339933; font-weight: bold;">&lt;-</span> <span style="font-weight: bold;">mapM</span> <span style="color: green;">&#40;</span>Stemming<span style="color: #339933; font-weight: bold;">.</span>stem stemmer<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">words</span> string
        <span style="font-weight: bold;">putStrLn</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="">&quot;&lt; &quot;</span> <span style="color: #339933; font-weight: bold;">++</span> <span style="font-weight: bold;">unwords</span> string'
        stemUserInput stemmer</pre></div></div>

<p>Save this to Main.hs and then do something like<br />
<code><br />
$ ghc --make Main.hs -o stemmer<br />
[1 of 1] Compiling Main             ( Main.hs, Main.o )<br />
Linking stemmer ...<br />
$ ./stemmer<br />
Enter a sentence to stem, an empty line to stop.<br />
> The fishes worked forever with their fins<br />
< The fish work forev with their fin<br />
> Stemming with Haskell<br />
< Stem with Haskel<br />
</code></p>
<p>It was pretty easy to implement this library and also a nice exercise in using <a href='http://www.cse.unsw.edu.au/~chak/haskell/ffi/'>Haskell's Foreign Function Interface</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tupil.com/stemming-with-haskell/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>
