ring-swagger

ring-swagger & compojure-api
hawari 2017-11-27T09:33:04.000354Z

Hello, I've just recently started using clojure, and I'm currently using compojure-api to build an API. When a route is not present in my application, I notice that instead of returning 404 and a message that a resource couldn't be found, the default configuration returns 500 and a html saying that "Response map is nil". Obviously that's not something that I would like to convey to the API user. I found that there's a :invalid-routes-fn key in the api options from the documentation. What the documentation doesn't explain is what kind of function that this key expect, except of it's a 2-arity function. Can somebody helps me on this? I'd really appreciate it.

hawari 2017-11-27T09:33:21.000234Z

This is the documentation that I was talking about http://metosin.github.io/compojure-api/doc/compojure.api.sweet.html

hawari 2017-11-27T09:37:59.000410Z

Wow thanks @ikitommi, works like a charm!

hawari 2017-11-27T09:38:51.000445Z

I gotta ask though, when I include compojure-api, does all compojure and ring namespaces available for use?

ikitommi 2017-11-27T10:34:01.000237Z

Yes, c-api depends on both Ring & Compojure. With leiningen, you can say lein deps :tree to see the included deps per artefact.

hawari 2017-11-27T10:55:26.000094Z

Alright, thanks again @ikitommi!

mgrbyte 2017-11-27T15:45:08.000286Z

Wondering if I could use ring-oauth2 as :middleware with compojure-api, or if there's an example someone could point at?

mgrbyte 2017-11-27T15:45:30.000449Z

think I might, but not sure about the required ordering

ikitommi 2017-11-27T15:48:26.000629Z

I think it should just work? e.g. (api {:middleware [[ring.middleware.oauth2/wrap-oauth2 {...}]]})

mgrbyte 2017-11-27T15:48:58.000273Z

I'll let you know! πŸ™‚ :thumbsup:

ikitommi 2017-11-27T15:50:32.000788Z

ah, but you might need the cookies from ring-defaults.

mgrbyte 2017-11-27T15:50:47.000455Z

yes

mgrbyte 2017-11-27T15:51:02.000662Z

and possibly anti-forgery too

ikitommi 2017-11-27T15:51:52.000217Z

also, the swagger can be instructed to enforce those for the client (e.g. the swagger-ui).

1πŸ‘
ikitommi 2017-11-27T15:53:18.000581Z

when you get a setup working πŸ˜‰, would be great to have an example of that in the examples. If there is too much boilerplate, let’s see if that could be reduced with some addons to c-api itself.

1πŸ˜›
petr.mensik 2017-11-27T22:27:05.000016Z

I am trying to serve both static content and JSON responses from API however I am struggling with return of correct content type (currently everything is either plain or octet-stream). This is the API configuration

(def app
  (api
   (assoc api-config
          :middleware [[wrap-resource resources-root]
                       [wrap-cors :access-control-allow-origin [#"\\*"] :access-control-allow-methods [:get :post :put :delete :patch]]
                       wrap-reload
                       [wrap-defaults (-> site-defaults
                                          (assoc-in [:not-modified-responses] true)
                                          (assoc-in [:security :anti-forgery] false)
                                          (assoc-in [:security :ssl-redirect] (not (:dev env)))
                                          (assoc-in [:security :hsts] (not (:dev env))))]])
   (context "/auth" []
     api-routes-login
     api-routes-password)

   (context "/api" []
     :header-params [authorization :- String]
     :middleware [[wrap-authentication config/backend]]
     api-routes-calendar ; more routes here
     (undocumented (route/resources "/" {:root resources-root})
                 app-routes
                 not-found-routes)))
Where app-routes are
(defn- get-page
  "Servers HTML page from server"
  ([]
   (get-page "index.html"))
  ([page]
   (content-type (resource-response page {:root resources-root}) "text/html")))

(defroutes app-routes
  (GET "/" [] (get-page))) ;etc
Basically the same configration worked with plan compojure so what am I doing wrong here?