<commit_before>(ns alder.core
  (:use ring.util.response
        ring.middleware.resource
        ring.middleware.content-type
        ring.middleware.not-modified

        compojure.core)

  (:require [alder.db :as db]
            [alder.views :as views]

            [clojure.string :as string]
            
            [compojure.route :as route]
            [compojure.handler :as handler]
            [compojure.response :as response]

            [chord.http-kit :refer [with-channel]]
            [org.httpkit.server :refer [run-server]]
            [clojure.core.async :refer [<! >! put! close! go-loop]]

            [taoensso.timbre :as timbre :refer [info]]))

(db/migrate)

(defn wrap-logging [handler]
  (fn [request]
    (let [response (handler request)]
      (info "HTTP"
            (:remote-addr request)
            (str \"
                 (-> request :request-method name string/upper-case) " "
                 (:uri request)
                 \")
            (:status response))
      response)))

(defmulti handle-message (fn [cmd _] cmd))

(defmethod handle-message :create-new [_ _]
  (let [patch (db/create-patch!)]
    [:reply :create-new (:short_id patch)]))

(defmethod handle-message :save-patch [_ args]
  [:reply :ok nil])

(defn api-channel-handler [request]
  (with-channel request ws-ch
    (go-loop []
      (when-let [{:keys [message]} (<! ws-ch)]
        (let [[command args] message
              response (handle-message command args)]
          (info "MSG"
                (:remote-addr request)
                command)
          (>! ws-ch response))
        (recur)))))

(defroutes main-routes
  (GET "/" [] (views/index-page))
  (GET ["/:short-id" :short-id #"[A-Za-z0-9]{12}"] [short-id] (views/index-page))
  (GET "/alder-api-ws" [] api-channel-handler))

(def app
  (-> (handler/site main-routes)
      (wrap-logging)
      (wrap-resource "public")
      (wrap-content-type)
      (wrap-not-modified)))

(defonce server (atom nil))

(defn stop-server []
  (when-not (nil? @server)
    (@server :timeout 100)
    (reset! server nil)))

(defn -main [& args]
  (reset! server (run-server app {:port 3449})))
<commit_msg>Log websocket requests before they are processed<commit_after>(ns alder.core
  (:use ring.util.response
        ring.middleware.resource
        ring.middleware.content-type
        ring.middleware.not-modified

        compojure.core)

  (:require [alder.db :as db]
            [alder.views :as views]

            [clojure.string :as string]
            
            [compojure.route :as route]
            [compojure.handler :as handler]
            [compojure.response :as response]

            [chord.http-kit :refer [with-channel]]
            [org.httpkit.server :refer [run-server]]
            [clojure.core.async :refer [<! >! put! close! go-loop]]

            [taoensso.timbre :as timbre :refer [info]]))

(db/migrate)

(defn wrap-logging [handler]
  (fn [request]
    (let [response (handler request)]
      (info "HTTP"
            (:remote-addr request)
            (str \"
                 (-> request :request-method name string/upper-case) " "
                 (:uri request)
                 \")
            (:status response))
      response)))

(defmulti handle-message (fn [cmd _] cmd))

(defmethod handle-message :create-new [_ _]
  (let [patch (db/create-patch!)]
    [:reply :create-new (:short_id patch)]))

(defmethod handle-message :save-patch [_ args]
  [:reply :ok nil])

(defn api-channel-handler [request]
  (with-channel request ws-ch
    (go-loop []
      (when-let [{:keys [message]} (<! ws-ch)]
        (let [[command args] message]
          (info "MSG"
                (:remote-addr request)
                command)
          (let [response (handle-message command args)]
            (>! ws-ch response)))
        (recur)))))

(defroutes main-routes
  (GET "/" [] (views/index-page))
  (GET ["/:short-id" :short-id #"[A-Za-z0-9]{12}"] [short-id] (views/index-page))
  (GET "/alder-api-ws" [] api-channel-handler))

(def app
  (-> (handler/site main-routes)
      (wrap-logging)
      (wrap-resource "public")
      (wrap-content-type)
      (wrap-not-modified)))

(defonce server (atom nil))

(defn stop-server []
  (when-not (nil? @server)
    (@server :timeout 100)
    (reset! server nil)))

(defn -main [& args]
  (reset! server (run-server app {:port 3449})))

{'fraction_non_alphanumeric': 0.15884641794460974, 'fraction_numerical': 0.0050354772259098195, 'mean_word_length': 2.143884892086331, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '3782550', 'n_tokens_mistral': 1478, 'n_tokens_neox': 1423, 'n_words': 331}