Re-evalling in my repl doesn't seem to update the server. I've started off with the code given here https://code.thheller.com/blog/shadow-cljs/2024/10/18/fullstack-cljs-workflow-with-shadow-cljs.html
(ns recur.server
(:require [ring.adapter.jetty :as jetty]
[ring.middleware.file :as ring-file]
[ring.middleware.file-info :as ring-file-info]
[compojure.core :as cc]))
(defn my-handler [req]
{:status 200
:headers {"content-type" "text/plain"}
:body "hello world from my-handler"})
(cc/defroutes app-routes
(cc/POST "/api/test" req
(comment "this line wont run unless i restart the whole server")
;; (println (:body req))
{:status 200
:headers {"content-type" "text/plain"}
:body "hello world response"}))
(def handler
(-> #'app-routes
(ring-file/wrap-file "public")
(ring-file-info/wrap-file-info)))
(defn -main [& args]
(jetty/run-jetty handler {:port 3000}))
See the line with the comment. I've tried re starting the server from the repl (by running (repl/go), re evaluating all the functions etc. But for the line to take effect in the case of a post request I need to end the whole server and start it all over again.If anyone wishes to see the code in full it is here: https://github.com/Zakkkk/recur
(jetty/run-jetty handler {:port 3000}) needs to be (jetty/run-jetty #'handler {:port 3000}) -- see https://clojure.org/guides/repl/enhancing_your_repl_workflow#writing-repl-friendly-programs
(you did it for #'app-routes but not for handler and you may need it for my-handler references depending on how you write them)
I'm assuming you mean in -main? I don't use -main to start the server, I use repl/go in which case it starts with:
(jetty/run-jetty #'srv/handler
{:port 3000
:join? false})
the dev/repl.clj file is this:
(ns repl
(:require
[shadow.cljs.devtools.api :as shadow]
[recur.server :as srv]
[ring.adapter.jetty :as jetty]))
(defonce jetty-ref (atom nil))
(defn start
{:shadow/requires-server true}
[]
(shadow/watch :frontend)
(reset! jetty-ref
(jetty/run-jetty #'srv/handler
{:port 3000
:join? false}))
::started)
(defn stop []
(when-some [jetty @jetty-ref]
(reset! jetty-ref nil)
(.stop jetty))
::stopped)
(defn go []
(stop)
(start))
thanks for the article as well, ill give it a readMaybe a Compojure issue with inline code then, since it's all macros. If you switch to declaring routes that map to #'some-handler fns instead of inline code, that should solve that.
so you mean like each route in defroutes should be pointing to a function (with #'), instead of being inlined?
That's what I would do, yes.
I'll give it a go, thanks!