This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-11-27
Channels
- # bangalore-clj (2)
- # beginners (41)
- # cider (14)
- # cljs-dev (12)
- # cljsrn (2)
- # clojure (106)
- # clojure-austin (6)
- # clojure-dev (22)
- # clojure-dusseldorf (1)
- # clojure-france (1)
- # clojure-greece (17)
- # clojure-italy (6)
- # clojure-poland (3)
- # clojure-russia (10)
- # clojure-serbia (5)
- # clojure-spec (24)
- # clojure-uk (100)
- # clojurescript (126)
- # cursive (3)
- # data-science (5)
- # datascript (15)
- # datomic (3)
- # defnpodcast (2)
- # dirac (6)
- # emacs (10)
- # fulcro (121)
- # graphql (30)
- # hoplon (7)
- # jobs (2)
- # leiningen (20)
- # off-topic (14)
- # onyx (3)
- # other-languages (13)
- # pedestal (1)
- # perun (2)
- # planck (41)
- # re-frame (16)
- # reagent (6)
- # reitit (5)
- # remote-jobs (3)
- # ring (1)
- # ring-swagger (17)
- # shadow-cljs (137)
- # spacemacs (6)
- # sql (4)
- # uncomplicate (7)
- # unrepl (56)
- # vim (27)
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
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.
Wondering if I could use ring-oauth2
as :middleware
with compojure-api, or if there's an example someone could point at?
I think it should just work? e.g. (api {:middleware [[ring.middleware.oauth2/wrap-oauth2 {...}]]})
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?