<commit_before>(ns eventframework.app
  (:use 
    [eventframework.commands :only [is-valid-position put-command listen-commands]]
    [compojure.core :only [defroutes context GET PUT]])
  (:require 
    [compojure.handler :as handler]
    [ring.util.response :as response]
    [compojure.route :as route]
    aleph.http
    lamina.core
    cheshire.core))

(defn json-response [data]
  (response/content-type 
    (response/response (cheshire.core/generate-string data))
    "application/json"))

(defn getevents-handler [channel request] 
  (let [position (:position (:params request))]
    (if (not (is-valid-position position))
      (lamina.core/enqueue channel (json-response {:goaway true}))
      (listen-commands
        position
        (fn [position commands]
          (lamina.core/enqueue channel (json-response {
            :position position
            :events commands
          })))))))

(defroutes ajax
  (GET "/foo" [] "foo")
  (GET "/events" [] (aleph.http/wrap-aleph-handler getevents-handler))
  (PUT "/command/:type/:uuid" {
      route-params :route-params
      form-params :form-params
      remote-addr :remote-addr
    }
    (let [{type :type uuid :uuid} route-params]
      (put-command uuid {
        :type (keyword type)
        :uuid uuid
        :remote-addr remote-addr
        :payload (zipmap (map keyword (keys form-params)) (vals form-params))
      })
      uuid)))

(defroutes ui
  (GET "/" []  (response/resource-response "index.html" {:root "public"})))

(defroutes towrap
  (context "/ajax" [] ajax)
  ui
  (route/resources "/")
  (route/not-found (response/resource-response "404.html" {:root "error"})))

(def app (handler/site towrap))<commit_msg>Put only the JSON in the channel, not the whole response<commit_after>(ns eventframework.app
  (:use 
    [eventframework.commands :only [is-valid-position put-command listen-commands]]
    [compojure.core :only [defroutes context GET PUT]])
  (:require 
    [compojure.handler :as handler]
    [ring.util.response :as response]
    [compojure.route :as route]
    aleph.http
    lamina.core
    cheshire.core))

(defn getevents [position]
    (if (not (is-valid-position position))
      (cheshire.core/generate-string {:goaway true})
      (let [ch (lamina.core/channel)]
	      (listen-commands
	        position
	        (fn [position commands] (do
	          (lamina.core/enqueue ch (cheshire.core/generate-string {
	            :position position
	            :events commands
            }))
            (lamina.core/close ch))))
        ch)))

(defroutes ajax
  (GET "/foo" [] "foo")
  (GET "/events" [position] {
     :status 200
     :headers {"content-type" "application/json"}
     :body (getevents position)})
  (PUT "/command/:type/:uuid" {
      route-params :route-params
      form-params :form-params
      remote-addr :remote-addr
    }
    (let [{type :type uuid :uuid} route-params]
      (put-command uuid {
        :type (keyword type)
        :uuid uuid
        :remote-addr remote-addr
        :payload (zipmap (map keyword (keys form-params)) (vals form-params))
      })
      uuid)))

(defroutes ui
  (GET "/" []  (response/resource-response "index.html" {:root "public"})))

(defroutes towrap
  (context "/ajax" [] ajax)
  ui
  (route/resources "/")
  (route/not-found (response/resource-response "404.html" {:root "error"})))

(def app (handler/site towrap))
{'fraction_non_alphanumeric': 0.14484185634052615, 'fraction_numerical': 0.0026603606266627253, 'mean_word_length': 2.7392265193370164, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '743989', 'n_tokens_mistral': 1094, 'n_tokens_neox': 1068, 'n_words': 265}