Created
April 25, 2015 16:32
-
-
Save orther/1e25a807050e0a1c081a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 about [req] | |
{:status 200 | |
:body "My name is Brandon Orther. I am following along with LispCast's Intro to Web Development with Clojure." | |
:headers {}}) | |
(defn goodbye [req] | |
{:status 200 | |
:body "Goodbye, cruel world!" | |
:headers {}}) | |
(defn greet [req] | |
{:status 200 | |
:body "Hello, World!" | |
:headers {}}) | |
(defn yo [req] | |
(let [name (get-in req [:route-params :name])] | |
{:status 200 | |
:body (str "Yo! " name "!!")})) | |
(def ops | |
{"+" + | |
"-" - | |
"*" * | |
":" /}) | |
(defn calc [req] | |
(let [a (Integer. (get-in req [:route-params :a])) | |
b (Integer. (get-in req [:route-params :b])) | |
op (get-in req [:route-params :op]) | |
f (get ops op)] | |
(if f | |
{:status 200 | |
:body (str (f a b)) | |
:headers {}} | |
{:status 404 | |
:body (str "Unknown operator: " op) | |
:headers {}}))) | |
(defroutes app | |
(GET "/" [] greet) | |
(GET "/about" [] about) | |
(GET "/calc/:a/:op/:b" [] calc) | |
(GET "/goodbye" [] goodbye) | |
(GET "/request" [] handle-dump) | |
(GET "/yo/:name" [] yo) | |
(not-found "Page not found.")) | |
(defn -main [port] | |
(jetty/run-jetty app {:port (Integer. port)})) | |
(defn -dev-main [port] | |
(jetty/run-jetty (wrap-reload #'app) {:port (Integer. port)})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment