Skip to content

Instantly share code, notes, and snippets.

@yosemitebandit
Created September 1, 2015 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yosemitebandit/8d12355c51d833fd714a to your computer and use it in GitHub Desktop.
Save yosemitebandit/8d12355c51d833fd714a to your computer and use it in GitHub Desktop.
a clojure webapp with ring for deploying on heroku
(ns webdev.core
(:require [ring.adapter.jetty :as jetty]
[ring.middleware.reload :refer [wrap-reload]]
[compojure.core :refer [defroutes GET]]
[compojure.route :refer [not-found]]
[ring.handler.dump :refer [handle-dump]]))
(defn greet [req]
{:status 200
:body "hi there!"
:headers {}})
(defn goodbye [req]
{:status 200
:body "peace!"
:headers {}})
(defn about [req]
{:status 200
:body "I'm just a little app made for clj practice"
:headers {}})
(defn yo [req]
{:status 200
:body (str "Yo " (:name (:route-params req)) "!")
:headers {}})
(def ops
{"+" + "-" -
"*" *
":" /})
(defn calc [req]
(let [operator (:operator (:route-params req))
a (Integer. (:a (:route-params req)))
b (Integer. (:b (:route-params req)))
f (get ops op)]
(if f
{:status 200
:body (str (f a b))
:headers {}}
{:status 404
:body "nerp"
:headers {}})))
(defroutes app
(GET "/" [] greet)
(GET "/goodbye" [] goodbye)
(GET "/about" [] about)
(GET "/request" [] handle-dump)
(GET "/yo/:name" [] yo)
(GET "/calc/:a/:operator/:b" [] calc)
(not-found "not found, sry!!"))
(defn -main [port]
(jetty/run-jetty app {:port (Integer. port)}))
; using the greet handler as a var here
; this way wrap-reload, when it refreshes the namespaces,
; will affect the value of greet
(defn -dev-main [port]
(jetty/run-jetty (wrap-reload #'app) {:port (Integer. port)}))
web: java $JVM_OPTS -cp target/webdev.jar clojure.main -m webdev.core $PORT
(defproject webdev "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.7.0"]
[ring "1.2.1"]
[compojure "1.1.6"]]
:min-lein-version "2.0.0"
:uberjar-name "webdev.jar"
:main webdev.core
; lein will automatically use this "dev profile"
; and this profile will call the wrap-reloaded server
:profiles {:dev
{:main webdev.core/-dev-main}})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment