Fork me on GitHub
#clojure
<
2023-08-15
>
Adham Omran13:08:01

Hey all I'm writing some Clojure backend and I've hit a wall at trying to add wrap-cors to add "`Allow Control Access Origin "*"`" to avoid CORS errors in the frontend. Here's my minimal example

(ns tmp.minimum-example
  (:require
   [muuntaja.core :as m]
   [reitit.coercion.spec :as coercion-spec]
   [reitit.ring.middleware.exception :refer [exception-middleware]]
   [reitit.ring.coercion :refer [coerce-request-middleware
                                 coerce-response-middleware]]
   [reitit.ring.middleware.muuntaja :refer [format-middleware]]
   [ring.adapter.jetty :refer [run-jetty]]
   [ring.middleware.cors :refer [wrap-cors]]
   [reitit.ring :as ring])
  (:gen-class))

(def router-config
  {:data {:coercion coercion-spec/coercion
          :muuntaja m/instance
          :middleware [format-middleware
                       exception-middleware
                       coerce-request-middleware
                       coerce-response-middleware]}})

(def routes
  (ring/ring-handler
   (ring/router
    ["/v1" {:middleware [#(wrap-cors %
                                     :access-control-allow-origin [#".*"]
                                     :access-control-allow-methods [:get :post :put :delete
                                                                    :origin])]}
     ["/foo" {:get (fn [_] {:body {:key "value"}})}]]
    router-config)))

(comment
  (def server
    (run-jetty #'routes {:port 3123
                         :join? false}))

  (.stop server))
I want the GET request to /v1/foo to respond with the header Access-Control-Allow-Origin * I've looked at https://www.youtube.com/watch?v=Jf94HNmCXyU&amp;list=PLBeQxJQNprbgkmvbv80xC8R0SHcPsosjZ&amp;index=4 but even the creator didn't get CORS to work. I also looked at the code from other tutorials but I wasn't able to find any that were useful. I read the following threads • https://github.com/metosin/reitit/issues/143https://stackoverflow.com/questions/58030830/clojure-cross-origin-error-totally-losthttps://groups.google.com/g/luminusweb/c/8pKnjIR5eDA Where did I go wrong? What should I do?

agigao14:08:25

Allow credentials doesn’t work with wildcards afaik. You need to be specific regarding host and the port.

seancorfield15:08:50

The main thing I see people run into is that wrap-cors must be the absolute outermost piece of middleware and it has to be able to handle OPTIONS (rather than something else in your stack intercepting or blocking that).

Adham Omran15:08:16

I removed Allow credentials and that had no effect.

seancorfield16:08:06

With reitit? No idea, never used it. Ask in #C7YF1SBT3

Adham Omran16:08:47

Thank you for your time and the direction

grounded_sage14:08:00

Any experienced people with handling IP address rate limiting and VPN blocking I can dm? Only got some simple questions.

p-himik15:08:13

I'd just move the question to #C03RZGPG3 and ask it publicly.

👍 2
grounded_sage15:08:34

Curious how this goes in off-topic? Just so I have a future frame of reference for questions.

p-himik15:08:41

Because it doesn't sound that it fits into any other channel here, at least not a channel that I know of and that's relatively populated.

grounded_sage15:08:20

That makes sense. Yea I asked here as I didn’t know any other channel that would be relevant

Ben Grabow15:08:11

How would I go about voting/promoting attention on an issue in CLJ? I ran into this yesterday and it consumed most of the afternoon until I figured out what was wrong. https://clojure.atlassian.net/browse/CLJ-1715 Looks like it has a patch already suggested.

Alex Miller (Clojure team)15:08:08

every open jira has a corresponding Ask Clojure question that can be voted on

Alex Miller (Clojure team)15:08:54

but really, what would be great is if you could comment on the ask clojure question with the problem you were encountering. (this jira starts with one solution and really only alludes to the problem, without considering many other possible solutions)

Alex Miller (Clojure team)15:08:36

given that, there is little chance this jira will move forward without significant work, and the most important thing it is missing is what you're actually trying to do, so adding that would be very helpful

