This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-07-02
Channels
- # aleph (1)
- # architecture (4)
- # beginners (39)
- # boot (12)
- # cider (25)
- # cljs-dev (3)
- # cljsrn (5)
- # clojure (175)
- # clojure-dusseldorf (1)
- # clojure-italy (13)
- # clojure-nl (4)
- # clojure-russia (1)
- # clojure-spec (52)
- # clojure-uk (110)
- # clojurescript (35)
- # data-science (2)
- # datomic (61)
- # editors (8)
- # emacs (2)
- # fulcro (7)
- # graphql (15)
- # hoplon (1)
- # hyperfiddle (3)
- # jobs (4)
- # jobs-discuss (12)
- # juxt (2)
- # lein-figwheel (6)
- # leiningen (35)
- # off-topic (4)
- # onyx (5)
- # parinfer (1)
- # pedestal (63)
- # re-frame (38)
- # reitit (7)
- # ring-swagger (7)
- # rum (2)
- # shadow-cljs (27)
- # tools-deps (10)
- # uncomplicate (16)
Is there a function in pedestal (or any of it’s dependencies) that will return the path part of a given URL?
To step back a bit: I’m trying to see if the referer url matches any of my defined routes. Any suggestions welcome 🙂
@martinklepsch the key :path-info
should be on the request map. I think that’s what you are looking for.
@ddeaguiar that works for the current incoming request but i’m interested in operating on the referer
of the request
more general problem to give some context:
I want to allow users to use URLs like /some/thing/CURRENT
where CURRENT
is resolved based on the referer and a redirect to the actual “current” thing is returned
hope that makes some sense
> sorry, you want to take a url and get the path-info essentially yeah, and that’s the simpest part of it I guess, I duct-taped it with some regexes now but thought there’s probably something built in
Path info is calculated on a per-container basis. There’s a ContainerRequest protocol which is implemented for servlet containers
so for example, here’s the impl for servlets https://github.com/pedestal/pedestal/blob/23b2144e72f122793531192ee2079c095de71002/service/src/io/pedestal/http/request/servlet_support.clj#L26-L31
Essentially you’d need to know what the context path is for the referrer to do a similar thing
Ended up with this now which feels somewhat hacky but at least it gives me back a map with info about the parsed route:
(route/try-routing-for (routes identity {}) :map-tree "/d/reagent/reagent/0.8.1" :get)
@martinklepsch you may find parse-path
to be helpful https://github.com/pedestal/pedestal/blob/f89d0abe244d0e8385d66a697724ef56180a00e5/route/src/io/pedestal/http/route/path.clj#L35.
sample usage
(require '[io.pedestal.http.route.path :as route.path])
(def parsed-path (route.path/parse-path "/foo/:id/:baz"))
;; {:path-constraints {:baz "([^/]+)", :id "([^/]+)"},
;; :path-params [:id :baz],
;; :path-parts ["foo" :id :baz]}
(route.path/path-regex parsed-path)
;; #"/\Qfoo\E/([^/]+)/([^/]+)"
@ddeaguiar Interesting, are you suggesting to build my own match-route
based on my route information and that function?
In case you haven’t seen this message: https://clojurians.slack.com/archives/C0K65B20P/p1530544463000434
but I think in your case you are dealing with referer urls right? These are not routes in your app, correct?
they might not be but I’m specifically trying to handle the case that they are part of my app (for which I need to match them against my route table
I’m unclear as to what the route
fn is in your example:
(route/try-routing-for (routes identity {}) :map-tree "/d/reagent/reagent/0.8.1" :get)
I ended up with this now, which is OK but it seems that this could be made easier: https://github.com/martinklepsch/cljdoc/commit/8a369e546d6b4fb6df979ee1ba71126e6e0d967a#diff-2b617a32cb2ee893338f526bde351be6R84
routes
returns whatever expand-routes
returns
why do you need match-routes
again? You can leverage a specific routing impl directly
I’m using that little indirection of passing a resolver
to decouple the definition of the routes table from the actual handlers
If you look at what try-routing-for
does, it creates a router of a specific type and invokes the :enter
impl
I need it because I want to determine route-name
and path-params
for an arbitrary path-info
string like "/some/uri"
yes that’s what I ended up using 😄
It does what I need but it seems undocumented and that ;;; Help for debugging
comment makes me think that I might be abusing it for things it wasn’t intended for
I think it’s perfectly acceptable to use a router directly. I don’t think you need to bother with try-routing-for
I’ll head to bed but thanks for looking into this. Maybe something along those lines would be a good addition to the API.
@martinklepsch you can create the router of your choice using the relevant constructor and then use the io.pedestal.http.route.router/find-route
impl on the router to match routes
The simplest form of router usage can be found here: https://github.com/pedestal/pedestal/blob/master/route/src/io/pedestal/http/route/prefix_tree.clj#L497-L508
Considered creating a “container” thing to get the path info but some regex is doing the job fine for now…
(Looking for something akin to Bidi’s match-route
I guess: https://github.com/juxt/bidi#route-patterns)
Hi, is there any example on how to ensure an explicit content encoding (ie I want to be sure I always encode the body as utf-8), while also setting the correct content-length header?
I have made a custom interceptor that encodes JSON via Cheshire, and I use (count (.getBytes s))
to get length in bytes, but I’d like to be 100% sure it does the right thing.
@orestis you may want to consider using the built-in json-body
interceptor instead.
https://github.com/pedestal/pedestal/blob/master/service/src/io/pedestal/http.clj#L115-L127
I don’t think pedestal sets the content-length header but it does support updating the servlet response if it’s set.
Thanks! Already I see a bunch of functionality I can use to change my interceptor. I need some more fine grained control so I can’t use the built in one.
Any idea about bytes/encoding etc? I guess this is a more generic thing, but in other languages I’d have to explicitly encode a string to utf-8, so passing strings around makes me worried. Though now I see that I can also use a function that will write to the stream. I might have to dig a little bit deeper there to see how it works.
I might file an issue for this for further discussion, if that’s OK? I was half expecting for this to work out of the box.