This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-08-15
Channels
- # alda (1)
- # beginners (24)
- # biff (9)
- # calva (55)
- # cherry (1)
- # clj-kondo (36)
- # cljs-dev (3)
- # clojure (37)
- # clojure-austin (2)
- # clojure-brasil (1)
- # clojure-europe (14)
- # clojure-nl (1)
- # clojure-norway (24)
- # clojure-spec (3)
- # clojure-uk (1)
- # community-development (6)
- # core-typed (1)
- # datalevin (5)
- # datomic (28)
- # emacs (14)
- # events (1)
- # gratitude (9)
- # hyperfiddle (27)
- # instaparse (3)
- # joker (16)
- # lsp (89)
- # malli (24)
- # missionary (2)
- # nbb (5)
- # off-topic (59)
- # re-frame (12)
- # reitit (17)
- # releases (4)
- # sci (14)
- # spacemacs (1)
- # squint (7)
- # xtdb (41)
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&list=PLBeQxJQNprbgkmvbv80xC8R0SHcPsosjZ&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/143
• https://stackoverflow.com/questions/58030830/clojure-cross-origin-error-totally-lost
• https://groups.google.com/g/luminusweb/c/8pKnjIR5eDA
Where did I go wrong? What should I do?Allow credentials
doesn’t work with wildcards afaik. You need to be specific regarding host and the port.
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).
I removed Allow credentials
and that had no effect.
@U04V70XH6 How do I do that?
With reitit? No idea, never used it. Ask in #C7YF1SBT3
Thank you for your time and the direction
Any experienced people with handling IP address rate limiting and VPN blocking I can dm? Only got some simple questions.
Curious how this goes in off-topic? Just so I have a future frame of reference for questions.
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.
That makes sense. Yea I asked here as I didn’t know any other channel that would be relevant
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.
every open jira has a corresponding Ask Clojure question that can be voted on
https://ask.clojure.org/index.php/1504/use-afn-applytohelper-rather-than-applyto-invokeexpr-eval
Thank you!
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)
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
Took me a while to notice your reply. Happy to provide a comment on what I was up to: https://ask.clojure.org/index.php/1504/use-afn-applytohelper-rather-than-applyto-invokeexpr-eval?show=13241#c13241
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?
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.
let me know if you have any questions about it
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.
@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 muchin 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
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.
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.
Fair enough, it's just a linter warning that's coming from an external tool. Clojure itself doesn't care.
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.
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