
Developing and Deploying a Simple Clojure Web Application - liebke
http://mmcgrana.github.com/2010/07/develop-deploy-clojure-web-applications.html
======
mahmud
In Common Lisp ( _without_ cheating or handwaving; this is _it_ ):

    
    
      (defpackage :add-nums
        (:use :cl :hunchentoot :cl-who))
      
      (in-package :add-nums)
      
      (defmacro with-html (&body body)
        `(with-html-output-to-string (*standard-output* nil :prologue t :indent t)
           ,@body))
      
      (define-easy-handler (add-nums :uri "/add-nums")
          ((a :parameter-type 'integer)
           (b :parameter-type 'integer))
        (with-html
          (:html
           (:head (:title "Add two numbers"))
           (:body
            (if (and a b)
              (htm (:p (str (+ a b)))))
            (:form :method :get
             (:input :type :text :name "a")
             (:input :type :text :name "b")
             (:input :type :submit))))))
      
      (defparameter *site* (make-instance 'acceptor :port 8080))
      
      (start *site*)

~~~
weavejester
The equivalent Clojure code is actually a few lines shorter than your example:

    
    
        (ns add-nums
          (:use compojure.core [hiccup core page-helpers] ring.adapter.jetty))
    
        (defroutes handler
          (GET "/add-nums" [a b]
            (let [a (Integer/parseInt a)
                  b (Integer/parseInt b)]
              (html
                [:html
                  [:head [:title "Add two numbers"]]
                  [:body
                    (if (and a b) [:p (+ a b)])
                    (form-to [:get "/add-nums"]
                      (text-field :a)
                      (text-field :b)
                      (submit-button))]])))
    
        (run-jetty handler {:port 8080})
    

Mark's tutorial code is a little more complete than your example, and doesn't
aim for brevity. It also doesn't use any of the form functions from Hiccup
(admittedly, they're not as well documented as they could be).

~~~
mahmud
Finally, an actual programmer's response.

Thank you! It's good to know that verbosity was for the sake pedagogy and
completeness, and not necessity.

------
pietrofmaggi
Another nice example can be found in the last "Functional Web" column on IEEE
Internet Computing by Steve Vinosky: [http://steve.vinoski.net/pdf/IC-
Getting_Started_with_Google_...](http://steve.vinoski.net/pdf/IC-
Getting_Started_with_Google_App_Engine_and_Clojure.pdf)

 _In the July/August 2010 column, Getting Started with Google App Engine and
Clojure, guest columnist Aaron Bedra shows how to use Clojure, a relatively
new but robust Lisp implementation on the Java Virtual Machine, create and
deploy an application using the Compojure web framework on the Google App
Engine platform._

~~~
TY
Great article, thanks for the link.

Couple more helpful links for some basics not covered in the article (all work
in progress):

Compojure docs: <http://www.compojure.org>

Stub for "Clojure Web Development" book:
[https://docs.google.com/Doc?docid=0AQqGP1CDN0uIZGhmZjJmcGZfM...](https://docs.google.com/Doc?docid=0AQqGP1CDN0uIZGhmZjJmcGZfMjNjNHIycGZu&hl=en)

------
seanwoods
This is a great tutorial. Over the past few months I've been struggling with
getting a "barebones" app up and running with Clojure. Leiningen has really
come into its own in 2010 and tutorials like this will be great for people who
need a basic template in order to bootstrap them into the whole experience.

------
blueberry
If this is an easy way to create a web app that adds two numbers on Clojure,
I'd say Clojure is not ready for web development yet.

~~~
swannodette
Huh. 45 LOC for templating, routing, and form processing and you don't need to
configure a different webserver since jetty is production quality. Do you have
links to something that takes less effort than what is being illustrated here
that can be deployed immediately into production and perform well?

~~~
Jach
<http://pastebin.com/PsecNBvB>

(To be honest, I think clojure has its purposes, but a one-off calculator page
isn't one of them. Use the right tool for the right job and all that.)

~~~
ataggart
What point are you trying to make? That one can write less code if one does
less stuff?

~~~
Jach
The parent asked for a link to something that takes less effort, I obliged. I
don't think the amount of code is a particularly useful metric in most cases,
but I would argue that the PHP+HTML solution is much clearer and easier to
deploy, and taking into account everything you wish about such a simple web
page (readability, maintainability, cross-server, security, etc.), takes less
effort in the end. (I don't have any big issues with jetty or tomcat, since I
use one or the other almost daily.) I'm sure there are larger use cases where
clojure is simpler than raw PHP, but that's why PHP has frameworks.

------
deweller
This is great information. I'd love to see a similar tutorial for NodeJS for
comparison.

------
ikbear
Clojure is cool.

