How to run the backend server/code in re-frame template? - clojure

Using lein re-frame template I see that there's a -main function that runs a jetty server for the backend code located in clj/. Using emacs I've been able to cider-jack-in-cljs and get the frontend running in the browser, hot reload works and all but I've tried to add routes to the routes function in handler.clj inside the clj/ and haven't been able to figure out how to get that "backend side" of things to work.
I found this other question sort of related but in that case the user just wanted to know why the clj/ folder was there, not how to run that code. I haven't been able to find any documentation so far, any help is greatly appreciated.
PD: I know that having the backend and frontend in the same project/repo is not recommended (it's mentioned in the other question I linked above) but I just want to get a simple "first app" working and running first and then hopefully get the backend out into another project/repo.

Assuming you created the project with something like lein new re-frame myapp +handler the code to start the server is on the file src/clj/myapp/server.clj
You can open the file and run cider-jack-in-clj, which will ask if you want to launch lein or shadow-cljs. Since it's a CLJ file, choose lein. Once CIDER starts, you can evaluate the -main function (eg. (-main)) to start the server.
You can open the URL at http://localhost:3000 and Jetty will serve the resources that are already compiled by shadow-cljs, so you'll see the same output as viewing the other port from CLJS directly.
Note that the backend code from the template starts the Jetty server but won't help with reloading the backend. To see how enable hot reloading for the backend, check https://github.com/ring-clojure/ring/wiki/Setup-for-development

Related

Full-Stack Clojure: How to get a Browser REPL and a Server REPL simultaneously during development

I'm developing my first full-stack Clojure application. I've managed to get the following working properly in Linux Mint:
leiningen
figwheel + garden[auto] = Interactive SPA development with hot code and CSS reloading
leiningen REPL for Server with rebel-readline, start server at REPL,
and serve SPA to browser
leiningen uberjar compiles, jar file runs, and SPA is served by
server
What I can't figure out how to do is get a development environment set up that allows me to have a server REPL and an SPA REPL at the same time so that I can integrate sente for websocket support and monitor the re-frame app-db state within the SPA REPL. I suppose, ideally, I'd like to have figwheel + gargen[auto] running with the SPA communicating with the server via the sente websocket.
My response is not really what you asked for, but it might interest you nonetheless I believe so bear with me.
Did you try the lightmod editor? It aims to be a fullstack editor with minimal setup. In fact, when you launch it you can preselect template for your project, and get automatically a REPL for backend, and a REPL for your SPA, with auto-reloading etc. It does not have all of Emacs goodies but I found it really good to get a quick setup.
It turns out that the sente function used within the ClojureScript app, named make-channel-socket!, called to initiate the connection back to the sente-websocket-server running on the app's server has a third parameter, options map, key named :host. By default, sente sets the :host value to the server from where the ClojureScript app was loaded.
If one runs 'lein figwheel' and loads the ClojureScript app via the figwheel server, sente, by default, trys to connect to the figwheel server, which is NOT, of course, running a sente websocket server.
For development mode, one must:
1. Start the app server in a terminal (e.g., 'lein repl')
2. Compile the ClojureScript app with a :host value of 'localhost:'
3. Run the ClojureScript app via figwheel in another terminal
4. Connect to the figwheel server from the browser (e.g., 'localhost:3449')
When the ClojureScript app is loaded into the browser via the figwheel server it executes the sente connection call that now makes a connection to the sente websocket server running in the app server.
This is FANTASTIC! Now one can use figwheel, with its REPL for the browser, and run the app server, with its REPL, at the same time.

Webpack prod config vs dev config issues

I am trying to reuse one webapp that's been open sourced a while ago that was written using some Django and ReactJS... Now I am a devops engineer so my skillset when it comes to JS and even Django are fairly limited so I am stuck .. My main problem is that this webapp can run just fine locally.. so I can start it and connect using http://localhost:8000 , but whenever I try and set it up on a server and make it "public" for the internal network it fails with accessing all the JS assets.
I know the problem comes with my webpack configs but I can't sort it out.. Been trying all day but I can't even find the proper documentation since it's using Webpack 2.5.
https://github.com/tsprasath/estate/tree/master/webpack
I am attaching the link to the webpack configs from the repo.. If anyone can at least point me to the right thing to look at, that would be helpful.
Thanks in advance!
I dont think its an issue with your webpack. You are trying to run your react app (which is at client side) from django server. I believe you will need to use some kind of middleware to let django know that it needs to use static files generated for react using webpack. I dont know exactly how its done, but same procedure is followed if react app needs to be served from node/express server.
Or see if this helps: https://github.com/nicholaskajoh/React-Django

How can I reload code in a Clojure pedestal repl for a quick development workflow?

I am using Pedestal (pedestal.io) and in development am running the server from inside a repl.
When I make a change to the application code (such as changing the html template) how can I have the repl reflect these changes automatically upon save of file and have them show up on browser refresh?
You might want to refer to the auto-reload-server sample which uses ns-tracker for watching changes in your code.

How to integrate figwheel with ring server to get back-end auto-reload?

I am new to clojure/clojurescript web development.
Use lein ring server and the modifications(for example routes) will be automatically reloaded to the server(hot-deploy).
Use lein figwheel, and it will spy on the changes of cljs source files and updated to the page accordingly.
Now I started figwheel and modified the back-end code(for example the routes). After I reload the page, the modified contents was not updated.
So Is there a way to integrate figwheel with the ring server so that the back-end change can be updated automatically?
The ring middleware wrap-reload will do this for you. There is also a very nice leiningen template called Chestnut which will set up a project for you with Figwheel and an auto reloading Ring backend.
This question shows an example of wrap-reload usage Compojure development without web server restarts

View changes to Selmer templates without restarting server

I'm creating a web app with Clojure using Selmer templates. I'm using Jetty, and I have to restart it to see my changes. I want to see my changes without restarting the server. How can I achieve this?
I suggest you to migrate to http-kit to get hot code reload, it's very easy
Add [http-kit "2.1.18"] to your project.clj and org.httpkit.server to your use block, and just replace run-jetty with run-server. See details in the link above