-
Notifications
You must be signed in to change notification settings - Fork 2k
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
1 parent
2915539
commit 8e53a43
Showing
5 changed files
with
68 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
(defproject hello "reitit" | ||
:description "Immutant, Reitit, Jsonista" | ||
:description "Immutant-nio, Reitit, Jsonista & Porsas" | ||
:dependencies [[org.clojure/clojure "1.10.0"] | ||
[ikitommi/immutant-web "3.0.0-alpha1"] | ||
[metosin/jsonista "0.2.2"] | ||
[metosin/reitit "0.2.12"]] | ||
[hikari-cp "2.7.1"] | ||
[org.postgresql/postgresql "42.2.5"] | ||
[metosin/porsas "0.0.1-alpha4"] | ||
[metosin/reitit "0.3.1"]] | ||
:jvm-opts ^:replace ["-Dclojure.compiler.direct-linking=true"] | ||
:main hello.handler | ||
:aot :all) |
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,68 @@ | ||
(ns hello.handler | ||
(:require [immutant.web :as web] | ||
[hikari-cp.core :as hikari] | ||
[reitit.ring :as ring] | ||
[porsas.core :as p] | ||
[jsonista.core :as j]) | ||
(:gen-class)) | ||
(:gen-class) | ||
(:import (java.util.concurrent ThreadLocalRandom) | ||
(io.undertow.server HttpServerExchange))) | ||
|
||
(defn blocking [handler] | ||
(fn [req] | ||
(.startBlocking ^HttpServerExchange (:server-exchange req)) | ||
(handler req))) | ||
|
||
(defn random [] | ||
(unchecked-inc (.nextInt (ThreadLocalRandom/current) 10000))) | ||
|
||
(def query-one (:query-one (p/compile {:row (p/rs->compiled-record)}))) | ||
|
||
(defn random-world [ds] | ||
(with-open [con (p/get-connection ds)] | ||
(query-one con ["SELECT id, randomnumber from WORLD where id=?" (random)]))) | ||
|
||
(defn plain-text-handler [_] | ||
{:status 200 | ||
:headers {"content-type" "text/plain; charset=utf-8"} | ||
:body (.getBytes "Hello, World!")}) | ||
|
||
(defn json-handler [_] | ||
{:status 200 | ||
:headers {"Content-Type" "application/json"} | ||
:body (j/write-value-as-bytes {:message "Hello, World!"})}) | ||
|
||
(def app | ||
(ring/ring-handler | ||
(ring/router ["/json" json-handler]) | ||
(ring/create-default-handler) | ||
{:inject-match? false, :inject-router? false})) | ||
(defn db-handler [ds] | ||
(fn [_] | ||
{:status 200 | ||
:headers {"Content-Type" "application/json"} | ||
:body (j/write-value-as-bytes (random-world ds))})) | ||
|
||
(defn -main [& _] | ||
(web/run | ||
app | ||
{:port 8080 | ||
:host "0.0.0.0" | ||
:dispatch? false | ||
:server {:always-set-keep-alive false}})) | ||
(let [ds (hikari/make-datasource | ||
{:read-only true | ||
:maximum-pool-size 64 | ||
:pool-name "db-pool" | ||
:adapter "postgresql" | ||
:username "benchmarkdbuser" | ||
:password "benchmarkdbpass" | ||
:database-name "hello_world" | ||
:server-name "tfb-database" | ||
:port-number 5432 | ||
:register-mbeans false})] | ||
(web/run | ||
(ring/ring-handler | ||
(ring/router | ||
[["/plaintext" plain-text-handler] | ||
["/json" json-handler] | ||
["/db" (blocking (db-handler ds))]]) | ||
(ring/create-default-handler) | ||
{:inject-match? false, :inject-router? false}) | ||
{:port 8080 | ||
:host "0.0.0.0" | ||
:dispatch? false | ||
:worker-threads 64 | ||
:server {:always-set-keep-alive false}}))) | ||
|
||
(comment | ||
(-main)) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<configuration> | ||
<root level="ERROR"/> | ||
</configuration> |