Skip to content

Latest commit

 

History

History
103 lines (70 loc) · 2.5 KB

README.md

File metadata and controls

103 lines (70 loc) · 2.5 KB

http-server

Clojars Project

Serve static assets.

Works in clojure JVM and babashka.

Clojure

To your deps.edn add an alias:

:serve {:deps {org.babashka/http-server {:mvn/version "0.1.13"}}
        :main-opts ["-m" "babashka.http-server"]
        :exec-fn babashka.http-server/exec}

Then run from the command line:

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

or:

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

Or install as a tool:

$ clj -Ttools install io.github.babashka/http-server '{:git/tag "v0.1.13"}' :as serve
$ clj -Tserve exec

Babashka

In a script, e.g. /usr/local/bin/http-server:

#!/usr/bin/env bb

(require '[babashka.deps :as deps])
(deps/add-deps
 '{:deps {org.babashka/http-server {:mvn/version "0.1.13"}}})

(require '[babashka.http-server :as http-server])

(apply http-server/-main *command-line-args*)

Then invoke using:

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

In bb.edn tasks:

{:deps {org.babashka/http-server {:mvn/version "0.1.13"}
        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 :headers :edn}}))

  serve {:doc "Serve static assets"
         :requires ([babashka.http-server :as server])
         :task (server/exec (merge {:port 1337
                                    :dir "."}
                                   cli-opts))}

  prn {:task (clojure "-X clojure.core/prn" cli-opts)}

  -dev {:depends [serve prn]}

  dev {:task (run '-dev {:parallel true})}}}
$ bb dev --port 1338
Serving assets at http://localhost:1338
{:port 1338}

Credits

Thanks to Jakub Holy who made this gist which was in turn based on this babashka example. I used several variations of this in my own projects, which eventually became this repo.

License

Copyright © 2022 Michiel Borkent

Distributed under the MIT License. See LICENSE.