Fork me on GitHub
#clojure
<
2019-07-05
>
Pick Feky01:07:01

I have following composure-api code, is there any way to wrap CORS around routes with ring-cors lib?

Pick Feky01:07:25

it seems not generating CORS headers in response.

Pick Feky01:07:46

shd I just use ring?

seancorfield01:07:36

@scalphunter How are you verifying that that behavior? ring-cors only does anything for :options requests. Everything else just passes through untouched I think.

seancorfield01:07:33

If I hit one of our APIs with curl I don't see CORS headers for regular requests. I only see CORS headers in a browser's dev tools when it sends OPTIONS ahead of an operation...

seancorfield01:07:09

(admittedly, we're using plain Compojure with ring-cors but I wouldn't expect compojure-api to behave that differently)

Pick Feky01:07:18

I use postman , and it shows headers. but i coudn't see the headers from composure-api generated response.

Pick Feky01:07:39

i think i shd check inside further. thanks!

vinurs02:07:08

how can i conver this to clojure map

seancorfield02:07:40

See what you get by calling bean on it @haiyuan.vinurs

vinurs02:07:08

i’ve tried it

vinurs02:07:11

1. Caused by java.lang.RuntimeException Invalid token: :

vinurs02:07:21

it seems that it meets : will failed

seancorfield02:07:56

Hmm, bean can be called on a lot of Java objects. Can you share the code you tried?

dpsutton03:07:44

I see snsapi and accessToken there. is this something that should be taken down and all access revoked from AWS?

theeternalpulse03:07:29

I'm having issues with migratus, which are I think related to the jdbc library, Not sure if the connectoin data for the database is correct, but I'm getting

java.sql.SQLException: No suitable driver found for jdbc:
wihh the data
:db {:classname "org.postgresql.Driver"
                  :subprotocol "postgesql"
                  :subname "//localhost/parrot"
                  }

seancorfield03:07:00

First off, don't use that form of map, use the modern, well-supported form

seancorfield03:07:07

Second, put an r in it.

seancorfield03:07:31

@theeternalpulse {:dbtype "postgres" :dbname "parrot" ...}

seancorfield03:07:49

There's no such database as postgesql 🙂

theeternalpulse03:07:14

ah, I'll see about the modern form, the migratus seems to only have the type I posted as the data to use

seancorfield03:07:50

If Migratus is passing the db-spec through to clojure.java.jdbc, it totally doesn't care what's in it.

seancorfield03:07:11

That classname/subprotocol/subname is horrible and error-prone. I wish people would stop using it 😞

seancorfield03:07:22

That's why next.jdbc does not support it!

theeternalpulse03:07:38

does this look better

:db {:dbtype "postgresql"
                  :dbname "parrot"
                  :host "127.0.0.1"
                  :user "postgres"
                  :password "<real pw>"}

seancorfield03:07:21

You can omit :host -- it will default to localhost.

seancorfield03:07:40

The :dbtype can be "postgres" if you prefer to use the short name.

theeternalpulse03:07:46

still getting

SEVERE: Error creating DB connection for {:dbtype "postgresql", :dbname "parrot", :host "127.0.0.1", :user "postgres", :password "d<censored>"}
java.sql.SQLException: No suitable driver found for jdbc:

theeternalpulse03:07:04

I'm using a docker istance of postgres if that matters

seancorfield03:07:42

Then in that case you don't have the JDBC driver for PostgreSQL as a dependency in your project I expect.

theeternalpulse03:07:14

ah, I was curious about if that was in the library itself or if I depend on i

seancorfield03:07:08

org.postgresql/postgresql {:mvn/version "42.2.2.jre7"}

seancorfield03:07:07

The documentation says "You must specify the appropriate JDBC driver dependency in your project -- these drivers are not included with java.jdbc." @theeternalpulse

seancorfield03:07:41

(and the next.jdbc docs say "In addition, you will need to add dependencies for the JDBC drivers you wish to use for whatever databases you are using.")

theeternalpulse03:07:06

yeah, my kickoff point was migratus, should have read through java.jdbc first

Pick Feky04:07:03

