Fork me on GitHub
#pedestal
<
2018-11-06
>
eraserhd14:11:04

Header names are case sensitive when you use response-for, which is easy enough to work around but a potential surprise.

ccann15:11:01

thanks @eraserhd, I did pick up on that pretty quickly 🙂

ccann16:11:29

One thing that was surprising was how to define interceptors. I wrote an interceptor that looked something like this:

(def logger
  {:name :logger
   :enter (fn [context]
            (with-out-str (clojure.pprint/pprint (:request context)))
            context)})
and added it to my interceptor chain in the server map configuration. But pedestal threw an assertion error saying one of the interceptors in the chain wasn’t an interceptor. Indeed the others are records and mine is a map, but what I absorbed from the docs was that you can write interceptors with maps as such. I ended up finding the io.pedestal.interceptors.helpers namespace in the source and the macros looked pretty nifty, so I rewrote my logging interceptor (I know there is one of these already available) with defbefore and my handlers with defhandler. Now I’m wondering if it makes more sense to use the macros in general vs just maps vs before, after, etc. I’m not sure what’s the most idiomatic

mtnygard16:11:50

@ccann Do you still have the code around from when the map didn't work in your interceptor chain?

mtnygard16:11:34

The best way to turn a map into an interceptor is by calling io.pedestal.interceptor/interceptor on it. Anything that is in a route's interceptor vector has that done automatically. But I wonder if there are places where we aren't doing that.

mtnygard16:11:18

IOW, I'd like it to "just work" without using the (mildly deprecated) macros in helpers

ccann16:11:24

@mtnygard I do! it’s basically just

(-> service-map
          http/default-interceptors
          http/dev-interceptors
          (update ::http/interceptors into routes/interceptors))
where routes/interceptors is a vector of [log-request (body-params) json-body] the first being my logger, the actual implementation of which was:
(def log-request
  {:name :log-req
   :enter
   (fn [context]
     (log/info ::log-request)
     (log/info (with-out-str (clojure.pprint/pprint (:request context))))
     context)})

mtnygard16:11:54

I can confirm that don't automatically call io.pedestal.interceptor/interceptor on that key in the service map.

mtnygard16:11:06

Sorry for the confusion, and I'm adding an issue to the docs repo so we can clarify.

ccann16:11:18

awesome thanks!

mtnygard16:11:09

LMK if you run into anything else. I travel a lot so I'm not usually this quick to respond, but I'll see it eventually. 🙂

🙂 4
✈️ 4
ccann16:11:30

it also looks like the before, after etc. functions have two arities [f] and [f & args] but then in the source code you can use it like so (interceptors/before ::foo-bar (fn [ctx]...

mtnygard16:11:34

The second arity tries to be too clever. If f is not a function, then the macro uses it as the name. Then it deconstructs args to use as an existing function name and additional arguments to that function.

mtnygard16:11:42

It was not a good idea, and I'd recommend avoiding it.

👍 4
ccann22:11:12

this might just be demonstrating my lack of mature docker / networking knowledge, but I couldn’t get my pedestal app to respond when running in a docker container with ports mapped appropriately and hitting it with curl. e.g.:

docker run -it -p 3080:3080 008940197b83
then
curl --request GET \
           --url 

ccann22:11:46

until I added ::http/host "0.0.0.0" to my service map, curl would just report curl: (52) Empty reply from server. but running outside a docker container I don’t need that key.

jvtrigueros22:11:07

Ah yes, this bit me once! I opened a PR to updates docs.

jvtrigueros22:11:26

It's a safer default, but needs to be documented.

ccann22:11:37

what’s going on exactly?

jvtrigueros22:11:09

If I understand correctly, only hosts calling from localhost are allowed to connect to the server.

jvtrigueros22:11:26

How are you routing traffic the service in Docker?

ccann22:11:15

ohhh. uhh, I’m just running java -jar server.jar in my container. and running with docker run as above not sure if that answered your question

jvtrigueros22:11:41

Yeah so when you do java -jar server.jar on your physical machine, you are localhost, so executing the curl command works.

jvtrigueros22:11:00

When running inside the Docker container, the app sees you as not localhost but some other host.

ccann22:11:26

okay, great. thanks!

ccann22:11:40

I’ll upvote that PR

ccann23:11:06

do you have a link to it?