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 clojure-play.core | |
(:use org.httpkit.server | |
[compojure.core :refer :all] | |
[compojure.route :as route] | |
[clojure.data.json :as json] | |
[clojure.tools.logging :only [info]] | |
[clojure-play.routes :as routes] | |
[ring.middleware.json :only [wrap-json-body]] | |
[ring.middleware.cors :refer [wrap-cors]]) | |
(:require [monger.core :as mg] | |
[monger.collection :as mc] | |
[clojure.edn :as edn] | |
[clojure.java.io :as io] | |
[compojure.handler :as handler]) | |
(:import [org.bson.types ObjectId] | |
[com.mongodb DB WriteConcern]) | |
(:gen-class)) | |
(println "in the beginning was the command line...") | |
(defonce channels (atom #{})) | |
(defn connect! [channel] | |
(info "channel open") | |
(swap! channels conj channel)) | |
(defn notify-clients [msg] | |
(doseq [channel @channels] | |
(send! channel msg))) | |
(defn disconnect! [channel status] | |
(info "channel closed:" status) | |
(swap! channels #(remove #{channel} %))) | |
(defn ws-handler [request] | |
(with-channel request channel | |
(connect! channel) | |
(on-close channel (partial disconnect! channel)) | |
(on-receive channel #(notify-clients %)))) | |
(defn my-routes [db] | |
(routes | |
(GET "/foo" [] "Hello Foo") | |
(GET "/bar" [] "Hello Bar") | |
(GET "/json_example/:name" [] routes/json_example) | |
(GET "/json_example" [] routes/json_example) | |
(POST "/email" [] routes/post_email) | |
(POST "/write_comment" [] (fn [req] (routes/write_comment req db))) | |
(POST "/update_comment" [] (fn [req] (routes/update_comment req db))) | |
(GET "/read_comments/:path" [path] (fn [req] (routes/read_comments req db path))) | |
(GET "/read_comments/:path1/:path2" [path1 path2] (fn [req] (routes/read_comments req db (str path1 "/" path2)))) | |
(GET "/ws" [] ws-handler))) | |
(defn connectDB [] | |
(defonce connection | |
(let | |
[uri "mongodb://patientplatypus:platy1@ds239681.mlab.com:39681/daily_blech" | |
{:keys [conn db]} (mg/connect-via-uri uri)] | |
{:conn conn | |
:db db})) | |
{:db (:db connection) | |
:conn (:conn connection)}) | |
(def cors-headers | |
"Generic CORS headers" | |
{"Access-Control-Allow-Origin" "*" | |
"Access-Control-Allow-Headers" "*" | |
"Access-Control-Allow-Methods" "GET POST OPTIONS DELETE PUT"}) | |
(defn preflight? | |
"Returns true if the request is a preflight request" | |
[request] | |
(= (request :request-method) :options)) | |
(defn -main | |
"this is main" | |
[& args] | |
(println "hello there main") | |
(def db (get (connectDB) :db)) | |
(println (read-string (slurp (io/resource "environment/config.edn")))) | |
(defn wrap-preflight [handler] | |
(fn [request] | |
(do | |
(println "inside wrap-preflight") | |
(println "value of request") | |
(println request) | |
(println "value of handler") | |
(println handler) | |
(if (preflight? request) | |
({:status 200 | |
:headers cors-headers | |
:body "preflight complete"}) | |
(request))))) | |
(run-server | |
(wrap-preflight | |
(wrap-cors | |
(wrap-json-body | |
(my-routes db) | |
{:keywords? true :bigdecimals? true}) | |
:access-control-allow-origin [#"http://www.thedailyblech.com"] | |
:access-control-allow-methods [:get :put :post :delete :options] | |
:access-control-allow-headers ["Origin" "X-Requested-With" | |
"Content-Type" "Accept"])) | |
{:port 4000})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment