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.
This is the documentation that I was talking about http://metosin.github.io/compojure-api/doc/compojure.api.sweet.html
Hi, does this help? https://github.com/metosin/compojure-api/wiki/Serving-static-resources#embedded-routes
Wow thanks @ikitommi, works like a charm!
I gotta ask though, when I include compojure-api, does all compojure and ring namespaces available for use?
Yes, c-api depends on both Ring & Compojure. With leiningen, you can say lein deps :tree
to see the included deps per artefact.
Alright, thanks again @ikitommi!
Wondering if I could use ring-oauth2
as :middleware
with compojure-api, or if there's an example someone could point at?
think I might, but not sure about the required ordering
I think it should just work? e.g. (api {:middleware [[ring.middleware.oauth2/wrap-oauth2 {...}]]})
I'll let you know! π :thumbsup:
ah, but you might need the cookies from ring-defaults.
yes
and possibly anti-forgery too
also, the swagger can be instructed to enforce those for the client (e.g. the swagger-ui).
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.
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?