I just downloaded postgresql drive jar file and put it into resources-path. it automatically loads driver then.

Pick Feky04:07:16

btw, i solved issue of CORS, i just manually added headers. seems like my ring-cors wrapper doesn't work. I think i have to make my own middleware on this for my own study.

seancorfield04:07:52

@scalphunter You should never need to download JAR files -- just put the dependencies in your project.clj, build.boot, or deps.edn file.

Aron04:07:14

only those three? where else?

theeternalpulse05:07:21

he's referencing the different systems to load dependencies, lein,boot,and tools.deps. Not sure if there are other tools that are as popular as those though.

jaihindhreddy06:07:06

@U0VQ4N5EE project.clj if you use leiningen, build.boot if you use boot and deps.edn if you use tools deps.

💚 4
seancorfield04:07:22

The ring-cors wrapper works just fine. We use it heavily in production.

Pick Feky05:07:56

@seancorfield Yeah, you're right. ring-cors hides headers. I checked out and finally it works now. I missed regex in :access-control-allow-origin. Thanks!

4
pinkfrog06:07:50

why apache storm changed from clojure to java?

pinkfrog06:07:02

any deficiency in the language itself?

seancorfield06:07:33

@i They mostly changed so that more developers could contribute I think.

4
seancorfield06:07:55

(since there are lots more Java developers than Clojure developers)

pinkfrog06:07:51

if that’s the only case, then I am relieved. 😉

pinkfrog06:07:07

(as long as it’s not about performance)

dpsutton06:07:33

There’s a reddit thread with discussion and some excellent thoughts (as always) from Alex Miller

kartik715307:07:12

Can you share link of reddit thread.

seancorfield06:07:50

Any rewrite can take advantage of a lot of previous experience and knowledge.

pinkfrog06:07:54

#2 came from Alibaba. well..

kirill.salykin11:07:53

how is if implemented? having hard time to find it in source code…

lispyclouds11:07:26

its a special form recognized by the compiler.

lispyclouds11:07:05

special forms are generally implemented in the host language. eg Java, JS, C# etc

manutter5111:07:43

Or at least it starts there, I’m still tracking it down myself.

kirill.salykin11:07:18

🙂 i understand you

manutter5111:07:56

Ah, there’s the parser class, looks like it’s all in that same file, cool.

manutter5111:07:12

I’ve often wondered that myself but this is the first time I’ve actually tracked it down.

manutter5111:07:19

The meat of it is in a class called IfExpr, defined farther down in that same file.

noisesmith12:07:28

one interesting detail about clojure's if, is it doesn't use the value of false but rather the canonical interned identity

noisesmith12:07:52

in Java, you can create a new boolean of value false, and if recognizes it as a false value for dispatch

noisesmith12:07:24

in Clojure, Boolean/FALSE is checked for identity, your own new false won't be identical, so will be treated as non-false (true)

noisesmith12:07:25

(cmd)user=> (identical? Boolean/FALSE false)
true
(ins)user=> (identical? (Boolean. "FALSE") false)
false
(ins)user=> (if (Boolean. "FALSE") :OK :no)
:OK
(ins)user=> (= (Boolean. "FALSE") false)
true

dmaiocchi16:07:44

Anybody had done a prometheus exporter in clojure to look at?

noisesmith16:07:48

riemann is written in clojure and can export to prometheus - probably not the simplest example, I haven't looked at the implementation myself

noisesmith16:07:03

prometheus is a TCP connection right?

noisesmith16:07:29

there are no deps on other riemann namespaces in that code, which is a good sign for reusability / example purposes

clj 4
dmaiocchi16:07:58

Ah nice not bad, I was actually looking to use the official prometheus Java client

dmaiocchi16:07:31

I have seen there is also clojure unofficial wrapper but it is outdated

noisesmith16:07:02

I'd be slightly suspicious of what makers of an official java client for an http API consider "useful" :D

😁 8
dmaiocchi16:07:03

The http method is kind cool

Pick Feky16:07:16

my graduate advisor name was rieman (but not the Riemann) and my last project name was prometheus.

dpsutton16:07:11

Not THE Riemann lol

noisesmith16:07:21

