Skip to content
Create a gist now

Instantly share code, notes, and snippets.

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
A simple example to go with my post on Compojure.
(ns cello-world.routes
(:require [compojure.route :as route]
[compojure.core :as cc]
[ring.util.response :as rur])
(:use [ring.adapter.jetty :only [run-jetty]]))
(cc/defroutes hello-routes
(cc/GET "/hello/name/" []
(fn [req]
(rur/response "<h1>Cello Vedang</h1>")))
(cc/GET "/hello/city/" []
(fn [req]
(rur/response "<h1>Cello from Pune!</h1>"))))
(cc/defroutes main-routes*
(cc/GET "/" [] (fn [req]
(rur/response "<h1>Cello World</h1>")))
(cc/GET "/bye/" [] (fn [req]
(rur/response "<h1>Goodbye World</h1>")))
(cc/GET "/hello*" [] hello-routes)
(route/not-found "<h1>Page not found</h1>"))
(defn verify-secret
"Verify that secret-key has been sent as a parameter s in the request"
(fn [request]
(if (= "s=please" (:query-string request))
(handler request)
{:status 403
:body "You don't know the secret word"})))
;;; Wrap my main routes in swathes of middleware
(def main-routes (-> #'main-routes*
(defonce ^:dynamic *server* nil)
(defn start-server
(let [server (run-jetty #'main-routes
{:port port
:join? false})]
(println "Started server on port -" port)
(alter-var-root #'*server* (constantly server))))
(defn stop-server
(.stop *server*))
(defn restart
(when (bound? #'*server*)
(start-server port))
;; to run the program
(start-server 5000)
;; The site should be accessible at
;; An example URL would be
;; to Stop the server
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.