This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-05-18
Channels
- # announcements (11)
- # architecture (1)
- # aws (2)
- # beginners (101)
- # calva (3)
- # clj-kondo (2)
- # cljdoc (1)
- # cljs-dev (20)
- # clojure (92)
- # clojure-dev (34)
- # clojure-france (1)
- # clojure-italy (2)
- # clojure-spec (13)
- # clojure-uk (3)
- # clojurescript (28)
- # cursive (7)
- # data-science (58)
- # datomic (18)
- # docs (1)
- # fulcro (11)
- # graphql (3)
- # hoplon (25)
- # klipse (3)
- # leiningen (4)
- # nrepl (3)
- # off-topic (57)
- # pedestal (1)
- # portland-or (1)
- # precept (1)
- # reagent (5)
- # reitit (2)
- # rewrite-clj (4)
- # ring (1)
- # shadow-cljs (97)
- # sql (90)
- # tools-deps (2)
map-indexed
keeps all the items and processes them, keep-indexed
with drop items that produce nil
from the predicate (updated to be accurate about nil
vs false
).
user=> (map-indexed (fn [i x] (when (< i x) x)) (repeat 10 5))
(5 5 5 5 5 nil nil nil nil nil)
user=> (keep-indexed (fn [i x] (when (< i x) x)) (repeat 10 5))
(5 5 5 5 5)
user=>
i'm trying to parse a POST request on an app that i did not start as a lein new compojure myapp
i've gotten as far as (POST "/notify-by-email-please" {body :body} (slurp body))
which outputs a string like email=
not bad, but i don't know how to parse raw text. the normal compojure destructuring tells me it's a byteStream
doesn't really notice my param names... guessing i don't have the right middleware
i'm trying to use HTTP kit instead of ring ...
@sova what is the body of the POST request? JSON? Encoded form parameters?
There's probably a middleware out there that does what you want, turning that data into a hash map.
I'm using a simple HTML form with method=POST
it's just a landing page with a "submit your email for when this product launches"
and thanks sean, always most helpful to all.
I always use the ring-defaults
middleware stack for apps -- it takes care of so much basic stuff.
For simple stuff, I generally disable the CSRF token stuff...
Take a look at https://github.com/seancorfield/usermanager-example/blob/master/src/usermanager/main.clj as a basic setup. Pretty sure that would solve your problem: form POSTs become hash map elements in :params
in the Ring req
uest.
so theoretically i could pull them straight out of the params key
the middleware just makes that more palatable?
That little app does POSTs of form data and deals with it as hash maps.
Yup. Ring gets its power from middleware -- and you really need that "default" stack to make your like easier.
Ah, so even something like compojure destructuring depends on having the right middleware there
Well, Compojure will do the destructuring -- but, as you've seen, that only gets you so far.
:body #object[org.httpkit.BytesInputStream 0x77a2b38d BytesInputStream[len=17]]
Well... that is the body ๐
It's not a very helpful body, but it is consumable ๐
Middleware will turn that into a hash map for you.
I see. that's exactly what I want
I think we use ring-defaults
in all our web apps, even our simple REST APIs.
i'm using server/run-server (http-kit)
Yeah, our apps all run on either Jetty or http-kit -- per that usermanager example: you can choose which server to start with at runtime.
We generally use Jetty because New Relic is more compatible with it for monitoring/metric reporting purposes.
What's New Relic?
But we initially used http-kit because we were having some threading issues with Jetty on an older version.
New Relic is an application monitoring service.
okay, now i got the middleware in there correctly, invalid anti forgery
https://newrelic.com/ we use it heavily to monitor performance in production.
you were saying it's probably fine to ignore the anti forgery / CSRF...
new relic looks neat
Yeah, just disable that, like the example does.
We've been using New Relic for years. Love it!
Not cheap, but awesome if you have critical production services.
It shows timings for all your app routes, it shows all your database activity in each request... it can provide alarms for pretty much any conditions, and send alerts to email and Slack etc.
woo the params show up in the request now
thanks man
Also can do front end performance analysis -- browser stuff like network and rendering and DOM times.
So, are you building a cool, secret, soon-to-be-launched product with Clojure/Script @sova?
it's just a "sign up to be notified" page for now ๐
Can't say it will be "soon" launched, but the basic framework is coming along.
thanks very much sean
I downloaded the example code in https://pragprog.com/book/shcloj3/programming-clojure-third-edition . There was a very minimal deps.edn:
{
:deps
{org.clojure/clojure {:mvn/version "1.9.0"}
org.clojure/test.check {:mvn/version "0.9.0"}
}
}
I added path for src and alias for test runner:
:test-runner {:extra-paths ["test"]
:extra-deps {com.cognitect/test-runner {:git/url ""
:sha "209b64504cb3bd3b99ecfec7937b358a879f55c1"}}
:main-opts ["-m" "cognitect.test-runner"]}
But when trying to run the tests nothing is found:
clj -Atest-runner
Running tests in #{"test"}
Testing user
Ran 0 tests containing 0 assertions.
0 failures, 0 errors.
What am I missing here?Where are your tests?
The tests are in directory "test".
I'm trying to remember whether the test runner expects your test files to have test
in the namespace name...
Let me just try to repro...
Ok. Thanks!
Maybe I'm just too tired, it's already midnight here in Finland. ๐
Oh, you don't have the -d
option
I would help but I actually don't have a copy of the original repo or creds to get it on this machine
:main-opts
need to include "-d" "test"
iirc we had a script to run all the tests (the book vastly pre-dated deps.edn)
Ok. I was just wondering since the same alias I used above worked with my personal project.
I try to add the ยด-dยด option...
-d default is test
Nope. didn't work. Well, I think I try to continue tomorrow.
but -r prob needs to be set
Regex for namespaces to test. Defaults to #".*-test$" (i.e, only namespaces ending in '-test' are evaluated)
so -r ".*" maybe?
Let's try...
Ah, so it is the namespace suffix... that was going to be my next suggestion ๐
I didn't realize -d
defaulted to "test"
-- good to know.
That might be the reason since in my personal project all test namespaces are ending with "test", e.g. domain_test.clj.
Yup, that's the standard convention.
foo.bar
for the source code, foo.bar-test
for the test code.
But -r "*"
didn't work either.
It's a regex.
it's a regex
Hahaha...
I'll try to change one test namespace ending with "test"...
clj -A:test-runner -r ".*"
should work (as a test from the command-line)
(I just tried that locally)
Yep. That's the reason. Now it ran tests for the file I renamed from "chat.clj" to "chat-test.clj".
Yep. Now test-runner is running the tests. Thanks!
If you use clj-new
to create a new project, it creates an appropriately named test file.
And the default deps.edn
file in a new clj-new
-created project is
{:paths ["resources" "src"]
:deps {org.clojure/clojure {:mvn/version "RELEASE"}}
:aliases
{:test {:extra-paths ["test"]
:extra-deps {org.clojure/test.check {:mvn/version "RELEASE"}}}
:runner
{:extra-deps {com.cognitect/test-runner
{:git/url ""
:sha "76568540e7f40268ad2b646110f237a60295fa3c"}}
:main-opts ["-m" "cognitect.test-runner"
"-d" "test"]}}}
Saves quite a bit of typing.
Ok. Thanks.
In those examples I had to comment the tasklist example - there were some genclass stuff that broke the tests. After commenting that ns the test runner ran all tests. Got to go to sleep now. Kari continues Clojure studies again tomorrow. BTW - Programming Clojure, 3rd ed is excellent. I started to read it again maybe, 3rd or 4th time now. I have noticed that some books need to be read several times before they are assimilated into the existing clojure knowledge in my head.
Ah, right, you're working from a book whose code predates deps.edn
... sounds like you're making good progress tho'! Good work!
nope, 3rd ed has deps.edn in it
last minute switch before publication - just barely made it
This time I thought that I also experiment with the book code examples. I really want to be more fluent with Clojure. And more idiomatic.