<?xml version="1.0" encoding="UTF-8" ?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc=" http://purl.org/dc/elements/1.1/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>briancarper.net (λ) (Tag: SLIME)</title><link>http://briancarper.net/tag/48/slime</link><description>Some guy's blog about programming and Linux and cows.</description><item><title>Deploying Clojure websites</title><link>http://briancarper.net/blog/deploying-clojure-websites</link><guid>http://briancarper.net/blog/deploying-clojure-websites</guid><pubDate>Mon, 04 Jan 2010 20:42:17 -0800</pubDate><description>&lt;p&gt;On my server I'm running one Java process, which handles four of my websites on four different domains.  These are all running on Clojure + Compojure.  Some people asked for details of how to do this, so here's a rough outline.  For the sake of brevity I'm only going to talk about two domains here, though it scales up to however many you want pretty easily.&lt;/p&gt;

&lt;p&gt;This is surely not the only way to do this, and probably not the best way, but it's what I've arrived at after a year of goofing off.&lt;/p&gt;

&lt;p&gt;Summary: Emacs + SLIME + Clojure running in GNU Screen; all requests are handled by Apache and &lt;code&gt;mod_proxy&lt;/code&gt; sends them to the appropriate Jetty instance / servlet.&lt;/p&gt;

&lt;!--more Lots of fun details follow --&gt;

&lt;h1&gt;Directory structure&lt;/h1&gt;

&lt;p&gt;First you have to decide on a directory structure.  There's a sort of Clojure convention to have a &lt;code&gt;src&lt;/code&gt; and &lt;code&gt;deps&lt;/code&gt; directory, though Clojure is so young that this convention may or may not stick.  Behold my ASCII line-art directory tree:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;~/public_html/base/
|
|----src/
|    |
|    |----common/
|    |
|    |----net/
|         |
|         |----briancarper/
|         |    |
|         |    |----&amp;lt;lots of .clj files&amp;gt;
|         |
|         |----ffclassic/
|              |
|              |----&amp;lt;lots of .clj files&amp;gt;
|
|----deps/
|    |
|    |----&amp;lt;lots of .jar files&amp;gt;
|
|----briancarper.net/
|    |
|    |----public/
|    |
|    |----db/
|    |
|    |----apache/
|
|----ffclassic.net/
|    |
|    |----public/
|    |
|    |----apache/
|
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;src&lt;/code&gt; holds the Clojure source code for all of my sites.  Java (therefore Clojure) forces your directory structure to match your namespaces, and traditionally people use domain names in reverse order for both.  Whether you like or dislike this convention, you must admit that if you're writing code that's actually going to be used to host websites on domains, the convention probably makes sense.  There are some utility libraries I wrote that I like to share between sites, so I put those in some &lt;code&gt;common&lt;/code&gt; namespace in their own directory.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;deps&lt;/code&gt; holds a bunch of &lt;code&gt;.jar&lt;/code&gt; files.  This includes &lt;code&gt;clojure.jar&lt;/code&gt;, &lt;code&gt;clojure-contrib.jar&lt;/code&gt;, &lt;code&gt;compojure.jar&lt;/code&gt;, &lt;code&gt;swank-clojure.jar&lt;/code&gt; (for SLIME), and all the Java libraries I use, like Jetty, a bunch of Apache Commons libraries, Tokyo Cabinet, Rhino and so on.  This means that all of my sites are sharing libraries, and therefore must be running the same version of Clojure and all other libraries.  So when I upgrade a library that one site is using, I must upgrade all the other sites.  This is sometimes a hassle, but it forces me not to be lazy.&lt;/p&gt;

&lt;p&gt;Then I have one more directory for each of my domains, for static files and such.  These directories have different subdirectories depending on how I'm running them.  For the sites that use Tokyo Cabinet, I have a &lt;code&gt;db&lt;/code&gt; directory to hold the database files.  On some sites I want to run a PHP or CGI script, so I have a directory for Apache to use.  All of them have a &lt;code&gt;public&lt;/code&gt; folder, from which Clojure serves images, JS and CSS files etc.&lt;/p&gt;

&lt;p&gt;Phil Hagelberg's recent &lt;a href=&quot;http://github.com/technomancy/leiningen&quot;&gt;Leiningen project&lt;/a&gt; can possibly help with some of this.  I don't use Lein but I may switch someday.&lt;/p&gt;

&lt;h1&gt;Emacs + SLIME + CLASSPATH setup&lt;/h1&gt;

