Skip to content

Commit

Permalink
Add https options if using jetty
Browse files Browse the repository at this point in the history
- new -t option triggers default https setup - port 3443, certificate from ./boot-http-keystore.js
- new -T option allows overriding above options via edn map of :port, :keystore, and :key-password
- Keystore defaults to bundled, unless file or resource path provided
- http connector options ignored if https
- if using httpkit above options are ignored
  • Loading branch information
coltnz committed Jun 13, 2016
1 parent ff0ef24 commit 9d6a28a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ that can serve resources, directories or a typical ring handler.

[](dependency)
```clojure
[pandeiro/boot-http "0.7.3"] ;; latest release
[pandeiro/boot-http "0.7.4-SNAPSHOT"] ;; latest release
```
[](/dependency)

Expand Down
1 change: 1 addition & 0 deletions build.boot
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(set-env!
:source-paths #{"src" "test"}
:dev-dependencies '[[peridot "0.4.3"]]
:resource-paths #{"resources"}
:dependencies '[[org.clojure/clojure "1.7.0"]
[adzerk/bootlaces "0.1.12" :scope "test"]
[adzerk/boot-test "1.0.4" :scope "test"]])
Expand Down
Binary file added resources/boot-http-keystore.jks
Binary file not shown.
14 changes: 13 additions & 1 deletion src/pandeiro/boot_http.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

(def default-port 3000)

;keystore can be file path or resource
(def ssl-defaults {:port 3443 :keystore (str (clojure.java.io/resource "boot-http-keystore.jks")) :key-password "p@ssw0rd"})

(def serve-deps
'[[ring/ring-core "1.4.0"]
[ring/ring-devel "1.4.0"]])
Expand Down Expand Up @@ -36,11 +39,18 @@
p port PORT int "The port to listen on. (Default: 3000)"
k httpkit bool "Use Http-kit server instead of Jetty"
s silent bool "Silent-mode (don't output anything)"
t ssl bool "Serve via Jetty SSL connector on localhost on default port 3443 using cert from ./boot-http-keystore.jks"
T ssl-props SSL edn "Override default SSL properties e.g. \"{:port 3443, :keystore \"boot-http-keystore.jks\", :key-password \"p@ssw0rd\"}\""
R reload bool "Reload modified namespaces on each request."
n nrepl REPL edn "nREPL server parameters e.g. \"{:port 3001, :bind \"0.0.0.0\"}\""
N not-found SYM sym "a ring handler for requested resources that aren't in your directory. Useful for pushState."]

(let [port (or port default-port)
ssl-props (when (or ssl ssl-props)
(if (and ssl-props (not (map? ssl-props)))
(throw (IllegalArgumentException.
(str "Expected map for ssl-props got \"" ssl-props "\"")))
(merge ssl-defaults (or ssl-props {}))))
server-dep (if httpkit httpkit-dep jetty-dep)
deps (cond-> serve-deps
true (conj server-dep)
Expand All @@ -57,15 +67,17 @@
(def server
(http/server
{:dir ~dir, :port ~port, :handler '~handler,
:ssl-props ~ssl-props,
:reload '~reload, :env-dirs ~(vec (:directories pod/env)), :httpkit ~httpkit,
:not-found '~not-found,
:resource-root ~resource-root}))
(def nrepl-server
(when ~nrepl
(http/nrepl-server {:nrepl ~nrepl})))
(when-not ~silent
(boot/info "Started %s on http://localhost:%d\n"
(boot/info "Started %s on %s://localhost:%d\n"
(:human-name server)
(if ~ssl-props "https" "http")
(:local-port server)))))]
(when (and silent (not httpkit))
(silence-jetty!))
Expand Down
11 changes: 9 additions & 2 deletions src/pandeiro/boot_http/impl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,19 @@
:local-port (-> server .getConnectors first .getLocalPort)
:stop-server #(.stop server)}))

(defn server [{:keys [port httpkit] :as opts}]
(defn server [{:keys [port httpkit ssl-props] :as opts}]
((if httpkit start-httpkit start-jetty)
(-> (ring-handler opts)
wrap-content-type
wrap-not-modified)
{:port port :join? false}))
(cond-> {:port port
:join? false}
(seq ssl-props)
(merge {:http? false
:ssl? (boolean (seq ssl-props))
:ssl-port (:port ssl-props)
:keystore (:keystore ssl-props)
:key-password (:key-password ssl-props)}))))

;;
;; nREPL
Expand Down

0 comments on commit 9d6a28a

Please sign in to comment.