This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-04-20
Channels
- # aws (1)
- # beginners (14)
- # boot (30)
- # cider (6)
- # clara (9)
- # cljsjs (3)
- # cljsrn (22)
- # clojure (247)
- # clojure-dusseldorf (75)
- # clojure-france (38)
- # clojure-italy (6)
- # clojure-japan (8)
- # clojure-nl (3)
- # clojure-russia (16)
- # clojure-serbia (4)
- # clojure-spec (1)
- # clojure-uk (53)
- # clojurescript (168)
- # consulting (3)
- # copenhagen-clojurians (1)
- # core-async (7)
- # css (1)
- # datascript (3)
- # datomic (8)
- # dirac (22)
- # events (1)
- # hoplon (2)
- # jobs (1)
- # jobs-discuss (2)
- # leiningen (4)
- # lumo (138)
- # mount (13)
- # nyc (1)
- # off-topic (24)
- # om (34)
- # onyx (15)
- # pedestal (30)
- # re-frame (9)
- # reagent (23)
- # ring (1)
- # ring-swagger (24)
- # rum (6)
- # spacemacs (6)
- # specter (51)
- # uncomplicate (14)
- # unrepl (1)
- # untangled (17)
- # yada (12)
@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 https://www.clojure-toolbox.com/, 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: https://github.com/damesek/swagger-http-kit-compojure-api
@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
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)
(res/forbidden
{:error "'secret' query parameter is invalid"}))))
(def app
(ring/api
(ring/context "/docs" req
:middleware [authorized-for-docs?]
(docs/swagger-routes
{: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.
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
(api
{:swagger
{:ui "/"
:spec "/swagger.json"}}
(POST "/echo" []
:return (wrapped User)
:body [user User]
(wrap (ok user) {:some "meta"}))))
that returns data in:
{
"status": 200,
"body": {
"name": "string"
},
"meta": {
"some": "meta"
}
}
neat, thanks!
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)
(cond
(nil? auth-header)
(-> (res/unauthorized)
(res/header "WWW-Authenticate" "Basic realm=\"whattever\""))
(= auth-header (:auth-secret-docs cfg))
(handler request)
:else
(res/unauthorized
{:error "header 'authorization' inválido"})))))
(def app
(ring/api
(ring/context "/docs" req
:middleware [authorized-for-docs?]
(docs/swagger-routes
{:ui "/"
:options {:ui {:swagger-docs "/docs/swagger.json"}}
:spec "/swagger.json"
:data {:info
{:title "Title ..."
:description "Foo Bar"}}}))