Compojure

In my last article, I started discussing Compojure, a Web framework written
in the Clojure language. Clojure already has generated a great deal
of excitement among software developers, in that it combines the
beauty and expressive elegance of Lisp with the efficiency and
ubiquity of the Java Virtual Machine (JVM). Clojure has other traits
as well, including its famous use of software transactional memory
(STM) to avoid problems in multithreaded environments.

As a Web developer and a longtime Lisp aficionado, I've been
intrigued by the possibility of writing and deploying Web applications
written in Clojure. Compojure would appear to be a simple framework
for creating Web applications, built on lower-level systems, such as
"ring", which handles HTTP requests.

In my last article, I explained how to create a simple Web
application using the "lein" system, modify the project.clj
configuration file and determine the HTML returned in response to a
particular URL pattern ("route"). Here, I try to advance
the application somewhat, looking at the things that are typically of
interest to Web developers. Even if you don't end up using Clojure or
Compojure, I still think you'll learn something from understanding
how these systems approach the problem.

Databases and Clojure

Because Clojure is built on the JVM, you can use the same objects in
your Clojure program as you would in a Java program. In other words, if
you
want to connect to a PostgreSQL database, you do so with the same JDBC
driver that Java applications do.

Installing the PostgreSQL JDBC driver requires two steps. First, you
must download the driver, which is available at
http://jdbc.postgresql.org. Second, you then must tell the JVM
where it can find the classes that are defined by the driver. This is
done by setting (or adding to) the CLASSPATH environment variable—that is, put the driver in:

export CLASSPATH=/home/reuven/Downloads:$CLASSPATH

Once you have done that, you can tell your Clojure project that you want
to include the PostgreSQL JDBC driver by adding two elements to the
:dependencies vector within the
defproject macro:

Now you just need to connect to the database, as well as interact with
it. Assuming you have created a database named "cjtest" on
your
local PostgreSQL server, you can use the built-in Clojure REPL (lein
repl) to talk to the database. First, you need to load the database
driver and put it into an "sql" namespace that will allow you to work
with the driver:

(require '[clojure.java.jdbc :as sql])

Then, you need to tell Clojure the host, database and port to which you
want to connect. You can do this most easily by creating a
"db" map to
build the query string that PostgreSQL needs:

With this in place, you now can issue database commands. The easiest
way to do so is to use the with-connection macro inside the
"sql" namespace, which connects using the driver and then lets
you
issue a command. For example, if you want to create a new table
containing a serial (that is, automatically updated primary key) column
and a text column, you could do the following:

Databases and Compojure

Now that you know how to do basic database operations from the Clojure
REPL, you can put some of that code inside your Compojure
application. For example, let's say you want to have an
appointment calendar. For now, let's assume that there already is a
PostgreSQL "appointments" databases defined:

You'll now want to be able to go to /appointments in your Web application
and see the current list of appointments. To do this, you need to
add a route to your Web application, such that it'll invoke a function
that then goes to the database and retrieves all of those elements.

Before you can do so, you need to load the PostgreSQL JDBC driver into
your Clojure application. You can do this most easily in the
:require
section of your namespace declaration in handler.clj:

Remember that in a functional language like Clojure, the idea is to
get the results from the database and then process them in some way,
handing them off to another function for display (or further
processing). The above function produces HTML output, using the
Hiccup HTML-generation system. Using Hiccup, you easily can create (as
in the above function) an H1 headline, followed by a "ul" list.

The real magic happens in the call to
sql/with-query-results. That
function puts the results of your database call in the
rs variable.
You then can do a number of different things with that resultset. In
this case, let's turn each record into an "li" tag in the final
HTML. The easiest way to do that is to apply a function to each
element of the resultset. In Clojure (as in many functional
languages), you do this with the map function, which transforms a
collection of items into a new collection of equal length.

What does the format-appointment function do? As you can imagine, it
turns an appointment record into HTML:

In other words, you'll treat the record as if it were a hash and then
retrieve the elements (keys) from that hash using Clojure's shorthand
syntax for doing so. You wrap that up into HTML, and then you can display
it for the user. The advantage of decomposing your display
functionality into two functions is that you now can change the way in
which appointments are displayed, without modifying the main function
that's called when /appointments is requested by the user.

Comment viewing options

Fully in keeping with the tradition of the brand, the name of the Veneno originates from a legendary fighting bull. Veneno is the name of one of the Tractor Work Lights strongest and most aggressive fighting bulls ever.

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.