&lt;p&gt;Building or installing Clojure and Emacs and SLIME and friends is beyond the scope of this blog post.  &lt;a href=&quot;http://github.com/technomancy/swank-clojure&quot;&gt;swank-clojure&lt;/a&gt; has come a long way in being auto-installable via ELPA nowadays, if you like.&lt;/p&gt;

&lt;p&gt;The most important thing is to set up &lt;code&gt;CLASSPATH&lt;/code&gt; correctly.  &lt;code&gt;src&lt;/code&gt; should be on the &lt;code&gt;CLASSPATH&lt;/code&gt;, as well as every &lt;code&gt;.jar&lt;/code&gt; file in &lt;code&gt;deps&lt;/code&gt;.  My &lt;code&gt;.emacs&lt;/code&gt; contains this, among other things:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(setq swank-clojure-classpath (list &quot;.&quot; &quot;./src&quot; &quot;./deps/*&quot;))
(setq swank-clojure-library-paths (list &quot;/usr/local/lib&quot;))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Java will take a glob and expand it to include every jar file in a directory, thankfully.  But this seems to happen right when you start Java, so to add a new &lt;code&gt;.jar&lt;/code&gt; to your &lt;code&gt;CLASSPATH&lt;/code&gt; you generally need to restart the JVM.  This is annoying, but oh well.&lt;/p&gt;

&lt;p&gt;If you AOT-compile your Clojure code you might also have a &lt;code&gt;classes&lt;/code&gt; folder on your classpath, but I don't bother.  You could also compile your whole site into its own &lt;code&gt;.jar&lt;/code&gt; file and use that.  I don't do this because I want to be able to edit my Clojure source files on the server if I need to. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;swank-clojure-library-paths&lt;/code&gt; is necessary for me because Tokyo Cabinet uses some C libraries.  You might not need it.&lt;/p&gt;

&lt;p&gt;Then when I start Emacs, I &lt;code&gt;M-x cd&lt;/code&gt; to the base folder and start SLIME there.  It'll find Clojure in &lt;code&gt;deps&lt;/code&gt; and give me a REPL.  If you can get this working, you're well on your way.&lt;/p&gt;

&lt;h1&gt;Clojure code&lt;/h1&gt;

&lt;p&gt;I have a file &lt;code&gt;server.clj&lt;/code&gt; for each domain, which are in charge of setting up and starting / stopping the servlets.  So in &lt;code&gt;src/net/briancarper/server.clj&lt;/code&gt; I have something like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(ns net.briancarper.server
  (:use (compojure.http request servlet session routes)
        (compojure.server jetty)
        (compojure control)
        (net.briancarper ...)))  ;; lots of files containing the guts of the site

(defroutes some-routes ...)
(defroutes other-routes ...)

(defroutes all-routes
  some-routes
  other-routes)

(defserver blog-server
  {:port 8080 :host &quot;localhost&quot;}
  &quot;/*&quot; (servlet all-routes))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;src/net/ffclassic/server.clj&lt;/code&gt; is similar, except with different routes (obviously) and a different port for the servlet.  Note that we bind to &lt;code&gt;localhost&lt;/code&gt; here, so that people can't connect directly to port 8080 and bypass Apache.&lt;/p&gt;

&lt;p&gt;One of the routes should be set up to point to the proper &lt;code&gt;public&lt;/code&gt; folder.  Generally some catch-all route near the end of your list of routes, like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(GET &quot;/*&quot; (static-file (params :*)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;where this &lt;code&gt;static-file&lt;/code&gt; knows to serve files from &lt;code&gt;briancarper.net/public&lt;/code&gt;, and the other &lt;code&gt;server.clj&lt;/code&gt; for the other domains point to different directories.&lt;/p&gt;

&lt;p&gt;Compojure has the &lt;code&gt;compojure.http.helpers/serve-file&lt;/code&gt; function you can use to serve static files too.  I use my own function for various reasons.  Note that Compojure generally doesn't set any HTTP headers on your responses.  You should probably set some kind of cache control headers unless you want your users to re-download all of your image files and stylesheets every time they hit a new page.  You might also have to fiddle with &lt;code&gt;Content-Type&lt;/code&gt; sometimes, though Compojure is usually pretty good about guessing them.&lt;/p&gt;

&lt;p&gt;Note that Compojure's hands-off approach has its upsides too.  For example, I have a bunch of CSS files for my blog.  In Clojure, I read and concatenate all of those CSS files into one blob of text and have Clojure serve that as &lt;a href=&quot;http://briancarper.net/combined.css&quot;&gt;http://briancarper.net/combined.css&lt;/a&gt;.  &lt;code&gt;combined.css&lt;/code&gt; doesn't actually exist.  I do the same for JS.  This speeds up user requests quite a bit, without having to merge all the files on the filesystem.  I could further compactify them if I cared.&lt;/p&gt;

&lt;p&gt;If your Clojure app is handling arbitrary request URIs, you should be careful you aren't serving up &lt;code&gt;../../../../etc/passwd&lt;/code&gt; or something.  Pretty sure Apache protects you against this by default, but Compojure uses &lt;code&gt;compojure.http.helpers/safe-path?&lt;/code&gt; to test for it too if you use the built-in &lt;code&gt;serve-file&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Finally, for convenience, in the base directory I have a file called &lt;code&gt;server.clj&lt;/code&gt; which contains this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(ns server
  (:require [net.briancarper server]
            [net.ffclassic server]))

(defn go []
  (.start net.briancarper.server/blog-server)
  (.start net.ffclassic.server/ffclassic-server))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then, to start everything, from a REPL:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;user&amp;gt; (require 'server)
2010-01-04 18:10:50.046::INFO:  Logging to STDERR via org.mortbay.log.StdErrLog
net.briancarper.server.proxy$javax.servlet.http.HttpServlet$0
net.ffclassic.server.proxy$javax.servlet.http.HttpServlet$0
nil
user&amp;gt; (server/go)
2010-01-04 18:11:02.260::INFO:  jetty-6.1.15
2010-01-04 18:11:02.358::INFO:  Started SocketConnector@localhost:8080
2010-01-04 18:11:02.707::INFO:  jetty-6.1.15
2010-01-04 18:11:02.789::INFO:  Started SocketConnector@localhost:8087
nil
user&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;h1&gt;On the server (Apache)&lt;/h1&gt;

&lt;p&gt;To deploy this to my server I just &lt;code&gt;rsync&lt;/code&gt; it all over SSH.  On the server, I have Emacs and Java installed, as well as Apache and GNU Screen.&lt;/p&gt;

&lt;p&gt;Why Apache?  In case I want to run something that isn't Clojure, like webmail or some CGI script.  Plus it's easy to run Clojure as a non-priviledged user on a non-priviledged port, and have Apache forward requests to the proper servlet based on the domain in the request.  Apache is also nice for doing HTTPS.&lt;/p&gt;

&lt;p&gt;My Apache setup is a pretty standard Debian-ish setup.  You need &lt;code&gt;mod_proxy&lt;/code&gt; installed.  My Apache config for one domain looks a bit like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;VirtualHost *:80&amp;gt;
        ServerName ffclassic.net
        ServerAlias www.ffclassic.net

        &amp;lt;Directory /home/user/clj/ffclassic.net/apache/&amp;gt;
           AllowOverride All
           Order Allow,Deny
           Allow from all
        &amp;lt;/Directory&amp;gt;

        DocumentRoot /home/user/clj/ffclassic.net/apache/

        ProxyPass /foo !
        ProxyPass / http://localhost:8087/
        ProxyPassReverse / http://localhost:8087/
&amp;lt;/VirtualHost&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Yeah, that's about it.  By default, every request will be passed to Clojure.  The ports specified here should obviously match the ports you specify in your Clojure code for your servlets.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ProxyPass /foo !&lt;/code&gt; tells Apache to handle requests to &lt;code&gt;http://ffclassic.net/foo&lt;/code&gt; itself rather than pass them to Clojure.  I override URIs on a case-by-case basis like this, to tell Apache to run whatever CGI or PHP scripts I need.&lt;/p&gt;

&lt;p&gt;One oddity of running a setup like this is that thanks to &lt;code&gt;mod_proxy&lt;/code&gt;, the &quot;remote address&quot; for every request is always going to be &lt;code&gt;127.0.0.1&lt;/code&gt; by the time they get to Clojure.  So if you want to see the IP address of your users, you have to do something like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(defn- ip [request]
  (or ((:headers request) &quot;x-forwarded-for&quot;)
      (:remote-addr request)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Another oddity is that sometimes if Apache is running, and then I start Clojure, I have to restart Apache before it &quot;finds&quot; Clojure and starts forwarding traffic.  No big deal though.&lt;/p&gt;

&lt;p&gt;This is otherwise transparent from the Clojure end.&lt;/p&gt;

&lt;h1&gt;Starting it up&lt;/h1&gt;

&lt;p&gt;You probably want to SSH to your server, start a REPL, start your code running, and then log off the server.  There are various ways you can run keep Emacs running in the background.  You could run Emacs in server mode and attach to it with emacsclient.  I use GNU Screen because it's easy and I have other things running in Screen instances that I like to switch between.  So I start Screen, start Emacs, start SLIME, and start my site from there.  Then detach from Screen when I'm done.  It's easier than it sounds.&lt;/p&gt;

&lt;p&gt;Why SLIME and not a normal commandline REPL?  Because in Emacs I can patch the code easily on a function-by-function basis, among other things.  You can do this from a commandline REPL but not nearly as easily.  Emacs is a much nicer place to type than any command line.  When I need to make an update to the site or fix a bug, my workflow is typically:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Fix it locally on my test machine, make sure it works&lt;/li&gt;
&lt;li&gt;rsync the changed files to the server&lt;/li&gt;
&lt;li&gt;SSH to the server&lt;/li&gt;
&lt;li&gt;&lt;code&gt;screen -r&lt;/code&gt; to bring up Emacs&lt;/li&gt;
&lt;li&gt;Open the &lt;code&gt;.clj&lt;/code&gt; file(s) I changed&lt;/li&gt;
&lt;li&gt;&lt;code&gt;C-c C-c&lt;/code&gt; to recompile and reload the individual function(s) I changed, or &lt;code&gt;C-c C-k&lt;/code&gt; to recompile and load a whole &lt;code&gt;.clj&lt;/code&gt; file, or whatever.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;C-a d&lt;/code&gt; to detach from Screen&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I can do any database inspecting or fiddling I need to do from the REPL.  I can also do some live testing of the site or whatever else I want.  It's good for debugging.&lt;/p&gt;

&lt;p&gt;I've had Emacs running for months this way without having to restart it, it seems to work OK.&lt;/p&gt;

&lt;h1&gt;That's it&lt;/h1&gt;

&lt;p&gt;It's not that hard.  It's clearly more steps than dumping some PHP files into a directory and having them work.  But with Clojure, you get the benefit of a persistently running environment with a nice REPL interface, all in a nice fast multi-threaded JVM.  Plus you get to write all your code in Clojure instead of PHP.  In terms of fun and long-term maintenance costs, Clojure comes out way ahead in the end, for me anyways.&lt;/p&gt;</description></item><item><title>Clojure, SLIME, ODBC, SQL Server</title><link>http://briancarper.net/blog/clojure-slime-odbc-sql-server</link><guid>http://briancarper.net/blog/clojure-slime-odbc-sql-server</guid><pubDate>Fri, 26 Jun 2009 11:27:22 -0700</pubDate><description>&lt;p&gt;I had a lot of trouble connecting to an MS SQL Server at work via Clojure.  Java 6 comes with a JDBC-ODBC bridge which worked fine from a Clojure REPL at a command prompt, or from inferior-lisp in Emacs, but in SLIME it would hang every time I tried to connect and I'd have to kill Java.  Couldn't for the life of me figure out why.&lt;/p&gt;

&lt;p&gt;I got it to work eventually by using Microsoft's own JDBC driver, which you can download &lt;a href=&quot;http://msdn.microsoft.com/en-us/data/aa937724.aspx&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Once you put the downloaded .jar file on your &lt;code&gt;CLASSPATH&lt;/code&gt; (in my case, &lt;code&gt;sqljdbc4.jar&lt;/code&gt;) you can connect like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;user&amp;gt; (def db {:classname &quot;com.microsoft.sqlserver.jdbc.SQLServerDriver&quot;
               :subprotocol &quot;sqlserver&quot;
               :subname &quot;//server_hostname;database=SomeDatabase;user=SomeUser;password=SomePassword&quot;})
#'user/db
user&amp;gt; (use 'clojure.contrib.sql)
nil
user&amp;gt; (with-connection db 
        (with-query-results rs [&quot;SELECT * FROM whatever&quot;] (prn rs)))
... results ...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Posted for the sake of Googlebot and for my own future sanity.&lt;/p&gt;</description></item><item><title>Practical Common Lisp (review)</title><link>http://briancarper.net/blog/practical-common-lisp-review</link><guid>http://briancarper.net/blog/practical-common-lisp-review</guid><pubDate>Tue, 18 Sep 2007 14:24:37 -0700</pubDate><description>&lt;p&gt;My current &quot;programming language of the week&quot; is Common Lisp.  I got a dead-tree hard copy of &lt;a href=&quot;http://gigamonkeys.com/book/&quot;&gt;Practical Common Lisp&lt;/a&gt; as a going-away present from my last job.  This week on a trip to Eugene, Oregon (as part of the process of getting my NEXT job, hopefully) I had lots of time to read through it.  I &quot;finished&quot; it today, for some definition of &quot;finish&quot; which includes a &quot;skipped over large chunks of chapters that were way over my head&quot; clause.&lt;/p&gt;

&lt;p&gt;I was amazed to learn from &lt;a href=&quot;http://gigamonkeys.com/blog/2007/05/26/pcl-third-printing.html&quot;&gt;Peter Seibel's blog&lt;/a&gt; that as of May 2007, there were less than 10,000 copies sold.  I think I am really clouded in my view of what kinds of things people know in the world at large.  I know there must be some overwhelmingly large group of Windows-only programmers who only know C# and/or Java and nothing else, but perhaps because I live in the world of Linux and open source, I never come in contact with them.  When I look around me (figuratively, online) I see Linux guys who know Linux and Perl and Ruby and *nix-style C++ and whatnot.  So I often find it very difficult to extrapolate anything about the world at large based on what I see around me.&lt;/p&gt;

&lt;p&gt;Either way, PCL is a good book.  It has an immense amount of information in it.  Almost overwhelmingly large amounts of information, somehow.  It may be overwhelming simply because of how unfamiliar many of the concepts and terminologies are to one such as myself who doesn't come from a Lisp background.  But there are also many things that I'm 100% sure I wouldn't understand at all if I didn't already know Ruby pretty well (largely because Ruby ganked so much stuff from Lisp).  If I didn't have a Ruby background, I'd be even more lost than I was.&lt;/p&gt;

&lt;p&gt;Whether Lisp is really practical or not, I'm not sure.  For example, it takes a lot of housekeeping even to get a Lisp environment set up.  Thankfully available on Peter's website is a &lt;a href=&quot;http://gigamonkeys.com/book/lispbox/&quot;&gt;pre-packaged &quot;Lisp in a Box&quot;&lt;/a&gt; that can at least get you to a REPL prompt with no effort.  But then you have to start writing code.  Lisp code can be compiled, or interpreted, or a combination of both(?), or both simultaneously(?).  Or it can be typed manually into the REPL, and then run, or compiled, or dumped to a file.  Or you can dump an image of the Lisp instance (image?) to file entirely.  Often there's a mess of manual dependency-managing any time you use packages.  And then there are special things you need to do to make sure certain kinds of macros and things load correctly when compiled and run from compiled form, or when saved to file in &quot;readable&quot; form and then re-read as lisp code at a later time.  And there are certain things in Lisp that can't be output in &quot;readable&quot; format at all.  Do I sound confused?  I surely am.  I found &lt;a href=&quot;http://xach.livejournal.com/130040.html&quot;&gt;this writeup&lt;/a&gt; showing how one person gets started on a Lisp project, which is somewhat helpful.&lt;/p&gt;

&lt;p&gt;Yeah, you can get SLIME for Emacs and then start banging away in the REPL, but losing everything when you close it isn't a lot of fun.  So I'm getting by with typing stuff into an Emacs buffer and manually running top-level forms one at a time via a bunch of Emacs shortcuts.  It still seems like I'm doing a lot of the work that my environment should be doing for me.  But I suppose it's no worse than trying to set up a bunch of Makefiles to manage C++ code.  And much of my trouble seems to be a product of my inexperience (but that can probably be said of anything).&lt;/p&gt;

&lt;p&gt;Lisp does have a lot of things that are really interesting, like multi-methods and of course macros, which the book covers in a wonderful amount of detail.  Even the exception/condition system is different (and arguably much more powerful) than other languages'.  The Lisp language itself is simply more powerful than most other languages in many ways, is the only conclusion I can come to.  It's more extensible and it gives you more options for abstracting things in ways that are impossible to do (easily) in other languages due to the syntax and available constructs the languages provide.  At the same time, all this power comes at the expense of code that's nearly unreadable by my somewhat biased standards; nasty Perl-esque punctuation abounds, there are many inconsistencies in how things are called or passed parameters (often for &quot;historical reasons&quot;, the bane of my existence), and given the ability of macros to drastically alter the way code looks and works and to define mini-languages inside Lisp itself, you're very often not quite sure what exactly you're looking at at any given moment.&lt;/p&gt;

&lt;p&gt;But I highly recommend the book.  For non-Lispers like myself, if nothing else it gives you a taste of language that's very different from probably anything you've seen before, and it can point you in many interesting directions you may never have thought possible.  On the other hand, is Lisp a revolutionary Zen-like enlightenment-producing revelation of a language which will expand your programmer mind into vast new dimensions of coding efficiency and bliss?  I have yet to take sip of the Kool-Aid required to answer that question, but who knows.&lt;/p&gt;</description></item></channel></rss>

