Fork me on GitHub
#clojure
<
2018-05-26
>
datran02:05:43

I've got some questions about deployment. I've uberjarred up my web application and I'm using caddy to reverse proxy traffic to it, however all the absolute links are broken - for example loading my css file ("/css/site.css") and js file ("/js/app.js")

datran02:05:10

I know this isn't strictly a clojure problem, but maybe someone here has some experience with it

myguidingstar03:05:46

@datran what do you mean by "broken"?

myguidingstar03:05:43

how did you start your uberjarred app?

datran03:05:58

I started the app with java -jar myjar.jar

datran03:05:13

and by broken I mean the links are returning a 304 instead of a 200

datran03:05:32

but everything works locally, so I suspect the webserver

myguidingstar03:05:16

is it in your local machine or on a server elsewhere?

datran03:05:51

it's on a server across the room from me

datran03:05:17

and I can access it on my local network

myguidingstar03:05:18

let's assume you started your app on the server on port 8080

myguidingstar03:05:59

and you ssh into the server, try curl localhost:8080/css/site.css

myguidingstar03:05:16

is it 304 or 200?

myguidingstar03:05:06

good, so it's up

myguidingstar03:05:58

may I see your caddyfile?

datran03:05:05

sure, just a sec

datran03:05:51

I added the 'without' line to see if that would help, but it didn't seem to make any difference

myguidingstar03:05:25

without is for part of the request uri, not the base url (aka domain) part

datran03:05:14

ok, that makes sense why it's a no-op then

datran03:05:11

I might try just changing the links I have to stuff in my /public folder to be relative urls

datran03:05:17

since there are only the two of them

myguidingstar03:05:38

indeed it's a good practise, server softwares like nginx & caddy are better at serving assets than jetty/netty

datran03:05:53

hmm, how do you actually do that, though?

datran03:05:32

I've got a project structure with src/, resources/, env/, target/ etc

datran03:05:46

my css lives in resources/public/css/site.css

datran03:05:10

but my js gets compiled into target/cljsbuild/public/js/app.js

datran03:05:45

when I try just putting the full relative url into the link, that's a 404, it can't find it (resources/public/css/site.css)

datran03:05:14

which makes some sense because when it's all uberjarred up I don't think it has the same root

datran03:05:32

what does the inside of a jar look like? I can just unzip it, right?

myguidingstar03:05:01

the part resources/public is java/clojure thing. It's the ring middleware that serve it to the outside world

myguidingstar03:05:04

yup, it's just a zip

datran03:05:34

whoops, should not have done that at the base level of a directory

datran03:05:42

there's a lot of stuff in an uberjar

datran03:05:13

ok, but it does have a public/ directory with a css and a js directory inside

myguidingstar03:05:44

hmm, what does your html head part look like? <link rel="stylesheet" href="">

myguidingstar03:05:51

what's in that href?

datran03:05:13

(hic/include-css "public/css/site.css")] ;=> <link href="public/css/site.css" rel="stylesheet" type="text/css">

datran03:05:28

^ that's what I'm trying in my repl right now, that doesn't work in dev mode

datran03:05:48

the uberjar is the same as above, except switch the href for ="/css/site.css"

myguidingstar04:05:03

what's that in your public http://example.com site?

datran04:05:45

here's the live link if you want to see: https://shove-tussle.idle.horse/

datran04:05:57

thanks for talking me through this, btw

myguidingstar04:05:01

wait, you said 304, not 404 right?

myguidingstar04:05:17

your asset links look normal

myguidingstar04:05:26

304 is expected

myguidingstar04:05:54

it means "resource has not been modified since last requested"

datran04:05:04

hmm, that's right, but now I'm more confused

myguidingstar04:05:45

I guess as Caddy see the files haven't been changed, it tells browsers to use cached version

datran04:05:43

and when I actually view the source (at least for the css, the js is compiled with {:optimizations :advanced}, so lord knows what that should look like), it shows the correct file

datran04:05:05

