<commit_before>(ns eventstore.handler
  (:gen-class)
  (:use org.httpkit.server)
  (:require [compojure.core :refer :all]
            [compojure.route :as route]
            [clojure.tools.logging :as log]
            [eventstore.muon :as m]
            [gniazdo.core :as ws]
            [ring.middleware.reload :as reload]
            [eventstore.streams :as streams]
            [ring.util.response :as response]
            [ring.middleware.json :as rjson]
            [clojure.data.json :as json]
            [clojure.core.async :as async]
            [serializable.fn :as sfn]
            [ring.middleware.params :as pms]
            [eventstore.db :as db]
            [eventstore.filedb :as filedb]
            [eventstore.api :as api]
            [eventstore.mongo :as mongo]
            #_[eventstore.riak :as riak]
            [compojure.handler :refer [site]]))

(defn async-handler [ring-request]
  (with-channel ring-request channel
    #_(send! channel {:status 200
                      :headers {"Content-Type" "text/plain"}
                      :body "Long polling?"})
    (on-receive channel (fn [data]
                          (send! channel data)))))

(defn wrap-json [r]
  (response/header (response/response (json/write-str r))
                   "Content-Type" "application/json"))

(extend Double json/JSONWriter
  {:-write (fn [object out]
             (cond (.isInfinite object)
                   (.print out 9007199254740992.0)
                   (.isNaN object)
                   (.print out 0.0)
                   :else
                   (.print out object)))})

(extend Exception json/JSONWriter
  {:-write (fn [object out]
             (.print out (.getMessage object)))})

(extend clojure.lang.AFunction json/JSONWriter
  {:-write (fn [object out]
             (.print out (pr-str object)))})

(extend org.bson.types.ObjectId json/JSONWriter
  {:-write (fn [object out]
             (.print out (str "\"" (.toString object) "\"")))})

(extend clojure.lang.Ref json/JSONWriter
  {:-write (fn [object out]
             (.print out (json/write-str @object)))})

(def cold-latency 5000)
#_(def riak-streams (riak/riak "streams"))
#_(def active-streams
    (ref (into #{} (map #_#(hash-map :stream (pr-str %))
                        #(json/read-str (first (:payload_s %)) :key-fn keyword)
                        (db/lazy-events riak-streams "streams" 0)))))

