Fork me on GitHub
#pedestal
<
2020-06-12
>
emccue02:06:24

How would i add an interceptor to "everything"

emccue02:06:27

I'm thinking it makes sense to have my db pool as context accessible from an http request

ddeaguiar11:06:59

@emccue if you mean having an interceptor execute prior to any handler irrespective of route then you can create a common-interceptors coll and add it there. The buddy-auth sample does this to ensure the authentication interceptors are executed for each route (https://github.com/pedestal/pedestal/blob/master/samples/buddy-auth/src/buddy_auth/service.clj#L74-L80)

isak17:06:07

Seems like the callback only gets the message. Is there a way to change it?

isak17:06:41

thanks, I was just about to make my own listener

dvingo17:06:58

i was trying to find the slack logs, but looks like it's down now

dvingo17:06:14

this was the sample listener

(defn ws-listener
  [_request _response ws-map]
  (proxy [WebSocketAdapter] []
    (onWebSocketConnect [^Session ws-session]
      (proxy-super onWebSocketConnect ws-session)
      (when-let [f (:on-connect ws-map)]
        (f ws-session)))
    (onWebSocketClose [status-code reason]
      (when-let [f (:on-close ws-map)]
        (f (.getSession this) status-code reason)))
    (onWebSocketError [^Throwable e]
      (when-let [f (:on-error ws-map)]
        (f (.getSession this) e)))
    (onWebSocketText [^String message]
      (when-let [f (:on-text ws-map)]
        (f (.getSession this) message)))
    (onWebSocketBinary [^bytes payload offset length]
      (when-let [f (:on-binary ws-map)]
        (f (.getSession this) payload offset length)))))

      ;; in your service map:
::http/container-options {:context-configurator #(ws/add-ws-endpoints % ws-paths {:listener-fn ws-listener})}

dvingo17:06:19

no prob, I've been meaning to try this out too. would be great if you don't mind posting your working service setup when you have one

isak18:06:09

cool @hindol.adhya @danvingo, I just got it to work. Btw I noticed it is pretty hard to get a good reloading experience with the socket handlers, but here is one thing that works:

isak18:06:31

(I tried putting the var at a higher level, but didn't work)

hindol18:06:44

Yeah Java code is calling into this code. Reloading should work with the right indirection in place.

lucian30318:06:09

i'm writing a 'not found' interceptor to send all requests that were not fulfilled to another backend server and passes through its response to the output. all works, except for requests w/ a really large body, the body gets cut off in the output. it's complete in the request though. when i println the request i can see the entire body. but the output is truncated. i'm using immutant though jetty does the same. what could be causing this?

(defn send-request
  [request]
  (println request)
  (try
    (let [params   {:url                (str (config/env :neo-api-url) (:uri request))
                    :body               (:body request)
                    :method             (:request-method request)
                    :socket-timeout     10000 ;; in milliseconds
                    :connection-timeout 1000
                    :headers            (merge (:headers request)
                                               {:authorization (str "Bearer " (config/env :neo-api-token))
                                                :accept        "application/vnd.ucf.v1+json"})}
          response (client/request params)]
      (println response)
      response)
    (catch Exception e
      (timbre/warn "Could not reach API server" e))))

(def passthrough
  {:name  ::passthrough
   :leave (fn [context]
            (let [request (:request context)]
              (assoc context :response (send-request request))))})

lucian30318:06:42

i think the issue is the headers coming back esp content-length. i updated the code to only take the body and put it in a new response and it's working now:

(assoc context :response (response/response (:body (send-request request))))))})

lucian30320:06:29

is there an interceptor or other way to define params using plumatic schema like compojure-api/reitit or some other way of declarative parameter handling? other than using the reitit router in pedestal that is

lucian30317:06:13

i have briefly. wasn't sure if it was still an active project. same thing with the rook project. i will take a closer look

oliy07:06:27

It's stable as in no outstanding bugs and no outstanding features. If you think it's missing anything or you find a bug just raise an issue/pr