Fork me on GitHub
#beginners
<
2018-04-27
>
jakob10:04:38

Hi, I have a clojurescript app with clojure backend and now I am trying to send some static data (config) from the backend to the frontend on page load. I was thinking to send it as a JSON structure that the clojurescript app picks up. 1. Is there a better way? 2. If this is the best way, how do i convert a clojure map to a JSON map?

troglotit10:04:48

If you're the only client of that config (no third parties), then Transit (https://github.com/cognitect/transit-format) would be most efficient. But if you want to use JSON without Transit, then clj->js then calling (js/JSON.stringify (clj->js {:a 5})) would be the way https://cljs.github.io/api/cljs.core/clj-GTjs

jakob11:04:17

excellent answer! Thanks @troglotit

jakob11:04:22

but how can i convert to JSON on the backend_ clj->js seem to only work in clojurescript?

ūüėÖ 4
curlyfry11:04:00

https://github.com/dakrone/cheshire is a popular library for handling json in Clojure

‚ěē 4
curlyfry11:04:07

@karl.jakob.lind Another approach if the data is truly static is to keep it in a .cljc file that can be used both from Clojure and ClojureScript

jakob11:04:34

:thumbsup: thanks!

gklijs11:04:20

I have a constants.cljc with some maps, works great.

lum14:04:54

Hi, when having two luminus projects running with lein run should one change the default port nr. 7000 for one of them? I'm asking this because tried to find the name of processes with lsof -i but couldn't find any in that port.

troglotit15:04:56

Is there anyone tried to build an app with Reagent/Rum/etc with Apollo? Maybe there are blogposts or tutorials

gklijs15:04:51

I used re-graph, https://github.com/oliyh/re-graph to connect to a GraphQL endpoint. It's a nice small library that works well.

Karol Wójcik18:04:14

Hello I am trying to wrap my head around ring routing and the role of compojure in making the ring handlers. After some investigation, I found that the ring accepts the handler in which we have to explicitly match the route. The role of compojure is then to create a handler which on request calls (handler request) and if the route matches then it's called. Otherwise the nil is returned and other handlers for the route are called. I am very curious if I am right. If so then I would highly appreciate if you could explain to me why the ring is implemented in fashion when to match the route every single handler is taken into account. Wouldn't be better to have some sort of map which when the request occurs then matches to correct handler?

mg18:04:03

@kwcharllie379 Ring itself has no concept of routing. It defines a simple abstraction over HTTP request & response - requests are maps with certain keys, and responses are maps with certain keys. A ring handler is a function that takes a request and returns a response. Compojure adds a layer of routing on top of this, as well as a number of syntactic and developer convenience features. When you use compojure’s routes function or defroutes macro, the result is a ring handler.

Karol Wójcik18:04:02

@michael.gaare Yes I understand it very well I think. But don't you think it's some kind of time consuming to go through all handlers in order to match the route?

mg18:04:20

@kwcharllie379 Well, that’s getting into the implementation details of how compojure handles routing. bidi is another routing library that uses a trie for this rather than compojure’s more linear approach

Karol Wójcik18:04:56

Yes this is what I tried to say it's linear approach.

noisesmith18:04:11

Sorry, yes, linear on count routes defined

noisesmith18:04:36

Which is likely a very small n

Karol Wójcik19:04:07

I do not think so. Simple route like "/users" has 4 handlers because of GET, POST, DELETE, PUT. So for evey route there are at least 1 to 4 handlers. Which in my opinion is not very optimal.

noisesmith19:04:34

If that's a likely bottleneck, clojure won't perform well enough for the application

noisesmith19:04:50

Also, linear search over a collection that small is faster than a hash lookup on real world cpus

Karol Wójcik19:04:13

Ooo really I didn't know that. Thank you very much for the clarification.

Karol Wójcik20:04:14

I really appreciate that.

rauh18:04:18

btw, pedestal has 3 routers for you to choose from. Fast, faster & slow/flexible

Karol Wójcik18:04:06

Guide me please which path should I take? What do you prefer? I came from the Node.js world and I do not know which path should i take 1. Compojure and compojure-api 2. Pedestal

noisesmith18:04:20

Pedestal also means not using ring, right?

mg18:04:40

Pedestal allows you to use ring, but it also has its own async middlewares thing that can be more performant if you target that

Karol Wójcik18:04:57

So pedestal is right choice, right?

Karol Wójcik18:04:16

Ring is some kind of express as I can see

mg19:04:41

Personally I’d go with compojure & compojure-api. Coming from node.js, it has this nice swagger UI built in

Karol Wójcik19:04:41

Ok maybe it's the right path, because I can find a lot of similarities between Node.js and Ring & Compojure and since I am beginner it would be better to explore the Clojure in the area in which I am comfortable. Thank you @michael.gaare @rauh @noisesmith

ackerleytng23:04:42

how do i use clojure.spec to validate the existence of a file? should i be doing that?

ackerleytng23:04:04

i'm reading in an edn file as a config file and i'd like to validate the config