jcd15:08:17

I'd like a tool that would take a function, parse it and walk its AST, and count invocations of another function. I could probably hammer something together with tools.analyzer but it doesn't recurse into invocations so I'll have to put that together. If there is something that exists or does most of the work for me, I'd prefer to start with that. Anyone ever done anything like this or know of something I could start with?

jpmonettas16:08:17

Maybe you can take inspiration from here https://github.com/jpmonettas/hansel/blob/master/src/hansel/api.clj#L30-L89 There is a function to instrument a clojure var (instrument-var-clj), that supports "deep instrumentation", which means it will grab a var, get the source, instrument it, then walk the ast finding other functions calls and do the same recursively.

🙌 2
jpmonettas16:08:02

let me know if you have any questions about it

vemv16:08:46

https://github.com/clojure-emacs/orchard/blob/master/src/orchard/xref.clj might be interesting it gives you the dependencies of function f (the function it calls) or its dependents (the functions that call it). It doesn't calculate things like transitive dependencies. And it doesn't count invocations: you can only check whether x depends on y. You could read and walk the form though for counting invocations.

🙌 2
jcd16:08:52

Thanks both, I will check these out.

🙌 2
jpmonettas17:08:57

@U023QRP1648 just extracted that into this :

(defn- resolve-fn-symb [symb]
  (when-let [v (resolve symb)]
    (when (and (var? v) (fn? (deref v)))
      (symbol v))))

(defn- find-interesting-vars-references [form]
  (->> (tree-seq coll? seq form) ;; walk over code s-expressions
       (keep (fn [x]
               (when (symbol? x) 
                 (when-let [vsymb (resolve-fn-symb x)]                   
                   vsymb))))
       doall))

(defn count-fn
  ([var-symb fn-symb] (count-fn var-symb fn-symb (atom #{})))
  ([var-symb fn-symb *visited]
   (swap! *visited conj var-symb)
   (let [var-ns-symb (symbol (namespace var-symb))]
     (binding [*ns* (find-ns var-ns-symb)]
       (if-let [form (some-> (clojure.repl/source-fn var-symb)
                             read-string)]
         (let [sub-vars (->> (find-interesting-vars-references form)
                             (remove @*visited))
               fn-symb-cnt (count (filter #(= % fn-symb) sub-vars))]           
           (+ fn-symb-cnt (reduce + 0 (map #(count-fn % fn-symb *visited)
                                           (into #{} sub-vars)))))
         0)))))

=> (count-fn 'clojure.core/map 'clojure.core/seq)
33
looks like it is working but haven't check it much

jcd17:08:19

Awesome! I’ll fiddle with this

bronsa17:08:54

in terms of solutions using t.a , it should be easy to adapt transitive-deps from https://github.com/Bronsa/tools.analyzer.jvm.deps to return a map of symbol -> occurrences instead of a set

🙌 4
jcd17:08:04

Awesome! I’ll take a look at that too.

vncz17:08:48

Why am I receiving a warning about two members not being used?

p-himik17:08:30

Implementing a function of a protocol is not using that function. But that's really a question for that editor since Clojure doesn't have an opinion there.

Joshua Suskalo17:08:40

I'm not quite sure if this is calva or cursive, so if you're in intellij I don't know what's making that lint, but if you're in vscode then you can disable either temporarily or permanently the :clojure-lsp/unused-public-var lint with your clj-kondo config, or by putting a #_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]} on the line before your defprotocol.

vncz17:08:06

I mean I can skip it - it was more a curiosity

Joshua Suskalo17:08:49

Fair enough, it's just a linter warning that's coming from an external tool. Clojure itself doesn't care.

Joshua Suskalo17:08:53

And the warning is because while you are using typeName in the definition of typeFqn, you aren't using either that or entityType in your code.

vncz17:08:19

That I understand. I was just surprised to find unused warning in protocols

Joshua Suskalo17:08:58

Protocols end up producing clojure vars for those methods, so I think it's just because clj-kondo expands protocols into the vars, and then clojure-lsp is spotting the vars and checking for usage

👍 2