CLOJURE encoding using Emacs and Cider

I’m new to Clojure coding, and through some articles to set up Emacs for development.
The setup itself works as designed, but I’ve been running the server from a Cider session and doing anything Kill Emacs completely when the code changes. Or, I need to find the server process from the shell session and kill it from there. This is far from ideal.

Clojure-Getting Started with Tools/web. clj

(defn -main [& [port]]
(let [port (Integer. (or port (env :port) 5000))]
(jetty/run-jetty (site #'app) {:port port :join? false})))

Cider Conference

 clojure-getting-started.web> (defonce server (-main))

After starting the server, I will get the following error through the (server) function:

< pre>1. Unhandled java.lang.ClassCastException
org.eclipse.jetty.server.Server cannot be cast to clojure.lang.IFn

The error message makes sense, but how to use Emacs Refresh my code base?
I think (and hope) there is a better way than starting a shell session on Emacs and killing the processes there…

Suppose what you want is to run jetty server and load or reload the code when you change the code from emacs. Although the suggestions already given are good, it may be better than when you started What you need is more complicated. My suggestion is to use some of lein’s templates, which will set the default environment and workflow for you. Then, you can learn more about this default value until you get a workflow that suits you. My suggestion is Start with the default compojure template, which is

lein new compojure my-project

This creates a simple project, including the basic ring And compojure library and lein plugin and a simple dev configuration file.

Edit the src/my_project/handler.clj file and add ring.middleware.reload middleware, for example

p>

(ns my-project.handler
(:require [compojure.core :refer :all]
[compojure.route :as route]
[ring.middleware.reload :refer [wrap-reload]]
[ring.middleware.defaults :refer [wrap-defaults site-defaults]]))

(defroutes app-routes
(GET "/" [] "Hello World")
(route/not-found "Not Found"))

(def app
(-> app-routes
wrap-reload
(wrap-defaults site-defaults)))

The wrap-reload middleware will cause the code to be reloaded when it is modified. You do not need to reload Start the jetty server to make the code changes take effect-just reload the page.

In the terminal running

lein ring server

Either

lein ring server-headless

This will start a jetty server listening on port 3000. Then from emacs, you can Open a cider code to use when writing the code. Unless you make changes to the project.clj file, there is no need to restart the server process. The same as the cider process.

Then, once you are satisfied with it, Please check the lein-ring documentation. There you will find information on how to set up the repl.clj file in your project. Once you do this, you can do something similar

< pre>lein repl

Then do something similar from that repl

(start-server)

This will start Server. Then you can switch to emacs instead of running cider-jack-in, you can make a cider connection, which will connect him already running repl instead of starting the second repl session. After that, if you decide to start viewing clojurescript, You can check some of the default templates of the clojure clojurescript application. I like figwheel and the use of reagents very much, so I also found that the reagent templates are very good.

There are many lein templates, I think running them is very useful, take a look What do they do. Then I tend to choose the features/options I like.

I am new to Clojure coding, and I set up Emacs for development through some articles. < br>The setup itself works as designed, but I have been running the server from a Cider session and killing Emacs completely when making any code changes. Or, I need to find the server process from the shell session and kill it from there. This is far Not ideal.

Clojure-Getting Started with Tools/ web.clj

(defn -main [& [port]]
(let [port (Integer. (or port (env :port) 5000))]
(jetty/run-jetty (site #'app) {:port port :join? false))))

Cider Conference

clojure-getting-started.web> (defonce server (-main))< /pre> 

After starting the server, I will get the following error through the (server) function:

1. Unhandled java.lang.ClassCastException
org.eclipse .jetty.server.Server cannot be cast to clojure.lang.IFn

The error message makes sense, but how do I use Emacs to refresh my code base?
I think (and hope) there is a better way than starting a shell session on Emacs and killing the processes there...

Suppose you What I want is to run the jetty server and load or reload the code when changing the code from emacs. Although the advice that has been given is good, it may be more complicated than you need at the beginning. My advice is to take advantage of lein's Some templates, which will set the default environment and workflow for you. Then, you can learn more about this default value until you get a workflow that suits you. My suggestion is to start with the default compojure template, which is

< /p>

lein new compojure my-project

This creates a simple project, including basic ring and compojure libraries and lein plugins, and a simple dev configuration File.

Edit the src/my_project/handler.clj file and add ring.middleware.reload middleware, for example

(ns my-project.handler 
(:require [compojure.core :refer :all]
[compojure.route :as route]
[ring.middleware.reload :refer [wrap-reload]]
[ring.middleware.defaults :refer [wrap-defaults site-defaults]]))

(defroutes app-routes
(GET "/" [] "Hello World")
(route/not-found "Not Found"))

(def app
(-> app-routes
wrap-reload
(wrap-defaults site-defaults)))

The wrap-reload middleware will cause the code to be reloaded when it is modified. You don’t need to restart the jetty server for the code changes to take effect-just reload the page.

p>

In terminal operation

lein ring server

or

lein ring server-headless

This will start a listening port 3000 jetty server. Then from emacs, you can open a cider code to use when writing code. Unless you make changes to the project.clj file, there is no need to restart the server process. The same as the cider process.

< p> Then, once you are satisfied with this, check the lein-ring documentation. There, you will find information on how to set up the repl.clj file in your project. Once you do this, you can do something similar< /p>

lein repl

Then do something similar from that repl

(start-server )

This will start the server. Then you can switch to emacs instead of running cider-jack-in, you can make a cider connection, which will connect him already running repl instead of starting the second repl Session. After that, if you decide to start viewing clojurescript, you can view some of the default templates of the clojure clojurescript application. I really like figwheel and the use of reagents, so I also found that the reagent templates are very good.

There are a lot of lein Templates, I find it very useful to run them and see what they do. Then I tend to choose the features/options I like.

Leave a Comment

Your email address will not be published.