如何在ring处理程序中最方便地注入状态(而不使用全局变量)?
以下是一个示例:
(defroutes main-routes
(GET "/api/fu" [] (rest-of-the-app the-state)))
(def app
(-> (handler/api main-routes)))
我希望将the-state
传递给main-routes
的compojure handler中。该状态可能类似于使用以下代码创建的map:
我希望将the-state
传递给main-routes
的Compojure处理程序。该状态可能类似于使用以下代码创建的地图:
(defn create-app-state []
{:db (connect-to-db)
:log (create-log)})
在非Ring应用程序中,我会在主函数中创建状态,并将其作为函数参数注入到应用程序的不同组件中,或者是它们的部分。
在不使用全局变量的情况下,能否使用Ring的:init
函数实现类似的功能?