added simple compojure sample
This commit is contained in:
parent
996445ce4c
commit
c81718040a
2 changed files with 61 additions and 2 deletions
13
project.clj
13
project.clj
|
@ -3,7 +3,16 @@
|
|||
:url "http://example.com/FIXME"
|
||||
:license {:name "Eclipse Public License"
|
||||
:url "http://www.eclipse.org/legal/epl-v10.html"}
|
||||
:dependencies [[org.clojure/clojure "1.6.0"]
|
||||
:dependencies [
|
||||
|
||||
;; basic
|
||||
[org.clojure/clojure "1.6.0"]
|
||||
[ring/ring-core "1.3.2"]
|
||||
[ring/ring-devel "1.3.2"]
|
||||
[ring/ring-jetty-adapter "1.3.1"]])
|
||||
[ring/ring-jetty-adapter "1.3.1"]
|
||||
|
||||
;;compojure
|
||||
[compojure "1.3.1"]
|
||||
|
||||
|
||||
])
|
||||
|
|
50
src/meetup/compojure.clj
Normal file
50
src/meetup/compojure.clj
Normal file
|
@ -0,0 +1,50 @@
|
|||
(ns meetup.compojure
|
||||
(:require [ring.adapter.jetty :refer [run-jetty]]
|
||||
[ring.middleware.params :refer [wrap-params]]
|
||||
[ring.middleware.stacktrace :refer [wrap-stacktrace]]
|
||||
[clojure.walk :refer [keywordize-keys]]
|
||||
[compojure.core :refer [defroutes POST]]
|
||||
[compojure.route :as route]))
|
||||
|
||||
(defonce orders (atom {}))
|
||||
|
||||
(defn order-pizza-handler [params]
|
||||
(let [recipes #{:hawaian :italian :vegan}
|
||||
max-count 5
|
||||
p (keywordize-keys params)]
|
||||
|
||||
(if ((keyword(:recipe p)) recipes)
|
||||
(if-not (> (read-string (:count p)) max-count)
|
||||
(do (swap! orders into {(keyword (str (java.util.UUID/randomUUID))) p})
|
||||
{:status 200
|
||||
:body "your pizza will be available shortly"})
|
||||
{:status 404
|
||||
:body "you ordered too many pizzas"})
|
||||
|
||||
{:status 404
|
||||
:body "the requested recipe does not exists"})))
|
||||
|
||||
(defn cancel-pizza-handler [params]
|
||||
|
||||
@orders
|
||||
|
||||
(let [id (:id (keywordize-keys params))]
|
||||
(if-not (nil? (get @orders (keyword id)))
|
||||
(do (swap! orders dissoc (keyword id))
|
||||
{:status 200
|
||||
:body "your order has been canceled"})
|
||||
|
||||
{:status 200
|
||||
:body "your order does not exists"})))
|
||||
|
||||
(defroutes handler
|
||||
(POST "/order/pizza" {params :params} (order-pizza-handler params))
|
||||
(POST "/cancel/pizza" {params :params} (cancel-pizza-handler params))
|
||||
|
||||
(route/not-found "<h1>Invalid route.</h1>"))
|
||||
|
||||
(defonce server
|
||||
(run-jetty (-> #'handler
|
||||
(wrap-stacktrace)
|
||||
(wrap-params))
|
||||
{:port 3001 :join? false}))
|
Loading…
Reference in a new issue