通过使用ring-clojure和Compojure编写一个简单的REST服务器和ring-json的wrap-json-body
中间件来学习Clojure已经有两天了。
包含用户(包含几个默认用户)的向量users
:
(def users [{:id 0 :username "aname"}
{:id 1 :username "anothername"}])
接受地图(用户)并查找具有相同用户名的现有用户的函数(表单?)save-user
。如果用户名可用,我会在返回HTTP 201之前覆盖users
向量以包含新用户。如果使用了用户名,我只返回HTTP 400:
(defn save-user [user]
(prn users)
(if
(not-any? #(= (:username %) (:username user)) users)
(fn [request]
(def users (conj users user))
(status
(response (str "Saved user with username: " (:username user)))
201))
(status
(response (str "User with username '" (:username user) "' already exists"))
400)))
POST /users
使用收到的地图调用save-user
的路线:
(defroutes app-routes
(POST "/users" request (save-user (:body request))))
我认为这不重要,但中间件应用如下:
(def app
(-> app-routes
(wrap-cors :access-control-allow-origin "*" :access-control-allow-methods "*")
(wrap-json-response)
(wrap-keyword-params)
(wrap-json-body {:keywords? true :bigdecimals? true})
(wrap-defaults (assoc site-defaults :security false))))
无论出于何种原因,整个request
地图都会传递给then
内if
传递的函数。打印它:
(if
(not-any? #(= (:username %) (:username user)) users)
(fn [request]
(prn request))
...
...给我这个:
{:ssl-client-cert nil, :cookies {}, :remote-addr "0:0:0:0:0:0:0:1", :params {}, :flash nil, :route-params {}, :headers {"host" "localhost:3000", "accept" "*/*", "content-length" "42", "content-type" "application/json", "user-agent" "curl/7.43.0"}, :server-port 3000, :content-length 42, :form-params {}, :compojure/route [:post "/users"], :session/key nil, :query-params {}, :content-type "application/json", :character-encoding nil, :uri "/users", :server-name "localhost", :query-string nil, :body {:username "testusername", :password "testpassword"}, :multipart-params {}, :scheme :http, :request-method :post, :session {}}
如果我将匿名函数作为if else
传递,也会发生同样的情况。但是,当我只传递(status ...)
时,没有任何错误,就像上面的代码一样。
根据我的理解,request
地图根本不应在save-user
内可用,因为它不作为参数传递。为什么它传递给我的匿名函数,有没有办法简单地忽略它?
答案 0 :(得分:2)
在save-user
中,您将返回一个函数,它接受请求并返回响应(处理程序)。
我想您应该直接返回响应。只需将(fn [request]
替换为(do
即可将多个表达式包装在一个表单中。
(def users (atom [{:id 0 :username "aname"}
{:id 1 :username "anothername"}]))
(defn save-user [user]
(if (not-any? #(= (:username %) (:username user)) @users)
(do
(swap! users conj user)
(status
(response (str "Saved user with username: " (:username user)))
201))
(status
(response (str "User with username '" (:username user) "' already exists"))
400)))
我还将全局用户var更改为原子。在Clojure中重新定义函数内部的全局变量是一个很大的禁忌。