-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
52 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {})) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters