<?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>experimentalworks &#187; lisp</title>
	<atom:link href="http://blog.experimentalworks.net/tag/lisp/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.experimentalworks.net</link>
	<description></description>
	<lastBuildDate>Fri, 27 Jan 2012 10:07:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Dealing with Sessions in Compojure</title>
		<link>http://blog.experimentalworks.net/2010/02/dealing-with-sessions-in-compojure/</link>
		<comments>http://blog.experimentalworks.net/2010/02/dealing-with-sessions-in-compojure/#comments</comments>
		<pubDate>Sat, 27 Feb 2010 10:46:31 +0000</pubDate>
		<dc:creator>dsp</dc:creator>
				<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://blog.experimentalworks.net/?p=407</guid>
		<description><![CDATA[The blog post gives you a short introduction into the session management of Compojure.]]></description>
			<content:encoded><![CDATA[<p>I recently started working with <a href="http://compojure.org">Compojure</a>, a <a href="http://en.wikipedia.org/wiki/Web_Application_Framework">web framework</a> for programming language <a href="http://clojure.org">Clojure</a>. After playing around with basic GET and POST requests I tried to store and load data from a session. As Compojure is quiet new, there is not much documentation how to deal with sessions, particularly as Compojure and it&#8217;s API still changes fast.</p>
<p>In the following post I&#8217;ll show a brief example on how to create a login page with Compojure 0.3.2 running on Clojure 1.1.0.</p>
<p><span id="more-407"></span></p>
<h4>What is Clojure?</h4>
<p>Clojure is a <a href="http://en.wikipedia.org/wiki/Lisp_(programming_language)">Lisp</a> dialect. So it&#8217;s a programming language based on the principles of Lisp. The interesting part about Clojure is that it runs on the <a href="http://en.wikipedia.org/wiki/Java_Virtual_Machine">Java Virtual Machine</a>. As a developer you can therefore use the bright variety of Java written libraries in your code without touching one line of <a href="http://en.wikipedia.org/wiki/Java_(programming_language)">Java</a>. Use the power of Java libraries together with the power of Lisp.</p>
<h4>What is Compojure?</h4>
<p>Compojure is a web framework for Clojure. It handles <a href="http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol">incoming request</a>, offers an easy way to create <a href="http://java.sun.com/products/servlet/">servlets</a> and provides basic functionality for <a href="http://en.wikipedia.org/wiki/HTML">HTML</a> templates. The current stable version 0.3.2. Compojure is a young project and things change fast there, so be aware that a 0.3.2 application might not run on 0.4.</p>
<h4>What is a session?</h4>
<p>Basically it&#8217;s a per-user store managed by the server. For example a server backend can store the credentials of a user that it received in a request in this store making it possible to identify the user again when he or she does a second request. Usually this functionality is achieved by a library or by the interpreter itself (for example in <a href="http://php.net">PHP</a>) and is implemented by storing the session data either in memory or on disk together with a unique identifier that is then stored in a cookie at the users computer. So when the user comes back, the cookie is read and the identifier is used to find the data for the user.</p>
<h3>The application</h3>
<h4>A basic example</h4>
<p>Before we work with a session, let&#8217;s create a simple webpage that displays hello world for us.<br />
<script src="http://gist.github.com/316604.js"></script></p>
<p>The example is straight forward. We define a set of <em>routes</em>. In our example the route is &#8220;/*&#8221;, so it matches everything. The route then takes a function as a second argument. In our case we use the templating mechanism of Compojure and create a simple HTML response with the <font face="monospace">(html)</font> macro.</p>
<p>We then start the server using the <font face="monospace">(run-server)</font> macro and binding our routes to a specified mountpoint. I won&#8217;t go into details about servlets but usually it is sufficient if you have a servlet running on the mountpoint &#8220;/*&#8221; and then a set of routes.</p>
<p>To run the server you can use a small script like this one:<br />
<code><br />
#!/bin/sh<br />
CLASSPATH=.:~/dev/clojure/compojure.jar:~/dev/clojure/clojure.jar<br />
java -cp $CLASSPATH clojure.lang.Script net/experimentalworks/clojure/serve.clj<br />
</code></p>
<h4>Adding sessions</h4>
<p>Before we can start adding sessions we have to be aware that Compojure has a mechanism called <em>decorating</em>. So you can decorate a set or routes with what Compojure calls a middleware. We will think of it as a way to enable advanced mechanism. To add the session mechanism to a set of routes use the <font face="monospace">(decorate)</font> macro as shown in the following code:</p>
<p><script src="http://gist.github.com/316609.js"></script></p>
<p>Note the that we define a session decoration. At the moment sessions only support in-memory storage (which by the way should be usually the way you want to manage sessions). The <font face="monospace">:memory</font> keyword here is to tell Compojure to store it&#8217;s sessions in memory. Although you can omit the keyword, I prefer writing it down so people can easily figure out how our session is stored.</p>
<h4>Creating a login</h4>
<p>Let&#8217;s create a login. We will first add a new route called <font face="monospace">/login/:user</font>. This will tell Compojure that the last part of the query string is a parameter and it should extract it. We can then access the value using <font face="monospace">(:user params)</font>. We use the <font face="monospace">(session-assoc)</font> function to associate our username with the session-key <font face="monospace">:loggedin</font>. Here is what the code looks like:<br />
<script src="http://gist.github.com/316614.js"></script></p>
<p>You can then &#8220;login&#8221; to your site using <font face="monospace">http://localhost:8080/login/test</font> and you Compojure will recognize you again if you go back to <font face="monospace">http://localhost:8080/</font></p>
<h4>Logout</h4>
<p>To logout, we clear the key itself. Let&#8217;s a have a look directly into the code. We use the <font face="monospace">(session-dissoc)</font> function to drop the key (it seems that clear-session is broken in my <a href="http://github.com/weavejester/compojure">Compojure checkout</a> for whatever reasons..need to debug *debug*).<br />
<script src="http://gist.github.com/316623.js"></script><br />
Use <font face="monospace">http://localhost:8080/logout</font> to get rid of your session.</p>
<h4>Conclusion</h4>
<p>The basic idea behind Compojure sessions is simple and I hope you were able to grasp the idea a little bit. What can be annoying and what I didn&#8217;t show you is that you might run into problems within the actual implementation, although the basic stuff should work fine. Once you figured out the basics you can read and write sessions withing seconds and the code becomes clean and beautiful. Feel free to comment on this article if you like the post, it&#8217;s the only measurement for me that I can use to make sure I&#8217;m not writing stupid stuff.</p>
<p class="wp-flattr-button"></p> <p><a href="http://blog.experimentalworks.net/?flattrss_redirect&amp;id=407&amp;md5=c5813cff4a835393efe1fea8b95d7f2d" title="Flattr" target="_blank"><img src="http://blog.experimentalworks.net/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.experimentalworks.net/2010/02/dealing-with-sessions-in-compojure/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Clojure, and it&#8217;s not a typo.</title>
		<link>http://blog.experimentalworks.net/2010/02/clojure-and-its-not-a-typo/</link>
		<comments>http://blog.experimentalworks.net/2010/02/clojure-and-its-not-a-typo/#comments</comments>
		<pubDate>Wed, 03 Feb 2010 11:17:24 +0000</pubDate>
		<dc:creator>dsp</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[sonar]]></category>

		<guid isPermaLink="false">http://blog.experimentalworks.net/?p=393</guid>
		<description><![CDATA[
Why lisp can be sooooo sweet. A clojure class extending java.


compile it

(set! *compile-path* "build")
(compile 'net.experimentalworks.clojure.outdegree)



Ah yeah and hsv2rgb transformation:
]]></description>
			<content:encoded><![CDATA[<p>Why lisp can be sooooo sweet. A clojure class extending java.<br />
<script src="http://gist.github.com/294201.js"></script></p>
<p>compile it</p>
<pre>
(set! *compile-path* "build")
(compile 'net.experimentalworks.clojure.outdegree)
</pre>
<p>Ah yeah and hsv2rgb transformation:<br />
<script src="http://gist.github.com/294152.js"></script></p>
<p class="wp-flattr-button"></p> <p><a href="http://blog.experimentalworks.net/?flattrss_redirect&amp;id=393&amp;md5=18829467e846238680ab1209ca3e3f9f" title="Flattr" target="_blank"><img src="http://blog.experimentalworks.net/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.experimentalworks.net/2010/02/clojure-and-its-not-a-typo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

