【问题标题】:How do I access the session in a compojure-api handler?如何在 compojure-api 处理程序中访问会话?
【发布时间】:2015-10-14 23:35:39
【问题描述】:

使用 compojure-api,如:

(defapi app
  (swagger-ui)
  (swagger-docs 
    {:info {:title "Sample api"}})

  (GET* "/" []
    :no-doc true
    (ok "hello world"))

  (context* "/api" []
    :tags ["thingie"]

    (GET* "/plus" []
      :return       Long
      :query-params [x :- Long, {y :- Long 1}]
      :summary      "x+y with query-parameters. y defaults to 1."
      (ok (+ x y)))))

如何访问响铃会话?

【问题讨论】:

  • 我建议检查 defapi 扩展的内容。尤其是 restructure 部分扩展为。

标签: clojure compojure-api


【解决方案1】:

基于此处的文档:https://github.com/metosin/compojure-api/blob/master/src/compojure/api/core.clj,defapi 是以下宏:

(defmacro defapi
  [name & body]
  `(def ~name
     (api ~@body)))

如您所见,它只是用api 宏调用的结果定义了 var(而api 创建了一个环处理程序)

所以你可以在没有 defapi 的情况下使用它,并包装环会话:

(def app
  (-> (api (swagger-ui)
           (swagger-docs 
             {:info {:title "Sample api"}})

           (GET* "/" []
             :no-doc true
             (ok "hello world"))

           (context* "/api" []
             :tags ["thingie"]

           (GET* "/plus" []
             :return       Long
             :query-params [x :- Long, {y :- Long 1}]
             :summary      "x+y with query-parameters. y defaults to 1."
             (ok (+ x y))
      ring.middleware.session/wrap-session))

我想在那之后你应该能够正常使用会话,如https://github.com/ring-clojure/ring/wiki/Sessions 中所述。没有测试过,但我认为这是正确的方法

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-16
    • 2014-10-23
    • 2014-08-27
    • 1970-01-01
    • 2015-11-17
    • 2017-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多