Fork me on GitHub
#pedestal
<
2022-11-09
>
agorgl12:11:59

Can we somehow make resources / static content work like most other static content servers like nginx where serves in pedestal?

agorgl13:11:10

Using ::http/file-path to enable the default file pedestal interceptor by e.g. specifying ::http/file-path "resources/public/" works, but not as expected. It now serves files from given directory, but with a content-type response of application/octet-stream which leads to the html file being downloaded instead of presented by the browser

agorgl19:11:47

Tried fast-resource interceptor too, same outcome as file interceptor

agorgl19:11:10

I removed ::http/resource-path to disable default resource interceptor and added a route like:

["/" :get [(middleware/fast-resource "/public")]]
inspected the service map just to make sure, and same result: while I get the index.html back in the response, the content type is set to application/octet-stream and this leads to a download

hanDerPeder19:11:00

ah, yes. i remember now. the content-type ring middleware only sets the content type based on the request uri. it does not look at the file extension. theres an issue and a PR that fixes this in the ring repo, but alas, its not merged. https://github.com/ring-clojure/ring/issues/408 https://github.com/ring-clojure/ring/pull/452 we ended up doing this at work: `(defn content-type-interceptor [] (after ::content-type-interceptor (fn [ctx] (let [uri (get-in ctx [:request :uri]) resp (:response ctx)] (if-let [mime-type (or (get-in ctx [:response :headers "Content-Type"]) (mime/ext-mime-type uri) ;; Additional check compared to io.pedestal.http.ring-middlewares/content-type ;; Workaround for problem described here: https://github.com/ring-clojure/ring/issues/408 (when (instance? File (:body resp)) (mime/ext-mime-type (.getAbsolutePath (:body resp)))) ;; Same problem as above, when all else fails. Look for a hint in the context map. (when (::mime-lookup ctx) (mime/ext-mime-type (::mime-lookup ctx))))] (assoc-in ctx [:response :headers "Content-Type"] mime-type) ctx)))))`

agorgl21:11:49

I think that does the trick, although I would love this functionality to be supported upstream

agorgl21:11:54

Thanks for the suggestion!

dangercoder20:11:10

Is there something in pedestal which lets me define an interceptor which is executed before every other interceptor in the chain? (I want to tap> the interceptor :name and context for debugging purposes) 🙂. {::interceptor-name :my-interceptor ::input-context context}

hanDerPeder20:11:37

No, but that should be fairly straight forward to create: 1 map :name over your chain 2 instantiate the "tapping" interceptor for each name 3 interleave those with your original chain

hanDerPeder20:11:16

I think metosin has something like this, maybe in reitit, that will also run diff on the context to highlight whats changed.

dangercoder20:11:39

Thanks for the ideas, very much appreciated!

hlship22:11:43

Also, Pedestal already has logging of :enter, :leave, and :exit for each interceptor.

hlship22:11:54

Take a look in the .chain namespace.