Fork me on GitHub

@giaosudau hi, you can mount a middleware that does the caching. Somethig like:

(context "/db" []
  :middleware [my-cache-mw]


@ikitommi can you recommend some cache libs that related? I just start clojure for 2 weeks. Still learning the language.


@giaosudau welcome to clojure! I invited you to the ring channel too, mr. Reeves (creator of Ring) hangs out there, might be a good place for tips too. There is a big list of useful libs in, I think we have rolled custom caching mw for projects that need those so not sure what is most relevant existing mw for that.


Also, you might want to look at the Luminus template, which has good production settings (including mw) and uses compojure-api, something like lein new luminus swag +swagger. It has great docs too.


i’m building an API with compojure-api where it’s a requirement that JSON responses are wrapped in a { body: { ... }, statusCode: 200, metadata: {} } structure. im new to clojure as a whole and cant quite figure out if i can put some middleware around compojure-api to easily do this? statusCode is the HTTP status code… any ideas?


I created a repo on github with http-kit , because lot of cases.. I read can’t compile with java.. so here is:


that work on http-kit.. with compojure-api etc.. if that is helpful for somebody..


@sb arttu's example you link to is 3 years old, the AOT problems have been most probably fixed in 1.0 or even before that


@juhoteperi yes, but I found just this repo and similar comments, therefore I dropped out.. my repo or maybe good to drop to official site a similar repo


(I’m not expert, I created for similar beginners)


good evening @ikitommi, regarding the issue about protecting the docs with custom validation, I've encountered another problem. here is the code i'm running

(defn authorized-for-docs? [handler]
  (fn [request]
    (if (= "42"
           (get (:query-params request) "secret"))
      (handler request)
       {:error "'secret' query parameter is invalid"}))))

(def app

   (ring/context "/docs" req
     :middleware [authorized-for-docs?]
       {:ui "/"
        :options {:ui {:swagger-docs "/docs/swagger.json"}}
        :spec "/swagger.json"
        :data {:info
               {:title " ... "
                :description "..."}}}))
the underlying issue is that when i hit /docs the browser redirects to /docs/index.html, but the second request lacks the auth param in the querystring so it gets blocked am I doing something wrong?


@plins yes, the query-parameters are not copied to the redirect. Same will be for the /docs/swagger.json that the UI will be asking as a separate query to get the actual spec - it will not have the query parameter. I think you should use something that the server sends for all requests - like basic-auth or session auth.


@victor.andree if you want to get the wrapped responses also to swagger docs, you have to modify both schemas and responses.


I think with the data-driven resource you could do that in more generic way.


with normal endpoints, something like:

(defn wrapped [schema]
  {:status Long, :body schema :meta {s/Any s/Any}})

(defn wrap [response meta]
  (let [body (-> response
                 (select-keys [:status :body])
                 (assoc :meta meta))]
    (assoc response :body body)))

(s/defschema User {:name String})

(def app
     {:ui "/"
      :spec "/swagger.json"}}

    (POST "/echo" []
      :return (wrapped User)
      :body [user User]
      (wrap (ok user) {:some "meta"}))))


(If I understood what you are trying to do).


that returns data in:

  "status": 200,
  "body": {
    "name": "string"
  "meta": {
    "some": "meta"


that’s the body.


i was trying to find a way to do this in one place — all non-error responses should basically have this structure. so i started looking into restructuring the meta :return — but that doesn’t take care of wrapping the normal response.


@ikitommi, ive successfully managed to protect the docs with basic auth, and i'd like to post a snippet on the wiki for n00bs like me. here is what ive managed to do

(defn authorized-for-docs? [handler]
   (fn [request]
     (let [auth-header (get (:headers request) "authorization")]
       (prn auth-header)
         (nil? auth-header)
         (-> (res/unauthorized)
             (res/header "WWW-Authenticate" "Basic realm=\"whattever\""))

         (= auth-header (:auth-secret-docs cfg))
         (handler request)

          {:error "header 'authorization' inválido"})))))

(def app

   (ring/context "/docs" req
     :middleware [authorized-for-docs?]
       {:ui "/"
        :options {:ui {:swagger-docs "/docs/swagger.json"}}
        :spec "/swagger.json"
        :data {:info
               {:title "Title ..."
                :description "Foo Bar"}}}))


its ok to serve as an example? it could be improved or be more succinct?


thats pretty neat @plins . if its just an example, could probably simplify the authorized-for-docs? fn...


ok, great! i will simplify things a bit and post it on the wiki, thanks for the clarification!