Creating the Application

To create the application we’ll open Eclipse and then select a new Leiningen project. We’ll set the project name to picture-gallery and change our profile from default to compojure-app. If needed, please refer to Chapter 1, Getting Your Feet Wet, for more details on how to complete this step. We’ll use PostgreSQL as our database, so we’ll need to add the necessary dependencies to our project. Once your project has been initialized, open the project.clj and add the postgresql, clojure.java.jdbc, and lib-noir dependencies.

picture-gallery-a/project.clj
 
[postgresql/postgresql ​"9.1-901.jdbc4"​]
 
[org.clojure/java.jdbc ​"0.2.3"​]
 
[lib-noir ​"0.8.2"​]

Since we’re using lib-noir, we have to add its middleware to our handler for it to work correctly. We’ll open the picture-gallery.handler namespace and add a few things to it. First we have to reference the libraries we’ll be using. We’ll do this by adding them to the :require section of our namespace definition, at the top.

 
(:require ​..​​.​ [noir.util.middleware :as noir-middleware])

We’ll use the app-handler middleware found in the noir.middleware namespace to set up the handler for our site. The app-handler will set up all the common middleware, such as session management, for us.

Let’s replace the current app definition with the following one.

 
(​def​ app (noir-middleware/app-handler [home-routes app-routes]))

We can now remove some of the references from our namespace, since they’re provided by the app-handler. Our namespace declaration should now look like the following.

 
(​ns​ picture-gallery.handler
 
(:require [compojure.route :as route]
 
[compojure.core :refer [defroutes]]
 
[noir.util.middleware :as noir-middleware]
 
[picture-gallery.routes.home :refer [home-routes]]))

With our handler set up, let’s navigate to the picture-gallery.repl namespace in the package explorer and run it. This should start up a REPL where you can run the start-server function to start the application. You should see the following output in the REPL after running (start-server).

 
;; Clojure 1.6.0
 
=> (start-server)
 
picture-gallery is starting
 
Started server on port 3000
 
You can view the site at http://localhost:3000

Once the application starts, a new browser window will open, pointing to localhost:3000 and showing a stock template home page like the one in the following figure.

images/screenshots/compojure_default.png

Figure 17. Default page
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.67.166