but the js isn't executing or starting.

datran04:05:29

well, I think that means that the problem firmly lives in my code, so I'll just have to make sure I'm actually calling the start function

datran04:05:21

but it does work on the local network, so that doesn't make sense

myguidingstar04:05:59

my console shows "SecurityError: The operation is insecure."

myguidingstar04:05:07

maybe something not allowed with https?

datran04:05:52

ok, that's right. so I'm going to think this through out loud

datran04:05:35

I send a request, and caddy makes sure that you're using https

datran04:05:59

so we receive an encrypted request, which caddy forwards along to my jar

myguidingstar04:05:01

enable http version of the app and see if it works

datran04:05:22

do you know how to do that? I can look it up if you don't have it on the tip of your head

myguidingstar04:05:51

please help look it up 😄

datran04:05:16

no problem 🙂

myguidingstar04:05:51

my caddy understanding is a lazy sequence 😄

datran04:05:22

wait, I was using firefox, chrome gives a bit more useful error message

datran04:05:46

> Mixed Content: The page at 'https://shove-tussle.idle.horse/' was loaded over HTTPS, but attempted to connect to the insecure WebSocket endpoint '<ws://shove-tussle.idle.horse/ws>'. This request has been blocked; this endpoint must be available over WSS.

myguidingstar04:05:34

🙂 you found it

datran04:05:49

ok, so I've added the websocket directive to the caddyFile, let's see if that works

datran04:05:11

I think my dns is on a 5min ttl, so we might have to wait to see it in effect

myguidingstar04:05:56

why dns matters here?

datran04:05:44

it doesn't, it was just wishful thinking that the 2-second fix will work in 5 minutes

datran04:05:52

now I have to actually read documentation : p

datran04:05:08

ok, so it looks like I can fix it by using wss:// instead of ws://, but then I'll need a security cert for dev work, which I don't really like

datran04:05:34

or I could make it conditional based on whether its a prod vs dev build

datran04:05:36

I'm going to do a bit more reading in the hopes that I can accomplish what I want just in caddy, but I'm growing skeptical

datran05:05:18

ok, I think I'm just going to change my app to connect securely or not based on whether it's a prod build or not. But I'm off to bed. Thanks!

joelsanchez18:05:59

is there a shortcut for (doall (for?

andy.fingerhut18:05:53

I don't think so. You could define your own macro to do that, of course, if you use it that combo very commonly.

seancorfield18:05:22

@joelsanchez Depending on how complex your for expression is, maybe (into [] xf coll) would be clearer in intent? I used to use doall a lot when I first started with Clojure, trying to avoid tripping over lazy sequences, but now I feel that doall is a bit of a code smell...

joelsanchez18:05:17

doall is a bit of a code smell indeed but re-frame requires avoiding the laziness for subscriptions @seancorfield

joelsanchez18:05:23

I tend to rewrite using mapv sometimes but it's usually less readable (as UI code)

seancorfield18:05:49

Yeah, for can be very expressive...

joelsanchez18:05:34

I think I can write a forv helper tho 🙂 thanks

mpenet19:05:59

xforms from cgrand has a 'for' transducer and lots of other useful stuff

cvic20:05:21

Any clj_slack users here?

jonpither20:05:00

If I want to flip all bits, like the ~ operate in java.... is there a fn? We have flip-bit which takes an index, but I want flip-bits - flip all of them?

jonpither20:05:31

ah great - thanks @rakyi. Thanks @cvic - I need to read up

cvic20:05:30

Flip bits, not burgers 😄

cvic20:05:45

Well, figured out the clk_slack part...

clj-slack.core=> (for [user-id (range 1 500)]
            #_=>   (nth (get (clj-slack.core/slack-request connection "users.list" {:presence "true"}) :members) user-id)
            #_=> )

ExceptionInfo clj-http: status 429  slingshot.support/stack-trace (support.clj:201)
Ugh. Damn 429

cvic20:05:00

I must find another way...