<commit_before>(ns sse-server.core
  (:require [org.httpkit.server :refer [run-server with-channel on-close send! close]]
            [org.httpkit.timer :refer [schedule-task]]))

(def channel-hub (atom {}))

(defn handle-404 []
  {:status 404
   :headers {"Content-Type" "text/plain"}
   :body "File not found"})

(defn handle-connections []
  {:status 200
   :headers {"Content-Type" "text/plain"
             "Cache-Control" "no-cache"
             "Connection" "close"}
   :body (str (count @channel-hub))})


(defn handle-sse [req]
  (with-channel req channel
    (send! channel {:status 200
                    :headers {"Content-Type" "text/event-stream"
                              "Cache-Control" "no-cache"
                              "Connection" "keep-alive"}
                    :body ":ok\n\n"} false)
    (swap! channel-hub assoc channel req)
    (on-close channel (fn [_]
                        (swap! channel-hub dissoc channel)))))

(defn app [req]
  (case (:uri req)
    "/connections" (handle-connections)
    "/sse" (handle-sse req)
    (handle-404)))

(defn notify-channels []
  (let [time (System/currentTimeMillis)]
    (doseq [channel (keys @channel-hub)]
      (send! channel (str "data: " time "\n\n") false))))

(defn start-timer []<commit_msg>Add CORS support to clojure-http-kit implementation<commit_after>(ns sse-server.core
  (:require [org.httpkit.server :refer [run-server with-channel on-close send! close]]
            [org.httpkit.timer :refer [schedule-task]]))

(def channel-hub (atom {}))

(defn handle-cors []
  {:status 204
   :headers {"Access-Control-Allow-Origin" "*"
             "Connection" "close"}})

(defn handle-404 []
  {:status 404
   :headers {"Content-Type" "text/plain"}
   :body "File not found"})

(defn handle-connections []
  {:status 200
   :headers {"Content-Type" "text/plain"
             "Access-Control-Allow-Origin" "*"
             "Cache-Control" "no-cache"
             "Connection" "close"}
   :body (str (count @channel-hub))})


(defn handle-sse [req]
  (with-channel req channel
    (send! channel {:status 200
                    :headers {"Content-Type" "text/event-stream"
                              "Access-Control-Allow-Origin" "*"
                              "Cache-Control" "no-cache"
                              "Connection" "keep-alive"}
                    :body ":ok\n\n"} false)
    (swap! channel-hub assoc channel req)
    (on-close channel (fn [_]
                        (swap! channel-hub dissoc channel)))))

(defn app [req]
  (if (= (:request-method req) :options)
    (handle-cors)
    (case (:uri req)
      "/connections" (handle-connections)
      "/sse" (handle-sse req)
      (handle-404))))

(defn notify-channels []
  (let [time (System/currentTimeMillis)]
    (doseq [channel (keys @channel-hub)]
      (send! channel (str "data: " time "\n\n") false))))

(defn start-timer []
{'fraction_non_alphanumeric': 0.16113416320885202, 'fraction_numerical': 0.011410788381742738, 'mean_word_length': 2.2875, 'pattern_counts': {'":': 2, '<': 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': '3506448', 'n_tokens_mistral': 965, 'n_tokens_neox': 913, 'n_words': 228}