-
-
Save kryft/9303dfdfa730cac346ea to your computer and use it in GitHub Desktop.
lein jar hanging
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
(ns abhimata_backend.core | |
(:import org.postgresql.util.PGobject) | |
(:require [cemerick.friend :as friend :as friend] | |
(cemerick.friend [workflows :as workflows] | |
[credentials :as creds]) | |
[compojure.core :as compojure | |
:refer (GET POST ANY defroutes)] | |
[clojure.java.jdbc :as jdbc] | |
[ring.adapter.jetty :as jetty] | |
[ring.middleware.json :as ringjson] | |
[clojure.data.json :as json] | |
[ring.util.response :as resp] | |
(compojure [handler :as handler] | |
[route :as route])) | |
(:gen-class)) | |
;Extend jdbc to support PostgreSQL's json data type, as per | |
;http://hiim.tv/clojure/2014/05/15/clojure-postgres-json/ | |
(defn value-to-json-pgobject [value] | |
(doto (PGobject.) | |
(.setType "json") | |
(.setValue (json/write-str value)))) | |
(defn extend-jdbc-psql-protocols [] | |
(do | |
(extend-protocol jdbc/ISQLValue | |
clojure.lang.IPersistentMap | |
(sql-value [value] (value-to-json-pgobject value)) | |
clojure.lang.IPersistentVector | |
(sql-value [value] (value-to-json-pgobject value))) | |
(extend-protocol jdbc/IResultSetReadColumn | |
PGobject | |
(result-set-read-column [pgobj metadata idx] | |
(let [type (.getType pgobj) | |
value (.getValue pgobj)] | |
(case type | |
"json" (json/read-str value :key-fn keyword) | |
:else value)))))) | |
(def users (atom {"admin" { :username "admin" | |
:password (creds/hash-bcrypt "clojure") | |
:roles #{::admin}}})) | |
(def db-spec {:subprotocol "postgresql", | |
:subname "//localhost:5432/knyb", | |
:user "knyb"}) | |
(def form (atom "initialform")) | |
(defn save-form [json-form] | |
(do | |
(swap! form (fn [_] json-form)) | |
(jdbc/update! db-spec :abhimata_event | |
{:title "Test event", | |
:signup_form json-form } | |
["event_id = ?" 1]))) | |
(defn load-form [] | |
(let [event-db-entry | |
(jdbc/query db-spec | |
["select * from abhimata_event where event_id = 1"])] | |
(-> event-db-entry | |
first | |
:signup_form))) | |
(defroutes app-routes | |
(GET "/" [] "Hello, world!") | |
(GET "/logout" [] (friend/logout* (resp/response "logout ok"))) | |
(GET "/secret" req | |
(friend/authorize #{::admin} "Admin's eyes only!")) | |
(POST "/form" {json-form :json-params} (save-form json-form) ) | |
(GET "/form" [] (resp/response @form)) | |
(GET "/dbform" [] (resp/response (load-form))) | |
(route/resources "/") | |
(route/not-found "Not Found")) | |
(def app | |
(-> | |
(friend/authenticate app-routes | |
{:allow-anon? true | |
:redirect-on-auth? false | |
:login-uri "/login" | |
:default-landing-uri "/" | |
; :unauthorized-handler #(-> (h/html5 [:h2 "You do not have sufficient privileges to access " (:uri %)]) resp/response (resp/status 401)) | |
:credential-fn #(creds/bcrypt-credential-fn @users %) | |
:workflows [(workflows/interactive-form)]}) | |
(handler/site) | |
(ringjson/wrap-json-params) | |
(ringjson/wrap-json-response {:pretty true}))) | |
;(defonce server | |
; (do | |
; (extend-jdbc-psql-protocols) | |
; (jetty/run-jetty #'app {:port 3000 :join? false}))) | |
(defn -main [& args] | |
(do | |
(extend-jdbc-psql-protocols) | |
(jetty/run-jetty #'app {:port 3000 :join? false}))) |
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
(defproject abhimata_backend "0.1.0-SNAPSHOT" | |
:description "Backend for abhimata" | |
:url "https://github/kryft/abhimata" | |
:license {:name "GPL3" } | |
:plugins [[cider/cider-nrepl "0.7.0-SNAPSHOT"] | |
[lein-ring "0.8.5"]] | |
:dependencies [[org.clojure/clojure "1.6.0"] | |
[org.clojure/data.json "0.2.5"] | |
[org.clojure/java.jdbc "0.3.4"] | |
[postgresql/postgresql "9.1-901.jdbc4"] | |
[com.cemerick/friend "0.2.1"] | |
[ring/ring-core "1.3.0"] | |
[ring/ring-json "0.3.1"] | |
[compojure "1.1.8"] | |
[ring/ring-jetty-adapter "1.3.0"]] | |
:ring {:handler abhimata_backend.core/app} | |
:aot [abhimata_backend.core] | |
:main abhimata_backend.core) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment