From ee0c870e7460d328421dc7410b8e4294c063cbc0 Mon Sep 17 00:00:00 2001 From: q Date: Thu, 22 Feb 2024 17:01:18 +0700 Subject: [PATCH] Update src --- doc/clj-web-from-the-ground-up-6.md | 2 +- doc/clj-web-from-the-ground-up-8.md | 4 ++-- project.clj | 20 +++++++++------- resources/config.edn | 10 +------- src/clj_web/app.clj | 37 +++++++++++++++++------------ src/clj_web/config.clj | 2 +- src/clj_web/db.clj | 22 ++++++----------- src/clj_web/db/foo.clj | 5 ---- src/clj_web/handler/foo.clj | 15 ------------ src/clj_web/handler/spec.clj | 10 -------- src/clj_web/main.clj | 14 +++++------ 11 files changed, 52 insertions(+), 89 deletions(-) delete mode 100644 src/clj_web/db/foo.clj delete mode 100644 src/clj_web/handler/foo.clj delete mode 100644 src/clj_web/handler/spec.clj diff --git a/doc/clj-web-from-the-ground-up-6.md b/doc/clj-web-from-the-ground-up-6.md index 964fad5..be8846b 100644 --- a/doc/clj-web-from-the-ground-up-6.md +++ b/doc/clj-web-from-the-ground-up-6.md @@ -5,7 +5,7 @@ https://cljdoc.org/d/metosin/reitit/0.6.0/doc/ring/content-negotiation `app.clj` ```clj (:require [muuntaja.core :as m] - [reitit.ring.middleware.muuntaja :as muuntaja]) + [reitit.ring.middleware.muuntaja :as muuntaja]) (defn db-handler [req db] {:status 200, :body (jdbc/execute! db ["SELECT 3*5 AS result"])}) diff --git a/doc/clj-web-from-the-ground-up-8.md b/doc/clj-web-from-the-ground-up-8.md index a2502bb..1d9bf1b 100644 --- a/doc/clj-web-from-the-ground-up-8.md +++ b/doc/clj-web-from-the-ground-up-8.md @@ -1,8 +1,8 @@ logging ``` -[org.clojure/tools.logging "1.1.0"] -[ch.qos.logback/logback-classic "1.2.3"] +[org.clojure/tools.logging "1.3.0"] +[ch.qos.logback/logback-classic "1.5.0"] ``` `resources\logback.xml` diff --git a/project.clj b/project.clj index 4a6861b..4e84d2f 100644 --- a/project.clj +++ b/project.clj @@ -4,24 +4,26 @@ :license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0" :url "https://www.eclipse.org/legal/epl-2.0/"} :dependencies [[org.clojure/clojure "1.11.1"] - [com.stuartsierra/component "1.1.0"] - [reloaded.repl "0.2.4"] - ; web + ; ring [ring/ring-core "1.11.0"] [ring/ring-jetty-adapter "1.11.0"] - [ring-jetty-component "0.3.1"] [metosin/reitit "0.6.0"] - ; config - [aero "1.1.6"] + ; component + [com.stuartsierra/component "1.1.0"] + [ring-jetty-component "0.3.1"] + [reloaded.repl "0.2.4"] ; db [com.github.seancorfield/next.jdbc "1.3.909"] [com.h2database/h2 "2.2.224"] [com.zaxxer/HikariCP "5.1.0"] + ; config + [aero "1.1.6"] ; logging - [org.clojure/tools.logging "0.5.0"] - [ch.qos.logback/logback-classic "1.2.3"]] + [org.clojure/tools.logging "1.3.0"] + [ch.qos.logback/logback-classic "1.5.0"]] :main ^:skip-aot clj-web.core :target-path "target/%s" :profiles {:dev {:source-paths ["dev"]} - :uberjar {:aot :all}} + :uberjar {:aot :all + :jvm-opts ["-Dclojure.compiler.direct-linking=true"]}} :repl-options {:init-ns user}) diff --git a/resources/config.edn b/resources/config.edn index f85fa20..adaece7 100644 --- a/resources/config.edn +++ b/resources/config.edn @@ -1,9 +1 @@ -{:db-spec - {:classname "org.h2.Driver" - :subprotocol "h2:mem" ; the prefix `jdbc:` is added automatically - :subname "demo;DB_CLOSE_DELAY=-1" ; `;DB_CLOSE_DELAY=-1` very important!!! - ; http://www.h2database.com/html/features.html#in_memory_databases - :user "sa" ; default "system admin" user - :password "" ; default password => empty string - } -} +{:db-spec {:dbtype "h2" :dbname "example"}} \ No newline at end of file diff --git a/src/clj_web/app.clj b/src/clj_web/app.clj index 0e33da2..fad1cff 100644 --- a/src/clj_web/app.clj +++ b/src/clj_web/app.clj @@ -1,26 +1,33 @@ (ns clj-web.app - (:require [compojure.core :refer :all] - [compojure.route :as route] + (:require [clojure.tools.logging :as log] [com.stuartsierra.component :as component] - [ring.middleware.defaults :refer :all] - [muuntaja.middleware :as mw] - [clj-web.handler.foo :as foo-handler])) + [next.jdbc :as jdbc] + [reitit.ring :as ring] + [muuntaja.core :as m] + [reitit.ring.middleware.muuntaja :as muuntaja])) -(defn app-routes [db] - (routes - (GET "/" [] "Hello World!") - (GET "/db" req (foo-handler/bar req db)) - (POST "/data" {:keys [body-params]} (foo-handler/data body-params)) - (route/not-found "404"))) +(defn ok-handler [_] + {:status 200, :body "ok"}) + +(defn db-handler [req db] + (log/debug req) + {:status 200, :body (jdbc/execute! db ["SELECT 3*5 AS result"])}) + +(defn app-handler [db] + (ring/ring-handler + (ring/router + [["/" ok-handler] + ["/db" #(db-handler % db)]] + {:data {:muuntaja m/instance + :middleware [muuntaja/format-middleware]}}) + (ring/create-default-handler))) (defrecord App [handler db] component/Lifecycle (start [this] - (assoc this :handler (-> (handler db) - (mw/wrap-format) - (wrap-defaults api-defaults)))) + (assoc this :handler (handler db))) (stop [this] (assoc this :handler nil))) (defn new-app [] - (map->App {:handler app-routes})) + (map->App {:handler app-handler})) diff --git a/src/clj_web/config.clj b/src/clj_web/config.clj index 96f2449..12f51d5 100644 --- a/src/clj_web/config.clj +++ b/src/clj_web/config.clj @@ -10,4 +10,4 @@ (assoc this :config nil))) (defn new-config [] - (map->Config {:config-file "config.edn"})) + (map->Config {:config-file "config.edn"})) \ No newline at end of file diff --git a/src/clj_web/db.clj b/src/clj_web/db.clj index 23feadf..391c26e 100644 --- a/src/clj_web/db.clj +++ b/src/clj_web/db.clj @@ -1,24 +1,16 @@ (ns clj-web.db - (:require [com.stuartsierra.component :as component]) - (:import (com.mchange.v2.c3p0 ComboPooledDataSource))) + (:require [next.jdbc.connection :as connection]) + (:import (com.stuartsierra.component Lifecycle) + (com.zaxxer.hikari HikariDataSource))) (defrecord Database [datasource config] - component/Lifecycle + Lifecycle (start [this] - (let [db-spec (get-in config [:config :db-spec]) - cpds (doto (ComboPooledDataSource.) - (.setDriverClass (:classname db-spec)) - (.setJdbcUrl (str "jdbc:" (:subprotocol db-spec) ":" (:subname db-spec))) - (.setUser (:user db-spec)) - (.setPassword (:password db-spec)) - ;; expire excess connections after 30 minutes of inactivity: - (.setMaxIdleTimeExcessConnections (* 30 60)) - ;; expire connections after 3 hours of inactivity: - (.setMaxIdleTime (* 3 60 60)))] - (assoc this :datasource cpds))) + (let [db-spec (get-in config [:config :db-spec])] + (assoc this :datasource (connection/->pool HikariDataSource db-spec)))) (stop [this] (.close datasource) (assoc this :datasource nil))) (defn new-database [] - (map->Database {})) + (map->Database {})) \ No newline at end of file diff --git a/src/clj_web/db/foo.clj b/src/clj_web/db/foo.clj deleted file mode 100644 index e883399..0000000 --- a/src/clj_web/db/foo.clj +++ /dev/null @@ -1,5 +0,0 @@ -(ns clj-web.db.foo - (:require [clojure.java.jdbc :as jdbc])) - -(defn bar [db] - (first (jdbc/query db ["SELECT 3*5 AS result"]))) diff --git a/src/clj_web/handler/foo.clj b/src/clj_web/handler/foo.clj deleted file mode 100644 index 427ab61..0000000 --- a/src/clj_web/handler/foo.clj +++ /dev/null @@ -1,15 +0,0 @@ -(ns clj-web.handler.foo - (:require [clojure.tools.logging :as log] - [clj-web.db.foo :as foo-db] - [ring.util.http-response :refer :all] - [clojure.spec.alpha :as s] - [clj-web.handler.spec :as hs])) - -(defn bar [req db] - (log/debug req) - (ok (foo-db/bar db))) - -(defn data [body] - (if (s/valid? ::hs/data-request body) - (ok (s/conform ::hs/data-request body)) - (bad-request (s/explain-str ::hs/data-request body)))) diff --git a/src/clj_web/handler/spec.clj b/src/clj_web/handler/spec.clj deleted file mode 100644 index 3265ca7..0000000 --- a/src/clj_web/handler/spec.clj +++ /dev/null @@ -1,10 +0,0 @@ -(ns clj-web.handler.spec - (:require [clojure.spec.alpha :as s])) - -(s/def ::first-name string?) -(s/def ::last-name string?) -(s/def ::phone string?) - -(s/def ::data-request - (s/keys :req-un [::first-name ::last-name] - :opt-un [::phone])) \ No newline at end of file diff --git a/src/clj_web/main.clj b/src/clj_web/main.clj index f7f5d82..056fc11 100644 --- a/src/clj_web/main.clj +++ b/src/clj_web/main.clj @@ -9,14 +9,14 @@ (defn app-system [] (-> (component/system-map - :config (new-config) - :db (new-database) - :app (new-app) - :http (jetty-server {:port 3000})) + :config (new-config) + :db (new-database) + :app (new-app) + :http (jetty-server {:port 3000})) (component/system-using - {:http [:app] - :app [:db] - :db [:config]}))) + {:http [:app] + :app [:db] + :db [:config]}))) (defn -main "I don't do a whole lot ... yet."