Skip to content

Instantly share code, notes, and snippets.

@rwat rwat/pg.web.core.clj
Last active Apr 30, 2018

Embed
What would you like to do?
(ns pg.web.core
(:require [clojure.core.async :refer [>! <! >!! <!! alts! chan go go-loop put! take! thread timeout close!] :as async]
[clojure.tools.namespace.repl :refer [refresh]]
[compojure.core :refer [defroutes GET POST]]
[compojure.route :as route]
[compojure.handler :as handler]
[integrant.core :as ig]
[ring.adapter.jetty :as jetty]
[ring.middleware.content-type :refer [wrap-content-type]]
[ring.middleware.format-response :refer [wrap-transit-json-response]]
[ring.middleware.params :refer [wrap-params]]
[ring.middleware.keyword-params :refer [wrap-keyword-params]]
[ring.middleware.session :refer [wrap-session]]
[pg.web.messaging :as messaging]
[pg.web.util :as web-util]))
(defn index
[req]
(let [session (:session req)]
{:status 200
:headers {"Content-Type" "text/html"}
:session (if (:uid session)
session
(assoc session :uid (web-util/unique-id)))
:body (slurp "./resources/public/index.html")}))
(defn test-page
[req]
{:status 200
:headers {"content-type" "text/html"}
:body (str "<html><body>" req "</body></html>")})
(defn message
[req]
{:status 200
:body (if-let [{session :session} req]
(messaging/msg-receive session)
["didn't find a session"])})
;;;; routes and server
(defroutes main-routes
(GET "/" [req] (index req))
(GET "/test" [req] (test-page req))
(wrap-transit-json-response
(POST "/message" req (message req)))
(route/resources "/")
(route/not-found "Page not found"))
(def app
(-> (handler/site main-routes)
; (wrap-defaults site-defaults)
; (wrap-reload {:dirs ["clj"]})
(wrap-content-type)
; (wrap-anti-forgery)
(wrap-params)
(wrap-keyword-params)
(wrap-session)))
;;;;;;
(def config {:adapter/jetty {:port 3000}})
(defmethod ig/init-key :adapter/jetty [_ {:keys [port] :as opts}]
(jetty/run-jetty app {:port port :join? false}))
;;(defmethod ig/init-key :messaging/state [_ _]
;; (fn [_] {}))
(defmethod ig/halt-key! :adapter/jetty [_ server]
(.stop server))
(comment
(def system (ig/init config))
(ig/halt! system)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.