(def ms (m/start-server!
          #_(db/->DBDummy)
          #_(mongo/mongo)
          (filedb/->DBFile (clojure.java.io/resource "events.json"))
          #_(riak/riak riak/s-bucket)))
(def test-ds (:stm ms))

(defroutes app-routes
  (GET "/streams" []
       (wrap-json (streams/streams test-ds)))
  (GET "/projection-keys" []
       (wrap-json (api/projection-keys)))
  (GET "/projections" []
       (wrap-json (api/projections)))
  (GET "/stream/:stream-name" [stream-name]
       (wrap-json (api/stream test-ds stream-name)))
  (GET "/ws" [] async-handler)
  (GET "/thing" [] "Thing")
  (GET "/thing2" [] "Thing4")
  (GET "/first-projection" []
       (wrap-json (api/projection)))
  (POST "/projections" request (api/post-projection! test-ds (:body request)))
  (route/resources "/")
  (route/not-found "Not Found"))

(def app
  (rjson/wrap-json-body (pms/wrap-params (site app-routes)) {:keywords? true}))

(def reloadable-app
  (reload/wrap-reload #'app))

;; Workaround to have http-kit as the provider for Ring
;; In order to use http-kit, run `lein run` instead of `lein ring server`
(defn -main [& args]
  #_(future (m/start-server!))
  (let [handler (reload/wrap-reload #'app)]
    (println run-server)
    (time (run-server handler {:port 3000}))))

#_(let [socket (ws/connect "ws://localhost:3000/ws" :on-receive #(prn 'received %))]
  (ws/send-msg socket "hello")<commit_msg>Fix problem with hang at bootstrap<commit_after>(ns eventstore.handler
  (:gen-class)
  (:use org.httpkit.server)
  (:require [compojure.core :refer :all]
            [compojure.route :as route]
            [clojure.tools.logging :as log]
            [eventstore.muon :as m]
            [gniazdo.core :as ws]
            [ring.middleware.reload :as reload]
            [eventstore.streams :as streams]
            [ring.util.response :as response]
            [ring.middleware.json :as rjson]
            [clojure.data.json :as json]
            [clojure.core.async :as async]
            [serializable.fn :as sfn]
            [ring.middleware.params :as pms]
            [eventstore.db :as db]
            [eventstore.filedb :as filedb]
            [eventstore.api :as api]
            [eventstore.mongo :as mongo]
            #_[eventstore.riak :as riak]
            [compojure.handler :refer [site]]))

(defn async-handler [ring-request]
  (with-channel ring-request channel
    #_(send! channel {:status 200
                      :headers {"Content-Type" "text/plain"}
                      :body "Long polling?"})
    (on-receive channel (fn [data]
                          (send! channel data)))))

(defn wrap-json [r]
  (response/header (response/response (json/write-str r))
                   "Content-Type" "application/json"))

(extend Double json/JSONWriter
  {:-write (fn [object out]
             (cond (.isInfinite object)
                   (.print out 9007199254740992.0)
                   (.isNaN object)
                   (.print out 0.0)
                   :else
                   (.print out object)))})

(extend Exception json/JSONWriter
  {:-write (fn [object out]
             (.print out (.getMessage object)))})

(extend clojure.lang.AFunction json/JSONWriter
  {:-write (fn [object out]
             (.print out (pr-str object)))})

(extend org.bson.types.ObjectId json/JSONWriter
  {:-write (fn [object out]
             (.print out (str "\"" (.toString object) "\"")))})

(extend clojure.lang.Ref json/JSONWriter
  {:-write (fn [object out]
             (.print out (json/write-str @object)))})

(def cold-latency 5000)
#_(def riak-streams (riak/riak "streams"))
#_(def active-streams
    (ref (into #{} (map #_#(hash-map :stream (pr-str %))
                        #(json/read-str (first (:payload_s %)) :key-fn keyword)
                        (db/lazy-events riak-streams "streams" 0)))))

(def test-ds (filedb/->DBFile (clojure.java.io/resource "events.json")))

(defroutes app-routes
  (GET "/streams" []
       (wrap-json (streams/streams test-ds)))
  (GET "/projection-keys" []
       (wrap-json (api/projection-keys)))
  (GET "/projections" []
       (wrap-json (api/projections)))
  (GET "/stream/:stream-name" [stream-name]
       (wrap-json (api/stream test-ds stream-name)))
  (GET "/ws" [] async-handler)
  (GET "/thing" [] "Thing")
  (GET "/thing2" [] "Thing4")
  (GET "/first-projection" []
       (wrap-json (api/projection)))
  (POST "/projections" request (api/post-projection! test-ds (:body request)))
  (route/resources "/")
  (route/not-found "Not Found"))

(def app
  (rjson/wrap-json-body (pms/wrap-params (site app-routes)) {:keywords? true}))

(def reloadable-app
  (reload/wrap-reload #'app))

;; Workaround to have http-kit as the provider for Ring
;; In order to use http-kit, run `lein run` instead of `lein ring server`
(defn -main [& args]
  #_(future (m/start-server!))
  (def ms (m/start-server!
            #_(db/->DBDummy)
            #_(mongo/mongo)
            (filedb/->DBFile (clojure.java.io/resource "events.json"))
            #_(riak/riak riak/s-bucket)))
  (let [handler (reload/wrap-reload #'app)]
    (println run-server)
    (time (run-server handler {:port 3000}))))

#_(let [socket (ws/connect "ws://localhost:3000/ws" :on-receive #(prn 'received %))]
  (ws/send-msg socket "hello")
{'fraction_non_alphanumeric': 0.16104575163398693, 'fraction_numerical': 0.009673202614379085, 'mean_word_length': 2.2543598468736707, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, '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': '7119602', 'n_tokens_mistral': 2626, 'n_tokens_neox': 2496, 'n_words': 617}