Skip to content

Instantly share code, notes, and snippets.

@wmealing
Created October 19, 2015 15:12
Show Gist options
  • Save wmealing/716ced0b2b6367bee153 to your computer and use it in GitHub Desktop.
Save wmealing/716ced0b2b6367bee153 to your computer and use it in GitHub Desktop.
(ns inky-brain.rest
(:require [liberator.core :refer [resource defresource]]
[ring.middleware.params :refer [wrap-params]]
[compojure.core :refer [defroutes ANY]]
[clojure.java.io :as io]
[clojure.data.json :as json]
[inky-dispatcher.plugins :as dispatch]
[com.keminglabs.zmq-async.core :refer [register-socket!]]
[clojure.core.async :refer [>! <! <!! >!! go
go-loop chan timeout
sliding-buffer close!]])
(:use [ring.adapter.jetty]
[liberator.representation :only [ring-response]]
[liberator.dev :only [wrap-trace]]))
(def ^:private pub-in (chan (sliding-buffer 64)))
(def ^:private pub-out (chan (sliding-buffer 64)))
(defn bind-publish
"Registers to publish on port"
[]
(register-socket! {:in pub-in :out pub-out :socket-type :sub
:configurator (fn [socket]
(.bind socket "tcp://127.0.0.1:5454")
)}))
(defn- publish-line
"Sends status to every peer"
[line]
(go (>! pub-in "TEST WORLD")
(close! pub-in)
)
)
(defresource handle-anything []
:available-media-types ["application/json"]
:allowed-methods [:get :post]
:can-put-to-missing? false
:malformed? (fn [_] false)
:post-to-existing? (fn [_] true)
:post! (fn [ctx]
( let [ id 3]
(publish-line "WADE WORLD")
)
{::id 3}
)
:handle-created (fn [ctx] (str (:id ctx))) ;; not sure this is ever called.
:post-redirect? (fn [ctx] {:location (format "/yeah/%s" (:id ctx))})
)
(defroutes app
(ANY "/anything/" [] (handle-anything) ))
(def handler
(-> app
(wrap-trace :header :ui)))
(bind-publish)
(dispatch/load-plugins)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment