Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New task: socket-server #549

Merged
merged 1 commit into from
Mar 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 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,29 @@
(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 REPL handled by
clojure.core.server/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)))))