CodeKingPlusPlus
CodeKingPlusPlus

Reputation: 16081

network connected REPL with leiningen/ring/compojure/luminus

I am running a server with the luminus web framework which uses ring/compojure and I want to be able to connect to my code with a clojure repl. I understand that there is an nrepl server which stands for network repl and that you can connect to it via: `lein repl :connect user@host:port (or some various protocol, I've tried a few things).

The following code from repl.clj is created when you auto-generate a luminus project with `lein new luminus [project-name] (actually I added the browser-repl piece necessary to attach a clojurescript repl).

(ns sophia-site.repl
  (:require [cemerick.piggieback :as pig]
            [weasel.repl.websocket])
  (:use sophia-site.handler
        ring.server.standalone
        [ring.middleware file-info file]))

(defn browser-repl []
  (pig/cljs-repl :repl-env 
                 (weasel.repl.websocket/repl-env :ip "localhost" :port 9001)))

(defonce server (atom nil))

(defn get-handler []
  ;; #'app expands to (var app) so that when we reload our code,
  ;; the server is forced to re-resolve the symbol in the var
  ;; rather than having its own copy. When the root binding
  ;; changes, the server picks it up without having to restart.
  (-> #'app
      ; Makes static assets in $PROJECT_DIR/resources/public/ available.
      (wrap-file "resources")
      ; Content-Type, Content-Length, and Last Modified headers for files in body
      (wrap-file-info)))

(defn start-server
  "used for starting the server in development mode from REPL"
  [& [port]]
  (let [port (if port (Integer/parseInt port) 8080)]
    (reset! server
            (serve (get-handler)
                   {:port port
                    :init init
                    :auto-reload? true
                    :destroy destroy
                    :join? false}))
    (println (str "You can view the site at http://some-ip:" port))))

(defn stop-server []
  (.stop @server)
  (reset! server nil))

I have been unsuccessful with lein repl :connect ...

  1. What can I do to attach a clojure repl to my code on a server?

Thanks for all the help

Upvotes: 3

Views: 1108

Answers (1)

Symfrog
Symfrog

Reputation: 3418

In your project root you should run lein repl, once connected you can enter (start-server) at the repl prompt.

Your server and a browser tab will be launched and the repl prompt that you invoked (start-server) from can be used to interact with the running application.

In order to prevent a browser tab from launching you would need to add :open-browser? false to the options map passed to serve in repl.clj:

(serve (get-handler)
       {:port port
        :init init
        :auto-reload? true
        :destroy destroy
        :open-browser? false
        :join? false})

Upvotes: 4

Related Questions