Skip to content

Commit

Permalink
Adds extra optional argument headers (#9)
Browse files Browse the repository at this point in the history
* feat: adds headers argument

* docs: adds examples with headers

* refact: using :edn instead of a [:string]

* chore: fix docs
  • Loading branch information
rafaeldelboni authored Nov 1, 2023
1 parent 8febc08 commit 6964d9e
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 20 deletions.
1 change: 1 addition & 0 deletions .clj-kondo/babashka/fs/config.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{:lint-as {babashka.fs/with-temp-dir clojure.core/let}}
3 changes: 3 additions & 0 deletions .clj-kondo/http-kit/http-kit/config.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

{:hooks
{:analyze-call {org.httpkit.server/with-channel httpkit.with-channel/with-channel}}}
16 changes: 16 additions & 0 deletions .clj-kondo/http-kit/http-kit/httpkit/with_channel.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(ns httpkit.with-channel
(:require [clj-kondo.hooks-api :as api]))

(defn with-channel [{node :node}]
(let [[request channel & body] (rest (:children node))]
(when-not (and request channel) (throw (ex-info "No request or channel provided" {})))
(when-not (api/token-node? channel) (throw (ex-info "Missing channel argument" {})))
(let [new-node
(api/list-node
(list*
(api/token-node 'let)
(api/vector-node [channel (api/vector-node [])])
request
body))]

{:node new-node})))
5 changes: 5 additions & 0 deletions .clj-kondo/rewrite-clj/rewrite-clj/config.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{:lint-as
{rewrite-clj.zip/subedit-> clojure.core/->
rewrite-clj.zip/subedit->> clojure.core/->>
rewrite-clj.zip/edit-> clojure.core/->
rewrite-clj.zip/edit->> clojure.core/->>}}
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ To your `deps.edn` add an alias:
Then run from the command line:

``` clojure
clj -M:serve :port 1339 :dir "."
clj -M:serve :port 1339 :dir "." :headers '{"Cross-Origin-Opener-Policy" "same-origin"}'
```

or:

``` clojure
clj -X:serve :port 1339 :dir '"."'
clj -X:serve :port 1339 :dir '"."' :headers '{"Cross-Origin-Opener-Policy" "same-origin"}'
```

Or install as a tool:
Expand Down Expand Up @@ -56,7 +56,7 @@ In a script, e.g. `/usr/local/bin/http-server`:
Then invoke using:

``` clojure
$ http-server --port 8888 --dir resources/public
$ http-server --port 8888 --dir resources/public --headers '{"Cross-Origin-Opener-Policy" "same-origin"}'
```

In `bb.edn` [tasks](https://book.babashka.org/#tasks):
Expand All @@ -66,7 +66,7 @@ In `bb.edn` [tasks](https://book.babashka.org/#tasks):
org.babashka/cli {:mvn/version "0.2.23"}}
:tasks
{:requires ([babashka.cli :as cli])
:init (def cli-opts (cli/parse-opts *command-line-args* {:coerce {:port :int}}))
:init (def cli-opts (cli/parse-opts *command-line-args* {:coerce {:port :int :headers :edn}}))

serve {:doc "Serve static assets"
:requires ([babashka.http-server :as server])
Expand Down
35 changes: 19 additions & 16 deletions src/babashka/http_server.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
(:import [java.net URLDecoder URLEncoder]))

#_(def ^:private
cli-options [["-p" "--port PORT" "Port for HTTP server"
:default 8090 :parse-fn #(Integer/parseInt %)]
["-d" "--dir DIR" "Directory to serve files from"
:default "."]
["-h" "--help" "Print usage info"]])
cli-options [["-p" "--port PORT" "Port for HTTP server"
:default 8090 :parse-fn #(Integer/parseInt %)]
["-d" "--dir DIR" "Directory to serve files from"
:default "."]
["-h" "--help" "Print usage info"]])

;; A simple mime type utility from https://github.com/ring-clojure/ring/blob/master/ring-core/src/ring/util/mime_type.clj
(def ^{:doc "A map of file extensions to mime-types."}
Expand Down Expand Up @@ -149,23 +149,26 @@
html/html
str)}))

(defn- body [path]
{:headers {"Content-Type" (ext-mime-type (fs/file-name path))}
:body (fs/file path)})
(defn- body
([path]
(body path {}))
([path headers]
{:headers (merge {"Content-Type" (ext-mime-type (fs/file-name path))} headers)
:body (fs/file path)}))

(defn file-router [dir]
(defn file-router [dir headers]
(fn [{:keys [uri]}]
(let [f (fs/path dir (str/replace-first (URLDecoder/decode uri) #"^/" ""))
index-file (fs/path f "index.html")]
(cond
(and (fs/directory? f) (fs/readable? index-file))
(body index-file)
(body index-file headers)

(fs/directory? f)
(index dir f)

(fs/readable? f)
(body f)
(body f headers)

:else
{:status 404 :body (str "Not found `" f "` in " dir)}))))
Expand All @@ -174,20 +177,20 @@
"Serves static assets using web server.
Options:
* `:dir` - directory from which to serve assets
* `:port` - port"
* `:port` - port
* `:headers` - map of headers {key value}"
[{:keys [port]
:or {port 8090}
:as opts}]
(let [dir (or (:dir opts) ".")
opts (assoc opts :dir dir)
opts (assoc opts :port port)
opts (assoc opts :dir dir :port port)
dir (fs/path dir)]
(assert (fs/directory? dir) (str "The given dir `" dir "` is not a directory."))
(binding [*out* *err*]
(println (str "Serving assets at http://localhost:" (:port opts))))
(server/run-server (file-router dir) opts)))
(server/run-server (file-router dir (opts :headers)) opts)))

(def ^:private cli-opts {:coerce {:port :long}})
(def ^:private cli-opts {:coerce {:port :long :headers :edn}})

(defn exec
"Exec function, intended for command line usage. Same API as `serve` but
Expand Down

0 comments on commit 6964d9e

Please sign in to comment.