Fork me on GitHub
#babashka
<
2021-08-21
>
Oz09:08:40

Hey, I saw some cool examples for things done with babashka http-server, For example: https://github.com/kloimhardt/babashka-scittle-guestbook I want to try working with it, but I'm not sure how to set up the workflow. It seems that to update changes in guestbook.clj I need to restart the server, and to reflect changes in guestbook.cljs I have to refresh the page. I tried to load the file into nrepl but then it starts a server and won't recieve new input. I'm probably missing some basic concept about how all of this work. So: Is there a way to refresh the page on file change? Is there a way to reload the server on file change? Is there a way to do actual hot reload? Thanks!

Michaël Salihi09:08:43

Hi @ozfraier > I tried to load the file into nrepl but then it starts a server and won't recieve new input. The reason is that the @(promise) block the thread, so new evals are blocked. One solution is to commenting the last part (server block) using the discard reader #_ https://clojure.org/guides/weird_characters#_discard before evaluate the file in the REPL and then call core-http-server to manually start the server (or evaluate the form (srv/run-server home-routes {:port port})). After that you can evaluate your modified blocks as usual.

(defn core-http-server []
  (srv/run-server home-routes {:port port}))

#_ ; <-- Check the discard reader
(let [url (str ":" port "/")]
  (core-http-server)
  (println "serving" url)
  (browse/browse-url url)
  @(promise))

Oz10:08:37

Thank you @admin055 it works 🙂

Michaël Salihi10:08:49

You're welcome @ozfraier. Another solution (my preferred) is to use environment variable to exclude the server block when running the nrepl server dev=true bb --nrepl-server , so in your code you can put a condition against this env. It's what I'm doing in this project: https://github.com/prestancedesign/babashka-htmx-todoapp/blob/master/htmx_todoapp.clj#L232-L237

Michaël Salihi10:08:16

This way, we avoid the risk to commit with the discard reader. 👍

Oz10:08:35

neat! thanks

nate20:08:46

@ozfraier @admin055 another way to do this is to use the babashka.file property: https://book.babashka.org/#main_file

(when (= *file* (System/getProperty "babashka.file"))
  (let [url (str ":" port "/")]
    (core-http-server)
    (println "serving" url)
    (browse/browse-url url)
    @(promise)))
Then you can eval the file without accidentally running the server

5
Michaël Salihi21:08:26

Brilliant! This is definitely the solution that I will use from now on. Thanks @U0510902N!

Bobbi Towers21:08:02

For my latest attempt at the Exercism test runner I ditched everything and went with a babashka script. It will run whenever code is submitted on the new site, and its job is to just take a solution file and a test file and output a detailed report. I'm going about this very naively, but I ended up overriding the reporting methods in clojure.test to store their results in atoms and process their contents accordingly. The next case that I need to handle is what to do if either of the files contain invalid code. The error output is actually perfect, I just don't know the proper way of capturing this data while the script (or another?) is running so that it can be included in the report and displayed to the user. The script is here: https://github.com/exercism/clojure-test-runner/blob/845341d84494c3d8b087dffa1a7fba44ca086be5/test-runner.clj Any help or feedback would be appreciated, thanks! babashka

borkdude21:08:51

The error reporting stuff isn't exposed at this point but I want to do this in the near future

Bobbi Towers21:08:40

Maybe I could pick it apart first with rewrite-clj and only test the valid forms.