it's important to clarify

noisesmith16:07:43

I wouldn't trust someone who had a ghost as a graduate advisor for example

Pick Feky16:07:37

haha. but it's true except that he's korean (Rieman Kil <- Full name)

Pick Feky16:07:20

his granfather was math teacher, and named him rieman

dmaiocchi16:07:10

Regarding à prometheus exporter, basically it creates an API where the prometheus scrape from

dmaiocchi16:07:36

So it doesn't push, looking at riemann example there. It might be that example use not usual conf

dmaiocchi16:07:45

E. G looking at line 127

noisesmith17:07:03

that's a host that code will push to - it's not getting pulls

noisesmith17:07:11

it's definitely using http/post

👍 4
dmaiocchi17:07:26

Yep, imho afaik I need to build a server which respect the prometheus api of metrics, this will then scraped by prometheus

dmaiocchi17:07:01

(in normal cases afaik this Is how it works)

Andrea Imparato17:07:43

hi, anyone here use compojure-api?

noisesmith17:07:45

ahh, so the way riemann uses prometheus is abnormal then

Andrea Imparato17:07:18

i'm trying one of the basic examples and I get

Caused by: clojure.lang.ExceptionInfo: Call to clojure.core/fn did not conform to spec.
#:clojure.spec.alpha{:problems ({:path [:fn-tail :arity-1 :params], :pred clojure.core/vector?, :val clojure.core.unify/var-unify, :via [:clojure.core.specs.alpha/params+body :clojure.core.specs.alpha/param-list :clojure.core.specs.alpha/param-list], :in [0]} {:path [:fn-tail :arity-n], :pred (clojure.core/fn [%] (clojure.core/or (clojure.core/nil? %) (clojure.core/sequential? %))), :val clojure.core.unify/var-unify, :via [:clojure.core.specs.alpha/params+body :clojure.core.specs.alpha/params+body], :in [0]}), :spec #object[clojure.spec.alpha$regex_spec_impl$reify__2509 0x224b4d61 "[email protected]"], :value (clojure.core.unify/var-unify [G__1829 G__1830 G__1831 G__1832] (clojure.core/if-let [vb__1822__auto__ (G__1832 G__1830)] (clojure.core.unify/garner-unifiers G__1829 vb__1822__auto__ G__1831 G__1832) (clojure.core/if-let [vexpr__1823__auto__ (clojure.core/and (G__1829 G__1831) (G__1832 G__1831))] (clojure.core.unify/garner-unifiers G__1829 G__1830 vexpr__1823__auto__ G__1832) (if (clojure.core.unify/occurs? G__1829 G__1830 G__1831 G__1832) (throw (java.lang.IllegalStateException. (clojure.core/str "Cycle found in the path " G__1831))) (clojure.core.unify/bind-phase G__1832 G__1830 G__1831))))), :args (clojure.core.unify/var-unify [G__1829 G__1830 G__1831 G__1832] (clojure.core/if-let [vb__1822__auto__ (G__1832 G__1830)] (clojure.core.unify/garner-unifiers G__1829 vb__1822__auto__ G__1831 G__1832) (clojure.core/if-let [vexpr__1823__auto__ (clojure.core/and (G__1829 G__1831) (G__1832 G__1831))] (clojure.core.unify/garner-unifiers G__1829 G__1830 vexpr__1823__auto__ G__1832) (if (clojure.core.unify/occurs? G__1829 G__1830 G__1831 G__1832) (throw (java.lang.IllegalStateException. (clojure.core/str "Cycle found in the path " G__1831))) (clojure.core.unify/bind-phase G__1832 G__1830 G__1831)))))}
:thinking_face:

dmaiocchi17:07:20

It might require a special conf of prometheus yep

noisesmith17:07:53

@andrea.imparato that looks like your fn definition didn't have a vector arglist

👍 8
dmaiocchi17:07:23

@andrea.imparato feel free to ask in beginners channel.. And post you code

Andrea Imparato17:07:46

which is the beginners channel? 🙂

noisesmith17:07:13

