practicalli / clojure-web-services

Develop production grade server-side web services and APIs using Clojure and REPL driven development
https://practical.li/clojure-web-services
Creative Commons Attribution Share Alike 4.0 International
11 stars 14 forks source link

Starting web server #47

Closed practicalli-johnny closed 1 year ago

practicalli-johnny commented 3 years ago

writing a function to call specifically from the REPL, and then calling it from -main with a different argument:

(defn start [& [join?]] (jetty/run-jetty-app {:port 9091 :join? join?})) ;; call from REPL as (start) (defn -main [& args ] (start true))

but what do you think of using future like that if ring didn’t had :join? ?

It will mean your -main will just exit when you run it from the command line -- which is not what you want.

It also means you won't be able to stop the server within your REPL either.

run-jetty should return a handle for the server, that you can call .stop on.

See this code https://github.com/seancorfield/usermanager-example/blob/develop/src/usermanager/main.clj#L140-L175 which has logic to start/stop either Jetty or http-kit (so it's a bit more complex that what you're doing). (edited)

It captures the result of calling (jetty/run-jetty app {:port ... :join? false}) so that it can .stop it later.

http-kit works differently -- it starts in the background anyway, and returns a handle to the server that you invoke with no args to stop it. Another reason not to use future like that is that if the server throws an exception, you won't see it -- the process inside the future will just silently die. You only see the failure if you deref the future -- when it will throw the exception that the process failed with.

practicalli-johnny commented 1 year ago

https://practical.li/clojure-web-services/app-servers/create-server.html initial content.

Review if further information can be added.

practicalli-johnny commented 1 year ago

Simple restart within the REPL: https://practical.li/clojure-web-services/app-servers/simple-restart/

Atom to hold server reference: https://practical.li/clojure-web-services/app-servers/atom-based-restart/