This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-03-24
Channels
- # aleph (4)
- # beginners (93)
- # cider (7)
- # cljs-dev (16)
- # cljsrn (5)
- # clojure (192)
- # clojure-dusseldorf (3)
- # clojure-italy (14)
- # clojure-russia (16)
- # clojure-serbia (1)
- # clojure-spec (85)
- # clojure-taiwan (1)
- # clojure-uk (79)
- # clojurescript (188)
- # code-reviews (9)
- # core-async (2)
- # crypto (1)
- # cursive (26)
- # datomic (21)
- # heroku (1)
- # hoplon (3)
- # jobs (7)
- # jobs-discuss (20)
- # jobs-rus (13)
- # off-topic (77)
- # om (15)
- # onyx (23)
- # pedestal (94)
- # planck (11)
- # proton (10)
- # protorepl (1)
- # re-frame (16)
- # ring (22)
- # ring-swagger (9)
- # rum (2)
- # specter (18)
- # testing (2)
- # untangled (14)
- # vim (12)
- # yada (58)
Hey folks - I'm looking at making a really simple hacky json API for an internal company hackathon - it doesn't need to do much at all beyond accepting POSTS with json payloads and return JSON responses. I'd like to keep my example really simple, along the lines of a Sinatra app. What's the minimum to do this in Yada/Bidi?
(yada/resource
{:methods
{:post {:response "application/json" :accepts "application/json" :response (fn [ctx] {:hello "world"})}})
is the resourceIt feels like the docs jump straight from "here's a sample with no routes" to git clone edge
🙂
@korny noted. Good feedback!
@dominicm I get that - but that's not particularly a great demo for me to say "hey, this is as easy as your Sinatra example. Oh, hang on, you need to look at this other projects for routes" 🙂
I should say I like the separation between the two. And "being like Sinatra" isn't a goal for yada!
yada depends on bidi
So you just need to depend on yada
Architecturally they are separate libs but yada 'know' about bidi and provides some extra features if it's present
I suspect I'll just git clone edge
then delete all the bits I don't want. I'm just lazy and hoped someone else had already done this 🙂
The ext mechanism means you can have a yada which eliminates bidi completely. But that's just a technical point. Yada 'full' comes batteries included with bidi. Fine for hackathons.
Edge is intended to show 'full stack' including the great adzerk cljs reloading and browser repl figwheel-style experience
Overkill for a sinatra-like backend
@korny I was trying to find the emoji for 'I know the feeling' but then Bruce came up
So cool that someone made an emoji for Bruce!
Yeah. The Edge thing is great for full stack (needs a db layer though)
But would be great to have a stripped down version
It would be a lot simpler
@dominicm Does Yada really handle content negotiation? I tried to coerce a 406 Not Acceptable out of Yada by sending a request with a Accept: application/edn
header to a resource which only produced json i.e. :produces "application/json"
. The only thing Yada checked as far as I could tell was that the request body was on the accept-format specified in the resource.
I ended up creating my own interceptor to handle 406 responses. Edit: Would be nice to not have to do that.
If you only produce EDN but the client asks for JSON it's a 406
Yada supports content negotiation.
(Took me a whole summer to implement properly)
Well, I'l be damned if it does not work without my interceptor. I must have missed something when testing this earlier on. Thank you both for the clarification.
Can I somehow do a “server-side” redirect instead of returning 302 and letting the client do a new request?
In my case, I need all uri ending in “/” to redirect to the uri without the “/”. I could do a regex on all routes, but that seems a bit like a lot of local fixes to a global problem
Hmm, well, a redirect that the server (yada) takes care of, eg it does not involve the client. So it’s a kind of an alias of a uri
Well, my service is serving stuff like /some-resource
, but that gets prepended with /api/some-resource
by nginx.
ah, hadn't ever considered that. Surprised nginx can't prepend onto that sort of thing.
There’s probably a way to do what I want with nginx. But it’s more fun to solve in clojure 😉
The simplest way I can think of is a function which takes the "pattern" and doubles it up:
(defn double-vision
[pattern]
#{pattern (concat pattern ["/"])})
Might need to handle non-nested too, something like:
(if-not (sequential? pattern)
(vector pattern)
pattern)
Basically, it will take this:
[(double-vision ["/some-resource"]) (fn [req] …)]
and generate
[#{["/some-resource"] ["/some-resource" "/"} (fn [req] …)]