未在 compojure 路由中使用的更新原子值
updated atom value not used in compojure route
我有一个 (routes (route/not-found))
定义,其值来自 (atom)
。虽然我已经更新了原子,但路由保留了初始值。这与 类似,但我很难理解 de/referenced 需要什么。
(ns mveroute
(:require
[org.httpkit.server :as srv]
[compojure.core :as cmpj]
[compojure.route :as route]
[clj-http.client :as client])
(:gen-class))
(def my-atom (atom "foobar"))
(def app
(cmpj/routes
(route/not-found {:status 400 :body @my-atom})))
(defn -main [& args]
(reset! my-atom "hello world")
(srv/run-server #'app {:port 8005})
;; "hello world" as expected
(println @my-atom)
;; still "foobar" but wanted "hello world"
(-> "http://localhost:8005"
(client/get {:throw-exceptions? false})
:body
println))
我想 warp-routes
可能会来救援。但不是我如何使用它。
(defn atom-body [] {:status 200 :body @my-atom})
(cmpj/defroutes wrap-found
(route/not-found (atom-body)))
(def app
(cmpj/wrap-routes #'wrap-found {}))
最终目标是一个简单的 cli 应用程序,可以使用命令行参数设置 resource/html 根目录。
尝试如下操作:
(defn not-found-fn
[req]
{:status 400 :body "not found again!"})
(def app
(cmpj/routes
(route/not-found not-found-fn)))
因此在 not-found-fn
中,您可以按照自己喜欢的方式构造 :body
字符串。您还可以将字符串存储在由 not-found-fn
.
取消引用的原子中
旁注:
请参阅以下内容以阐明何时应在代码中使用 Var 对象而不仅仅是符号:
我有一个 (routes (route/not-found))
定义,其值来自 (atom)
。虽然我已经更新了原子,但路由保留了初始值。这与
(ns mveroute
(:require
[org.httpkit.server :as srv]
[compojure.core :as cmpj]
[compojure.route :as route]
[clj-http.client :as client])
(:gen-class))
(def my-atom (atom "foobar"))
(def app
(cmpj/routes
(route/not-found {:status 400 :body @my-atom})))
(defn -main [& args]
(reset! my-atom "hello world")
(srv/run-server #'app {:port 8005})
;; "hello world" as expected
(println @my-atom)
;; still "foobar" but wanted "hello world"
(-> "http://localhost:8005"
(client/get {:throw-exceptions? false})
:body
println))
我想 warp-routes
可能会来救援。但不是我如何使用它。
(defn atom-body [] {:status 200 :body @my-atom})
(cmpj/defroutes wrap-found
(route/not-found (atom-body)))
(def app
(cmpj/wrap-routes #'wrap-found {}))
最终目标是一个简单的 cli 应用程序,可以使用命令行参数设置 resource/html 根目录。
尝试如下操作:
(defn not-found-fn
[req]
{:status 400 :body "not found again!"})
(def app
(cmpj/routes
(route/not-found not-found-fn)))
因此在 not-found-fn
中,您可以按照自己喜欢的方式构造 :body
字符串。您还可以将字符串存储在由 not-found-fn
.
旁注:
请参阅以下内容以阐明何时应在代码中使用 Var 对象而不仅仅是符号: