From e90e323c1738df0212bbb7cf262a28aa94e52338 Mon Sep 17 00:00:00 2001 From: Luciano Laratelli Date: Fri, 11 Apr 2025 12:00:22 -0400 Subject: [PATCH] maybe that'll resolve our database woes --- deps.edn | 4 +- dev/user.clj | 1 + src/com/score_the_pigs.clj | 35 +++++-- src/com/score_the_pigs/config.clj | 98 +++++++++++++++++++ .../process/0.6.23/_remote.repositories | 4 + .../process/0.6.23/process-0.6.23.jar.sha1 | 1 + .../process/0.6.23/process-0.6.23.pom | 82 ++++++++++++++++ .../process/0.6.23/process-0.6.23.pom.sha1 | 1 + 8 files changed, 219 insertions(+), 7 deletions(-) create mode 100644 src/com/score_the_pigs/config.clj create mode 100644 vendor/babashka/process/0.6.23/_remote.repositories create mode 100644 vendor/babashka/process/0.6.23/process-0.6.23.jar.sha1 create mode 100644 vendor/babashka/process/0.6.23/process-0.6.23.pom create mode 100644 vendor/babashka/process/0.6.23/process-0.6.23.pom.sha1 diff --git a/deps.edn b/deps.edn index 849bd11..b4d4978 100644 --- a/deps.edn +++ b/deps.edn @@ -33,7 +33,9 @@ org.xerial/sqlite-jdbc {:mvn/version "3.45.2.0"} com.github.seancorfield/honeysql {:mvn/version "2.6.1126"} - migratus/migratus {:mvn/version "1.5.6"}} + migratus/migratus {:mvn/version "1.5.6"} + + babashka/process {:mvn/version "0.6.23"}} :aliases {:dev {:extra-deps {com.biffweb/tasks {:git/url "https://github.com/jacobobryant/biff", :git/sha "ada149e", :git/tag "v1.8.2", :deps/root "libs/tasks"} diff --git a/dev/user.clj b/dev/user.clj index 0920a1b..5b8eb58 100644 --- a/dev/user.clj +++ b/dev/user.clj @@ -4,6 +4,7 @@ [clojure.tools.logging :as log] [clojure.tools.namespace.repl :as tn-repl] [migration :as migration] + [clojure.string :as str] [com.score-the-pigs :as main :refer [system]] [com.score-the-pigs.util.db :as db] [honey.sql :as sql] diff --git a/src/com/score_the_pigs.clj b/src/com/score_the_pigs.clj index cfe25ea..16af238 100644 --- a/src/com/score_the_pigs.clj +++ b/src/com/score_the_pigs.clj @@ -1,15 +1,22 @@ (ns com.score-the-pigs (:require + [babashka.process :as process] + [clojure.string :as str] [clojure.test :as test] [com.biffweb :as biff] [com.score-the-pigs.app :as app] + [com.score-the-pigs.config :as config] [com.score-the-pigs.middleware :as mid] [com.score-the-pigs.ui :as ui] + [migratus.core :as migratus] [next.jdbc :as jdbc] [nrepl.cmdline :as nrepl-cmd] + [ring.adapter.jetty9 :as jetty] [taoensso.telemere.timbre :as log]) (:gen-class)) +(set! *warn-on-reflection* true) + (def modules [app/module]) @@ -54,19 +61,35 @@ (defn use-sqlite [ctx] (let [db-url (get ctx :example/db-url) ds (jdbc/get-datasource db-url) - ;; migration-config (ctx->migratus-config ctx) - ] + migration-config (ctx->migratus-config ctx) + db-loc (last (str/split db-url #":"))] - ;; (migratus/init migration-config) - ;; (migratus/migrate migration-config) + (process/shell (str "rm -rf " db-loc "*")) + (process/shell (str "sqlite3 " db-loc " 'PRAGMA journal_mode=WAL;'")) + + (migratus/init migration-config) + (migratus/reset migration-config) (assoc ctx :example/ds ds))) +(defn use-jetty [{:biff/keys [host port handler] + :or {host "localhost" + port 8080} + :as ctx}] + (let [server (jetty/run-jetty (fn [req] + (handler (merge ctx req))) + {:host host + :port port + :join? false + :allow-null-path-info true})] + (log/info "Jetty running on" (str "http://" host ":" port)) + (update ctx :biff/stop conj #(jetty/stop-server server)))) + (def components - [biff/use-aero-config + [config/use-aero-config use-sqlite biff/use-queues biff/use-htmx-refresh - biff/use-jetty + use-jetty biff/use-chime biff/use-beholder]) diff --git a/src/com/score_the_pigs/config.clj b/src/com/score_the_pigs/config.clj new file mode 100644 index 0000000..4840023 --- /dev/null +++ b/src/com/score_the_pigs/config.clj @@ -0,0 +1,98 @@ +(ns com.score-the-pigs.config + (:require + [aero.core :as aero] + [clojure.java.io :as io] + [clojure.string :as str])) + +(set! *warn-on-reflection* true) + +(defn- ns-parts [nspace] + (if (empty? (str nspace)) + [] + (str/split (str nspace) #"\."))) + +(defn- select-ns [m nspace] + (let [parts (ns-parts nspace)] + (->> (keys m) + (filterv (fn [k] + (= parts (take (count parts) (ns-parts (namespace k)))))) + (select-keys m)))) + +(defn- select-ns-as [m ns-from ns-to] + (into {} + (map (fn [[k v]] + (let [new-ns-parts (->> (ns-parts (namespace k)) + (drop (count (ns-parts ns-from))) + (concat (ns-parts ns-to)))] + [(if (empty? new-ns-parts) + (keyword (name k)) + (keyword (str/join "." new-ns-parts) (name k))) + v]))) + (select-ns m ns-from))) + +(defmacro catchall + [& body] + `(try ~@body (catch Exception ~'_ nil))) +;;;; --------------------------------------------------------------------------- + +;; Algorithm adapted from dotenv-java: +;; https://github.com/cdimascio/dotenv-java/blob/master/src/main/java/io/github/cdimascio/dotenv/internal/DotenvParser.java +;; Wouldn't hurt to take a more thorough look at Ruby dotenv's algorithm: +;; https://github.com/bkeepers/dotenv/blob/master/lib/dotenv/parser.rb +(defn parse-env-var [line] + (let [line (str/trim line) + [_ _ k v] (re-matches #"^\s*(export\s+)?([\w.\-]+)\s*=\s*(['][^']*[']|[\"][^\"]*[\"]|[^#]*)?\s*(#.*)?$" + line)] + (when-not (or (str/starts-with? line "#") + (str/starts-with? line "////") + (empty? v)) + (let [v (str/trim v) + v (if (or (re-matches #"^\".*\"$" v) + (re-matches #"^'.*'$" v)) + (subs v 1 (dec (count v))) + v)] + [k v])))) + +(defmethod aero/reader 'biff/env + [{:keys [profile biff.aero/env] :as opts} _ value] + (not-empty (get env (str value)))) + +(defmethod aero/reader 'biff/secret + [{:keys [profile biff.aero/env] :as opts} _ value] + (when-some [value (aero/reader opts 'biff/env value)] + (fn [] value))) + +(defn get-env [] + (reduce into + {} + [(some->> (catchall (slurp "config.env")) + str/split-lines + (keep parse-env-var)) + (System/getenv) + (keep (fn [[k v]] + (when (str/starts-with? k "biff.env.") + [(str/replace k #"^biff.env." "") v])) + (System/getProperties))])) + +(defn use-aero-config [{:biff.config/keys [skip-validation profile] :as ctx}] + (let [env (get-env) + profile (some-> (or profile + (get env "BIFF_PROFILE") + ;; For backwards compatibility + (get env "BIFF_ENV")) + keyword) + ctx (merge ctx (aero/read-config (io/resource "config.edn") {:profile profile :biff.aero/env env})) + secret (fn [k] + (when-let [f (get ctx k)] + (f))) + ctx (assoc ctx :biff/secret secret)] + (when-not (or skip-validation + (and (secret :biff.middleware/cookie-secret) + (secret :biff/jwt-secret))) + (binding [*out* *err*] + (println "Secrets are missing. Make sure you have a config.env file in the current " + "directory, or set config via environment variables.") + (System/exit 1))) + (doseq [[k v] (select-ns-as ctx 'biff.system-properties nil)] + (System/setProperty (name k) v)) + ctx)) diff --git a/vendor/babashka/process/0.6.23/_remote.repositories b/vendor/babashka/process/0.6.23/_remote.repositories new file mode 100644 index 0000000..8ecb57d --- /dev/null +++ b/vendor/babashka/process/0.6.23/_remote.repositories @@ -0,0 +1,4 @@ +#NOTE: This is a Maven Resolver internal implementation file, its format can be changed without prior notice. +#Fri Apr 11 11:54:54 EDT 2025 +process-0.6.23.jar>clojars= +process-0.6.23.pom>clojars= diff --git a/vendor/babashka/process/0.6.23/process-0.6.23.jar.sha1 b/vendor/babashka/process/0.6.23/process-0.6.23.jar.sha1 new file mode 100644 index 0000000..ddf192c --- /dev/null +++ b/vendor/babashka/process/0.6.23/process-0.6.23.jar.sha1 @@ -0,0 +1 @@ +867a3c75d88e401314d796e3736c12b2b5c3c924 \ No newline at end of file diff --git a/vendor/babashka/process/0.6.23/process-0.6.23.pom b/vendor/babashka/process/0.6.23/process-0.6.23.pom new file mode 100644 index 0000000..d34f9c0 --- /dev/null +++ b/vendor/babashka/process/0.6.23/process-0.6.23.pom @@ -0,0 +1,82 @@ + + + 4.0.0 + babashka + process + jar + 0.6.23 + process + Clojure library for shelling out / spawning subprocesses + https://github.com/babashka/process + + + EPL-1.0 + https://www.eclipse.org/legal/epl-1.0/ + + + + https://github.com/babashka/process + scm:git:git://github.com/babashka/process.git + scm:git:ssh://git@github.com/babashka/process.git + 2058c79fb63f80ca71917432eddea73e0c58717c + + + src + test + + + resources + + + + + resources + + + target + target/classes + + + + + central + https://repo1.maven.org/maven2/ + + false + + + true + + + + clojars + https://repo.clojars.org/ + + true + + + true + + + + + + + + + org.clojure + clojure + 1.9.0 + + + babashka + fs + 0.4.18 + + + + + diff --git a/vendor/babashka/process/0.6.23/process-0.6.23.pom.sha1 b/vendor/babashka/process/0.6.23/process-0.6.23.pom.sha1 new file mode 100644 index 0000000..28a9ad3 --- /dev/null +++ b/vendor/babashka/process/0.6.23/process-0.6.23.pom.sha1 @@ -0,0 +1 @@ +7844e991d609b6933a7b4d208c0d1b0da46a8940 \ No newline at end of file