@darioszr going back to your original question - I linked riemann because I have a lot of respect for the author's code, but clearly it's not what you need - but there is a wrapper from the soundcloud company as well https://github.com/soundcloud/prometheus-clj

noisesmith17:07:11

this one is likely worth checking out as well https://github.com/xsc/iapetos

dmaiocchi17:07:15

Thx! the SoundCloud I saw it. The 2nd didn't know. Imho since the 1st SoundCloud is kind outdated, I was reflecting if it is worth perhaps to just pick the Java official one and then wrap it directly in my exporter

dmaiocchi17:07:12

But I don't have much experience with Java but it could motivated me to a least try:grin:

noisesmith17:07:15

you could use either of those as a template and use your own interop even - I doubt they are much different from one another under the hood

noisesmith17:07:39

I learned java (to the extent I know it) via clojure interop - once you figure out how to read javadoc it's not so bad

👍 4
dmaiocchi17:07:18

I might have seen Java from bade codebases.. 😂 With hibernate etc

noisesmith17:07:26

as long as the API you are using doesn't mandate annotations and concrete inheritance, that's when things get kind of ugly

noisesmith17:07:45

oh - javadoc is good enough that you shouldn't need to read the java code to do interop

👍 4
noisesmith17:07:32

and it's derived from the source, so even if docstrings are bad, args and arg types, and return values which is what really matters will be right

dmaiocchi17:07:32

OK I will try, thx let see 👍 😁

cddr18:07:58

Hey Folks, I'm trying to generate some specs and write them to a file. When I pretty print them, they look like so...

(s/def
 ::addresses
 (s/keys
  :req-un
  [:addresses/addressable-id
   :addresses/addressable-type
   :addresses/city
Is there any way I can convince the pretty-printer to get the ::addresses to come out on the same line as the s/def?

Alex Miller (Clojure team)18:07:26

yes, there are some tricks if you use the "code" modes of the pretty printer

Alex Miller (Clojure team)18:07:39

let me find you a link to an example

Alex Miller (Clojure team)19:07:03

user=> (require '[clojure.spec.alpha :as s] '[clojure.pprint :as pp])
nil
(def c `(s/def
 :user/addresses
 (s/keys
  :req-un
  [:addresses/addressable-id
   :addresses/addressable-type
   :addresses/city])))
#'user/c
user=> (binding [pp/*code-table* (assoc @#'pp/*code-table* 'clojure.spec.alpha/def #'pp/pprint-hold-first)] (pp/with-pprint-dispatch pp/code-dispatch (pp/pprint c)))
(clojure.spec.alpha/def :user/addresses
 (clojure.spec.alpha/keys
   :req-un
   [:addresses/addressable-id
    :addresses/addressable-type
    :addresses/city]))

👏 8
😍 4
Alex Miller (Clojure team)19:07:54

not that here c is your code (as data) - it's important that you fully namespace the symbols (I'm using back tick to get that)

Alex Miller (Clojure team)19:07:40

then you you want to pprint, with specialized dispatch, using the code-dispatch which already knows how to handle most clojure code, and extend the code-table to understand how to handle s/def

Alex Miller (Clojure team)19:07:31

you can look at the pprint code to see more about the default code table and how things like pprint-hold-first are defined (which is, again, another dark arts subset of this dark art) :)

mpenet19:07:39

cljfmt can do that too I think

Alex Miller (Clojure team)19:07:54

undoubtedly, but this is "in the box" with both clj and cljs

Alex Miller (Clojure team)19:07:41

and this is an extensible system so you can do other customized things with your own macros or whatever

Alex Miller (Clojure team)19:07:09

like you could tweak this to put the req keys next to the :req-un if you were determined enough (see pprint-let for something probably similar)

lilactown22:07:57

there are libraries that do that

lilactown23:07:16

yep, that's the one that came to mind for me

lilactown23:07:23

I haveen't used any of them

Alex Miller (Clojure team)23:07:24

We might add something like this in spec 2 (although it’s prob going to involve some other things that don’t exist yet)

Evan Rinehart23:07:59

I just tried nightcode. Seems cool but I do not see a way to reorganize code with the keyboard

ag23:07:27

is it possible to determine and list all the methods registered for defmulti?