Skip to content

Commit

Permalink
Update src
Browse files Browse the repository at this point in the history
  • Loading branch information
quan-nh committed Feb 22, 2024
1 parent b76ecdf commit ee0c870
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 89 deletions.
2 changes: 1 addition & 1 deletion doc/clj-web-from-the-ground-up-6.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"])})
Expand Down
4 changes: 2 additions & 2 deletions doc/clj-web-from-the-ground-up-8.md
Original file line number Diff line number Diff line change
@@ -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`
Expand Down
20 changes: 11 additions & 9 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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})
10 changes: 1 addition & 9 deletions resources/config.edn
Original file line number Diff line number Diff line change
@@ -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"}}
37 changes: 22 additions & 15 deletions src/clj_web/app.clj
Original file line number Diff line number Diff line change
@@ -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}))
2 changes: 1 addition & 1 deletion src/clj_web/config.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
(assoc this :config nil)))

(defn new-config []
(map->Config {:config-file "config.edn"}))
(map->Config {:config-file "config.edn"}))
22 changes: 7 additions & 15 deletions src/clj_web/db.clj
Original file line number Diff line number Diff line change
@@ -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 {}))
5 changes: 0 additions & 5 deletions src/clj_web/db/foo.clj

This file was deleted.

15 changes: 0 additions & 15 deletions src/clj_web/handler/foo.clj

This file was deleted.

10 changes: 0 additions & 10 deletions src/clj_web/handler/spec.clj

This file was deleted.

14 changes: 7 additions & 7 deletions src/clj_web/main.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down

0 comments on commit ee0c870

Please sign in to comment.