<commit_before>(ns chat.server.handler
  (:gen-class)
  (:require [org.httpkit.server :refer [run-server]]
            [compojure.core :refer [routes]]
            [ring.middleware.defaults :refer [wrap-defaults api-defaults
                                              secure-site-defaults site-defaults]]
            [ring.middleware.edn :refer [wrap-edn-params]]
            [ring.middleware.cors :refer [wrap-cors]]
            [ring.util.response :refer [get-header]]
            [clojurewerkz.quartzite.scheduler :as qs]
            [clojure.tools.nrepl.server :as nrepl]
            [chat.server.sync :as sync :refer [sync-routes]]
            [chat.server.routes :as routes
             :refer [desktop-client-routes
                     mobile-client-routes
                     api-private-routes
                     api-public-routes
                     resource-routes
                     extension-routes]]
            [environ.core :refer [env]]
            ; just requiring to register multimethods
            chat.server.extensions.asana
            [chat.server.email-digest :as email-digest]))

(if (= (env :environment) "prod")
  (do
    (require 'taoensso.carmine.ring)
    (def ^:dynamic *redis-conf* {:pool {}
                                 :spec {:host "127.0.0.1"
                                        :port 6379}})
    (let [carmine-store (ns-resolve 'taoensso.carmine.ring 'carmine-store)]
      (def session-store
        (carmine-store '*redis-conf* {:expiration-secs (* 60 60 24 7)
                                      :key-prefix "braid"}))))
  (do
    (require 'ring.middleware.session.memory)
    (let [memory-store (ns-resolve 'ring.middleware.session.memory 'memory-store)]
      (def session-store (memory-store)))))

(defn assoc-cookie-conf [defaults]
  (-> defaults
      (assoc-in [:session :cookie-attrs :secure] (= (env :environment) "prod"))
      (assoc-in [:session :cookie-attrs :max-age] (* 60 60 24 7))
      (assoc-in [:session :store] session-store)))

(defn assoc-csrf-conf [defaults]
  (-> defaults
      (assoc-in [:security :anti-forgery]
        {:read-token (fn [req] (-> req :params :csrf-token))})))

(def static-site-defaults
  {:static {:resources "public"}
   :responses {:not-modified-responses true
               :absolute-redirects     true
               :content-types          true
               :default-charset        "utf-8"}})

(def mobile-client-app
  (-> (routes
        resource-routes
        mobile-client-routes)
      (wrap-defaults static-site-defaults)))

(def desktop-client-app
  (-> (routes
        resource-routes
        desktop-client-routes)
      (wrap-defaults static-site-defaults)))

(def api-server-app
  (-> (routes
        (-> extension-routes
            (wrap-defaults (-> api-defaults
                               assoc-cookie-conf
      ;                         assoc-csrf-conf
                               )))
        (-> api-private-routes
            (wrap-defaults (-> api-defaults
                               assoc-cookie-conf
      ;                         assoc-csrf-conf
                               )))
        (-> api-public-routes
            (wrap-defaults (-> site-defaults
                               assoc-cookie-conf)))
        (-> sync-routes
            (wrap-defaults (-> api-defaults
                               assoc-cookie-conf
      ;                         assoc-csrf-conf
                               ))))
      (wrap-cors :access-control-allow-origin [#".*"]
                 :access-control-allow-credentials true
                 :access-control-allow-methods [:get :put :post :delete])
      wrap-edn-params))

;; server
(defonce servers (atom {:api nil
                        :mobile nil
                        :desktop nil}))
<commit_msg>disable csrf check to let api routes work<commit_after>(ns chat.server.handler
  (:gen-class)
  (:require [org.httpkit.server :refer [run-server]]
            [compojure.core :refer [routes]]
            [ring.middleware.defaults :refer [wrap-defaults api-defaults
                                              secure-site-defaults site-defaults]]
            [ring.middleware.edn :refer [wrap-edn-params]]
            [ring.middleware.cors :refer [wrap-cors]]
            [ring.util.response :refer [get-header]]
            [clojurewerkz.quartzite.scheduler :as qs]
            [clojure.tools.nrepl.server :as nrepl]
            [chat.server.sync :as sync :refer [sync-routes]]
            [chat.server.routes :as routes
             :refer [desktop-client-routes
                     mobile-client-routes
                     api-private-routes
                     api-public-routes
                     resource-routes
                     extension-routes]]
            [environ.core :refer [env]]
            ; just requiring to register multimethods
            chat.server.extensions.asana
            [chat.server.email-digest :as email-digest]))

(if (= (env :environment) "prod")
  (do
    (require 'taoensso.carmine.ring)
    (def ^:dynamic *redis-conf* {:pool {}
                                 :spec {:host "127.0.0.1"
                                        :port 6379}})
    (let [carmine-store (ns-resolve 'taoensso.carmine.ring 'carmine-store)]
      (def session-store
        (carmine-store '*redis-conf* {:expiration-secs (* 60 60 24 7)
                                      :key-prefix "braid"}))))
  (do
    (require 'ring.middleware.session.memory)
    (let [memory-store (ns-resolve 'ring.middleware.session.memory 'memory-store)]
      (def session-store (memory-store)))))

(defn assoc-cookie-conf [defaults]
  (-> defaults
      (assoc-in [:session :cookie-attrs :secure] (= (env :environment) "prod"))
      (assoc-in [:session :cookie-attrs :max-age] (* 60 60 24 7))
      (assoc-in [:session :store] session-store)))

(defn assoc-csrf-conf [defaults]
  (-> defaults
      (assoc-in [:security :anti-forgery]
        {:read-token (fn [req] (-> req :params :csrf-token))})))

(def static-site-defaults
  {:static {:resources "public"}
   :responses {:not-modified-responses true
               :absolute-redirects     true
               :content-types          true
               :default-charset        "utf-8"}})

(def mobile-client-app
  (-> (routes
        resource-routes
        mobile-client-routes)
      (wrap-defaults static-site-defaults)))

(def desktop-client-app
  (-> (routes
        resource-routes
        desktop-client-routes)
      (wrap-defaults static-site-defaults)))

; XXX: Review use of CSRF
(def api-server-app
  (-> (routes
        (-> extension-routes
            (wrap-defaults (-> api-defaults
                               assoc-cookie-conf
      ;                         assoc-csrf-conf
                               )))
        (-> api-private-routes
            (wrap-defaults (-> api-defaults
                               assoc-cookie-conf
      ;                         assoc-csrf-conf
                               )))
        (-> api-public-routes
            (wrap-defaults (-> site-defaults
                               (assoc-in [:security :anti-forgery] false)
                               assoc-cookie-conf)))
        (-> sync-routes
            (wrap-defaults (-> api-defaults
                               assoc-cookie-conf
      ;                         assoc-csrf-conf
                               ))))
      (wrap-cors :access-control-allow-origin [#".*"]
                 :access-control-allow-credentials true
                 :access-control-allow-methods [:get :put :post :delete])
      wrap-edn-params))

;; server
(defonce servers (atom {:api nil
                        :mobile nil
                        :desktop nil}))

{'fraction_non_alphanumeric': 0.12736215376650273, 'fraction_numerical': 0.0064716541548019675, 'mean_word_length': 1.4773966014748316, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2332547', 'n_tokens_mistral': 2234, 'n_tokens_neox': 2190, 'n_words': 430}