如何在 compojure 中绑定动态变量?请参阅下面的示例,这里的 request-id 是为每个 api 请求生成的唯一 uuid。我希望能够在后续的日志记录方法中访问此 request- id。我尝试使用绑定功能,但仍然无法在some-page/some-method
.
处理程序.clj
(ns some_app.handler
(:require
[compojure.api.sweet :refer :all]
[compojure.route :as route]
[some_api.some_page :as some-page]))
(def ^:dynamic *request-id*
nil)
(defn ^:private generate-request-id []
(str (java.util.UUID/randomUUID)))
(def app
(binding [*request-id* (generate-request-id)]
(api
(context "/api" [] (GET "/some-page" [] (some-page/some-method))))))
一些页面.clj
(ns some_app.some_page
(:require
[clojure.tools.logging :as log]))
(def some-method []
(log/info {:request-id *request-id*}))