Skip to content

Commit

Permalink
New task: socket-repl
Browse files Browse the repository at this point in the history
Exposes the Socket Server functionality available in Clojure 1.8 or
above as a task. By default, the handler function is
clojure.core.server/repl, but a different handler can be specified.
  • Loading branch information
pesterhazy committed Feb 6, 2017
1 parent c07b087 commit d5bf0db
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
22 changes: 22 additions & 0 deletions boot/core/src/boot/task/built_in.clj
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,28 @@
(core/with-post-wrap [_]
(when (or client (not server)) @repl-cli)))))

(core/deftask socket-server
"Start a socket server.
The default behavior is to serve a simple Clojure REPL. To serve a different
handler function, specify a symbol using `--accept'.
If no bind address is specified, the socket server will listen on 127.0.0.1.
If no port is specified, an open port will be chosen automatically. The port
number is written to .socket-port in the current directory.
The REPL can be accessed with the command
$ nc localhost $(cat .server-port)"

[b bind ADDR str "The address server listens on."
p port PORT int "The port to listen to."
a accept ACCEPT sym "Namespaced symbol of the accept function to invoke."]
(let [repl-soc (delay (repl/launch-socket-server *opts*))]
(core/with-pass-thru [fs]
@repl-soc)))

(core/deftask pom
"Create project pom.xml file.
Expand Down
18 changes: 18 additions & 0 deletions boot/pod/src/boot/repl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,21 @@
[{:keys [bind port init-ns middleware handler] :as options}]
(let [opts (->> options setup-nrepl-env!)]
(@start-server opts)))

(defn launch-socket-server
"See #boot.task.built-in/socket-server for explanation of options."
[{:keys [bind port accept]}]
(let [opts {:host (or bind "127.0.0.1")
:port (or port 0)
:name (gensym "socket-server")
:accept (or accept 'clojure.core.server/repl)}]
(try
(require 'clojure.core.server)
(catch java.io.FileNotFoundException e
(throw (ex-info "Socket server requires Clojure version 1.8.0 or above"
{:version (clojure-version)}
e))))
(let [effective-port (-> ((resolve 'clojure.core.server/start-server) opts)
(.getLocalPort))]
(doto (io/file ".socket-port") .deleteOnExit (spit effective-port))
(util/info "Socket server started on port %s on host %s.\n" effective-port (:host opts)))))

0 comments on commit d5bf0db

Please sign in to comment.