Skip to content

Instantly share code, notes, and snippets.

@danielbraun
Last active August 10, 2019 21:30
Show Gist options
  • Save danielbraun/c3729251f1c8aabd6c51240fe1c6b9d8 to your computer and use it in GitHub Desktop.
Save danielbraun/c3729251f1c8aabd6c51240fe1c6b9d8 to your computer and use it in GitHub Desktop.
(ns ring-server-pg
(:require [ring.adapter.jetty :as jetty]
[clojure.java [jdbc :as jdbc]]
[clj-postgresql.core]
[ring.middleware.defaults :as defaults]))
(def db (System/getenv "DB_URL"))
(def last-request (atom nil))
(defn- insert-request! [req]
(jdbc/insert! db
:ring.requests
(-> req
(select-keys [:server-port
:server-name
:uri
:query-string
:scheme
:request-method
:headers
:body])
(dissoc :body)
(update :request-method name)
(update :scheme name))
{:entities #(clojure.string/replace % "-" "_")}))
(def some-handler
(fn [request]
(reset! last-request request)
(insert-request! request)
{:status 200
:headers {"Content-Type" "text/plain"}
:body "OK"}))
(def handler
(-> some-handler
(defaults/wrap-defaults defaults/site-defaults)))
(def server
(do
(assert db)
(when db
(jetty/run-jetty #'handler {:port 4000}))))
(comment
(-> @last-request
clojure.pprint/pprint))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment