diff --git a/README.md b/README.md index 68695a8..fb38388 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,11 @@ Download the main dm+d distribution; you can optionally also download the 'extra ATC BNF codes. Once downloaded, unzip one or both into a common directory and run: ```shell -clj -M:install --db dmd-2021-07-01.db ~/Downloads/dmd-2021-07-01 +clj -M:run install --db dmd-2021-07-01.db ~/Downloads/dmd-2021-07-01 +``` +or +```shell +java -jar dmd.jar --db dmd-2021-07-01.db ~/Downloads/dmd-2021-07-01 ``` This will look for files in the directory specified and create a new file-based @@ -109,13 +113,16 @@ or running: ```shell clj -M:run list --api-key /var/local/trud/api-key.txt -# or java -jar dmd.jar list --api-key /var/local/trud/api-key.txt +``` +or +```shell +java -jar dmd.jar list --api-key /var/local/trud/api-key.txt ``` Result: ```shell -➜ dmd git:(main) ✗ clj -M:run --api-key /var/local/trud/api-key.txt list +➜ dmd git:(main) ✗ clj -M:run list --api-key /var/local/trud/api-key.txt | :id | :releaseDate | :name | |--------------------------------------+--------------+----------------| @@ -129,7 +136,7 @@ To download a specific edition, you can download manually, unzip and then directly import from that distribution. ```shell -clj -M:run --db dmd.db install /tmp/downloads/trud +clj -M:run install --db dmd.db /tmp/downloads/trud ``` Or you can specify the version and let `dmd` download for you. @@ -139,17 +146,16 @@ Specify which release you want by date according to ISO-8601 standard: clj -M:run --api-key /var/local/trud/api-key.txt download 2021-04-05 ``` - #### 4. Run a HTTP server Once you have downloaded a distribution, you can use it to run a very fast HTTP server. ```shell -clj -M:serve dmd-2021-04-12.db 8080 +clj -M:run serve --db dmd-2021-04-12.db --port 8080 ``` or ```shell -java -jar dmd-server.jar dmd-2021-04-12.db 8080 +java -jar dmd.jar --db dmd-2021-04-12.db --port 8080 ``` As it is very likely that the complete dm+d dataset will fit into the memory @@ -526,16 +532,9 @@ clj -M:lint/kondo clj -X:jar ``` -#### Build and run command-line utility uberjar +#### Build and run an uberjar ```shell clj -X:uberjar java -jar target/dmd.jar --help ``` - -#### Build and run server uberjar - -```shell -clj -X:server-uberjar -java -jar target/dmd-server.jar dmd-2021-04-12.db 8080 -``` \ No newline at end of file diff --git a/src/com/eldrix/dmd/cli.clj b/cmd/com/eldrix/dmd/cli.clj similarity index 77% rename from src/com/eldrix/dmd/cli.clj rename to cmd/com/eldrix/dmd/cli.clj index fac8a11..5aa4de0 100644 --- a/src/com/eldrix/dmd/cli.clj +++ b/cmd/com/eldrix/dmd/cli.clj @@ -1,13 +1,16 @@ (ns com.eldrix.dmd.cli (:gen-class) - (:require [clojure.tools.cli :as cli] + (:require [clojure.string :as str] + [clojure.tools.cli :as cli] [com.eldrix.dmd.core :as dmd] - [clojure.string :as str])) + [com.eldrix.dmd.serve :as serve] + [clojure.tools.logging.readable :as log])) (def cli-options [[nil "--api-key PATH" "Path to file containing TRUD API key"] [nil "--cache-dir PATH" "Path to cache directory for TRUD downloads"] [nil "--db FILENAME" "Name of database; usually optional. A default will be chosen if omitted"] + ["-p" "--port PORT" "Port to use"] ["-h" "--help"]]) (defn usage [options-summary] @@ -18,6 +21,7 @@ " - list : list available distributions from TRUD" " - download : download and install a specific dm+d release (e.g. 2021-04-05)" " - install : install from a manually downloaded/unzipped distribution" + " - serve : run a HTTP server" "Options:" options-summary] (str/join \newline))) @@ -52,6 +56,15 @@ :else (dmd/install-from-dirs db params))) +(defn run-server [{:keys [db port] :or {port "8080"}}] + (if db + (let [st (dmd/open-store db)] + (log/info "starting server using " db " on port" port) + (try (serve/start-server st (Integer/parseInt port)) + (catch NumberFormatException e + (log/error "invalid port:" port)))) + (log/error "You must specify a database using --db"))) + (defn -main [& args] (let [{:keys [options arguments summary errors]} (cli/parse-opts args cli-options)] (cond @@ -72,7 +85,8 @@ (= "latest" command) (download options nil) (= "list" command) (list-available options) (= "download" command) (download options params) - (= "install" command) (install options params)))))) + (= "install" command) (install options params) + (= "serve" command) (run-server options)))))) (comment ) diff --git a/serve/com/eldrix/dmd/serve.clj b/cmd/com/eldrix/dmd/serve.clj similarity index 93% rename from serve/com/eldrix/dmd/serve.clj rename to cmd/com/eldrix/dmd/serve.clj index 23205f1..24a05e0 100644 --- a/serve/com/eldrix/dmd/serve.clj +++ b/cmd/com/eldrix/dmd/serve.clj @@ -179,15 +179,6 @@ (stop-dev) (start-dev svc port)) -(defn -main [& args] - (if-not (= 2 (count args)) - (println "Usage: clj -M:serve \n or java -jar dmd-server.jar ") - (let [[filename port] args - store (com.eldrix.dmd.store2/open-store filename) - port' (Integer/parseInt port)] - (log/info "starting NHS dm+d server" {:port port' :filename filename}) - (start-server store port')))) - (comment (do (require '[io.pedestal.test]) diff --git a/serve/logback.xml b/cmd/logback.xml similarity index 100% rename from serve/logback.xml rename to cmd/logback.xml diff --git a/deps.edn b/deps.edn index a3d7f6e..fd506e4 100644 --- a/deps.edn +++ b/deps.edn @@ -7,7 +7,6 @@ org.clojure/data.json {:mvn/version "2.4.0"} org.clojure/data.xml {:mvn/version "0.2.0-alpha6"} org.clojure/data.zip {:mvn/version "1.0.0"} - org.clojure/tools.cli {:mvn/version "1.0.206"} org.clojure/tools.logging {:mvn/version "1.1.0"} com.eldrix/trud {:git/url "https://github.com/wardle/trud.git" @@ -17,20 +16,15 @@ datalevin/datalevin {:mvn/version "0.4.40"}} :aliases {:dev - {:extra-paths ["serve" "test/src" "test/resources"]} + {:extra-paths ["cmd" "test/src" "test/resources"]} :run - {:main-opts ["-m" "com.eldrix.dmd.cli"] - :jvm-opts ["--add-opens" "java.base/java.nio=ALL-UNNAMED" - "--add-opens" "java.base/sun.nio.ch=ALL-UNNAMED" - "--illegal-access=permit"]} - - :serve - {:extra-paths ["serve"] - :extra-deps {io.pedestal/pedestal.service {:mvn/version "0.5.9"} + {:extra-paths ["cmd"] + :extra-deps {org.clojure/tools.cli {:mvn/version "1.0.206"} + io.pedestal/pedestal.service {:mvn/version "0.5.9"} io.pedestal/pedestal.jetty {:mvn/version "0.5.9"} ch.qos.logback/logback-classic {:mvn/version "1.2.5"}} - :main-opts ["-m" "com.eldrix.dmd.serve"] + :main-opts ["-m" "com.eldrix.dmd.cli"] :jvm-opts ["--add-opens" "java.base/java.nio=ALL-UNNAMED" "--add-opens" "java.base/sun.nio.ch=ALL-UNNAMED" "--illegal-access=permit"]} @@ -83,22 +77,14 @@ :extra-deps {jonase/eastwood {:mvn/version "RELEASE"}}} :jar - {:replace-deps {com.github.seancorfield/depstar {:mvn/version "2.1.278"}} + {:replace-deps {com.github.seancorfield/depstar {:mvn/version "2.1.297"}} :exec-fn hf.depstar/jar :exec-args {:jar "target/dmd-lib.jar"}} :uberjar - {:replace-deps {com.github.seancorfield/depstar {:mvn/version "2.1.278"}} + {:replace-deps {com.github.seancorfield/depstar {:mvn/version "2.1.297"}} :exec-fn hf.depstar/uberjar :exec-args {:jar "target/dmd.jar" :aot true - :main-class "com.eldrix.dmd.cli"}} - - :server-uberjar - {:replace-deps {com.github.seancorfield/depstar {:mvn/version "2.1.278"}} - :exec-fn hf.depstar/uberjar - :exec-args {:jar "target/dmd-server.jar" - :aliases [:serve] - :aot true - :main-class com.eldrix.dmd.serve}} - }} + :aliases [:run] + :main-class "com.eldrix.dmd.cli"}}}}