From c28c398a14cb4aa3a5a1418e001f8034e0c5fee8 Mon Sep 17 00:00:00 2001 From: Travis Shears Date: Sat, 7 Jun 2025 16:05:34 +0200 Subject: [PATCH] get docker uberjar working --- Dockerfile | 17 ++++++------- bruno/CodeSnippets/bruno.json | 16 +++++++++++- bruno/CodeSnippets/environments/homelab.bru | 3 +++ bruno/CodeSnippets/get_snippets.bru | 6 ++--- build.clj | 25 +++++++++++++++++++ deps.edn | 9 ++++++- src/snippets/api.clj | 9 ++++++- src/snippets/config.clj | 27 ++++++++++++--------- src/snippets/main.clj | 6 +++++ 9 files changed, 91 insertions(+), 27 deletions(-) create mode 100644 bruno/CodeSnippets/environments/homelab.bru create mode 100644 build.clj create mode 100644 src/snippets/main.clj diff --git a/Dockerfile b/Dockerfile index 53d51fc..3b4e7e3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,21 +13,17 @@ # it uses alpine for a small image FROM clojure:temurin-21-tools-deps-alpine AS jre-build -ENV TAILWIND_VERSION=v3.2.4 - # Install the missing packages and applications in a single layer -RUN apk add curl rlwrap && curl -L -o /usr/local/bin/tailwindcss \ - https://github.com/tailwindlabs/tailwindcss/releases/download/$TAILWIND_VERSION/tailwindcss-linux-x64 \ - && chmod +x /usr/local/bin/tailwindcss +RUN apk add curl rlwrap WORKDIR /app +COPY deps.edn ./ +RUN clj -P +COPY build.clj config.edn ./ COPY src ./src -COPY dev ./dev -COPY resources ./resources -COPY deps.edn . # construct the application jar -RUN clj -M:dev uberjar && cp target/jar/app.jar . && rm -r target +RUN clj -T:build uber && cp target/snippets-standalone.jar ./app.jar && rm -r target # This stage (see multi-stage builds) is a bare Java container # copy over the uberjar from the builder image and run the application @@ -36,11 +32,12 @@ WORKDIR /app # Take the uberjar from the base image and put it in the final image COPY --from=jre-build /app/app.jar /app/app.jar +COPY --from=jre-build /app/config.edn /app/ EXPOSE 8080 # By default, run in PROD profile -ENV BIFF_PROFILE=prod +# ENV BIFF_PROFILE=prod ENV HOST=0.0.0.0 ENV PORT=8080 CMD ["/opt/java/openjdk/bin/java", "-XX:-OmitStackTraceInFastThrow", "-XX:+CrashOnOutOfMemoryError", "-jar", "app.jar"] diff --git a/bruno/CodeSnippets/bruno.json b/bruno/CodeSnippets/bruno.json index 6c7e799..3858af6 100644 --- a/bruno/CodeSnippets/bruno.json +++ b/bruno/CodeSnippets/bruno.json @@ -5,5 +5,19 @@ "ignore": [ "node_modules", ".git" - ] + ], + "size": 0.0006971359252929688, + "filesCount": 5, + "proxy": { + "bypassProxy": "", + "enabled": false, + "auth": { + "enabled": false, + "username": "", + "password": "" + }, + "port": null, + "hostname": "", + "protocol": "http" + } } \ No newline at end of file diff --git a/bruno/CodeSnippets/environments/homelab.bru b/bruno/CodeSnippets/environments/homelab.bru new file mode 100644 index 0000000..3f795e0 --- /dev/null +++ b/bruno/CodeSnippets/environments/homelab.bru @@ -0,0 +1,3 @@ +vars:secret [ + host +] diff --git a/bruno/CodeSnippets/get_snippets.bru b/bruno/CodeSnippets/get_snippets.bru index 60b3da2..6ee7097 100644 --- a/bruno/CodeSnippets/get_snippets.bru +++ b/bruno/CodeSnippets/get_snippets.bru @@ -5,14 +5,14 @@ meta { } get { - url: {{host}}/api/snippet?limit=10&skip=10 + url: 192.168.1.157:29406/api/snippet?limit=4&skip=0 body: none auth: none } params:query { - limit: 10 - skip: 10 + limit: 4 + skip: 0 } body:json { diff --git a/build.clj b/build.clj new file mode 100644 index 0000000..e425d86 --- /dev/null +++ b/build.clj @@ -0,0 +1,25 @@ +(ns build + (:require [clojure.tools.build.api :as b])) + +(def lib 'snippets) + +(def class-dir "target/classes") +(def uber-file (format "target/%s-standalone.jar" (name lib))) + +;; delay to defer side effects (artifact downloads) +(def basis (delay (b/create-basis {:project "deps.edn"}))) + +(defn clean [_] + (b/delete {:path "target"})) + +(defn uber [_] + (clean nil) + (b/copy-dir {:src-dirs ["src" "resources"] + :target-dir class-dir}) + (b/compile-clj {:basis @basis + :ns-compile '[snippets.main] + :class-dir class-dir}) + (b/uber {:class-dir class-dir + :uber-file uber-file + :basis @basis + :main 'snippets.main})) diff --git a/deps.edn b/deps.edn index 14cbeac..a191441 100644 --- a/deps.edn +++ b/deps.edn @@ -10,6 +10,9 @@ org.postgresql/postgresql {:mvn/version "42.7.6"} frontmatter/frontmatter {:mvn/version "0.0.1"} + ;; environment variables + environ/environ {:mvn/version "1.2.0"} + ;; schema validation metosin/malli {:mvn/version "0.18.0"} @@ -22,7 +25,11 @@ ;; convenient package of "default" middleware: ;; ring/ring-defaults {:mvn/version "0.5.0"} - org.clojure/clojure {:mvn/version "1.12.1"}}} + org.clojure/clojure {:mvn/version "1.12.1"}} + :aliases + {;; Run with clj -T:build function-in-build + :build {:deps {io.github.clojure/tools.build {:git/tag "v0.10.9" :git/sha "e405aac"}} + :ns-default build}}} ;; :aliases ;; {:dev {:extra-deps {com.biffweb/tasks {:git/url "https://github.com/jacobobryant/biff", :git/sha "1570ccc", :git/tag "v1.8.29", :deps/root "libs/tasks"}} diff --git a/src/snippets/api.clj b/src/snippets/api.clj index c128804..6275199 100644 --- a/src/snippets/api.clj +++ b/src/snippets/api.clj @@ -3,6 +3,7 @@ [ring.adapter.jetty :as jetty] [clojure.pprint :as pprint] [snippets.db :as db] + [snippets.config :as config] [muuntaja.middleware :as mm] [ring.middleware.params] [reitit.ring :as rr])) @@ -43,9 +44,15 @@ ;; (def app ;; ;; use #' prefix for REPL-friendly code -- see note below ;; (wrap-defaults #'app-routes site-defaults)) +;; +(defn run-server [] + (let [jetty-config (:jetty (config/get-config)) + port (:port jetty-config) + host (:host jetty-config)] + (jetty/run-jetty #'app {:port port :host host}))) (defn -main [] - (jetty/run-jetty #'app {:port 3000})) + (run-server)) (comment ;; evaluate this def form to start the webapp via the REPL: diff --git a/src/snippets/config.clj b/src/snippets/config.clj index 080db58..dd3d315 100644 --- a/src/snippets/config.clj +++ b/src/snippets/config.clj @@ -1,13 +1,18 @@ -(ns snippets.config) +(ns snippets.config + (:require [environ.core :refer [env]])) -;; (require '[environ.core :refer [env]]) -(defn get-config [] (read-string (slurp "config.edn"))) +(defn- static-config [] (read-string (slurp "config.edn"))) -;; (def config -;; (merge -;; (read-string (slurp "config.edn")) - ;; {:db {:host (env :db-host) - ;; :port (Integer. (env :db-port)) - ;; :user (env :db-user) - ;; :password (env :db-password) - ;; :dbname (env :db-name)}})) +(defn assoc-in-not-nil [config keys value] + (if (nil? value) + config + (assoc-in config keys value))) + +(defn safe-parse-int [value] + (if (nil? value) nil (Integer/parseInt value))) + +(defn get-config [] + (-> + (static-config) + (assoc-in-not-nil [:jetty :host] (env :host)) + (assoc-in-not-nil [:jetty :port] (safe-parse-int (env :port))))) diff --git a/src/snippets/main.clj b/src/snippets/main.clj new file mode 100644 index 0000000..d6fef1e --- /dev/null +++ b/src/snippets/main.clj @@ -0,0 +1,6 @@ +(ns snippets.main + (:require [snippets.api :as api]) + (:gen-class)) + +(defn -main [] + (api/run-server))