<commit_before>(ns dictionator.server
  (:require [com.stuartsierra.component :as component]
            [org.httpkit.server :as httpkit]
            [ring.middleware.transit :refer [wrap-transit-response wrap-transit-body]]
            [ring.middleware.resource :refer [wrap-resource]]
            [ring.util.response :refer [response]]
            [bidi.bidi :as bidi]))

(def routes
  ["" {"/api" {:get {[""] :read}
               :post {[""] :mutate}}}])

(def handlers
  {:read (fn [req]
           (response {:vals [:a :b :c]}))
   :mutate (fn [req]
             (response {:keys [:a :b :c]}))
   :not-found (fn [req]
                (response {:code :not-found}))})

(defn router [{:keys [uri request-method] :as req}]
  (let [{:keys [handler]} (bidi/match-route routes uri :request-method request-method)]
    ((get handlers handler :not-found)
     req)))

(defn wrap-state [handler app-state]
  (fn [req]
    (handler (assoc req :app-state app-state))))

(defn prod-handler [app-state]
  (-> router
      (wrap-state app-state)
      wrap-transit-response
      wrap-transit-body
      (wrap-resource "public")))

(defn dev-handler [app-state]
  (fn [req]
    ((-> (prod-handler app-state)
         (wrap-resource "public"))
     req)))

(defrecord WebServer [port handler app-state shut-down]
  component/Lifecycle
  (start [component]
    (if shut-down ;; don't start server if shut-down function present - server already started
      component
      (let [request-handler (handler app-state)
            server (httpkit/run-server request-handler {:port port})] ;; server constructor returns shutdown-fn
        (assoc component :shut-down server))))
  (stop [component]
    (when-let [shutdown-fn (:shut-down component)]
      (shutdown-fn))
    (dissoc component :shut-down)))<commit_msg>Don't serve resources in production mode<commit_after>(ns dictionator.server
  (:require [com.stuartsierra.component :as component]
            [org.httpkit.server :as httpkit]
            [ring.middleware.transit :refer [wrap-transit-response wrap-transit-body]]
            [ring.middleware.resource :refer [wrap-resource]]
            [ring.util.response :refer [response]]
            [bidi.bidi :as bidi]))

(def routes
  ["" {"/api" {:get {[""] :read}
               :post {[""] :mutate}}}])

(def handlers
  {:read (fn [req]
           (response {:vals [:a :b :c]}))
   :mutate (fn [req]
             (response {:keys [:a :b :c]}))
   :not-found (fn [req]
                (response {:code :not-found}))})

(defn router [{:keys [uri request-method] :as req}]
  (let [{:keys [handler]} (bidi/match-route routes uri :request-method request-method)]
    ((get handlers handler :not-found)
     req)))

(defn wrap-state [handler app-state]
  (fn [req]
    (handler (assoc req :app-state app-state))))

(defn prod-handler [app-state]
  (-> router
      (wrap-state app-state)
      wrap-transit-response
      wrap-transit-body))

(defn dev-handler [app-state]
  (fn [req]
    ((-> (prod-handler app-state)
         (wrap-resource "public"))
     req)))

(defrecord WebServer [port handler app-state shut-down]
  component/Lifecycle
  (start [component]
    (if shut-down ;; don't start server if shut-down function present - server already started
      component
      (let [request-handler (handler app-state)
            server (httpkit/run-server request-handler {:port port})] ;; server constructor returns shutdown-fn
        (assoc component :shut-down server))))
  (stop [component]
    (when-let [shutdown-fn (:shut-down component)]
      (shutdown-fn))
    (dissoc component :shut-down)))
{'fraction_non_alphanumeric': 0.15760266370699222, 'fraction_numerical': 0.0, 'mean_word_length': 2.7907465825446898, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '4098042', 'n_tokens_mistral': 1175, 'n_tokens_neox': 1143, 'n_words': 308}