<commit_before>(ns pc.server
  (:require [cemerick.url :as url]
            [cheshire.core :as json]
            [clojure.core.async :as async]
            [clojure.set :as set]
            [clojure.tools.logging :as log]
            [clojure.tools.reader.edn :as edn]
            [clj-time.core :as time]
            [clj-time.format :as time-format]
            [compojure.core :refer (defroutes routes GET POST ANY)]
            [compojure.handler :refer (site)]
            [compojure.route]
            [crypto.equality :as crypto]
            [datomic.api :refer [db q] :as d]
            [org.httpkit.server :as httpkit]
            [pc.admin.db :as db-admin]
            [pc.datomic :as pcd]
            [pc.http.datomic :as datomic]
            [pc.http.doc :refer (duplicate-doc)]
            [pc.http.lb :as lb]
            [pc.http.sente :as sente]
            [pc.auth :as auth]
            [pc.auth.google :refer (google-client-id)]
            [pc.models.access-grant :as access-grant-model]
            [pc.models.cust :as cust]
            [pc.models.doc :as doc-model]
            [pc.models.layer :as layer]
            [pc.models.permission :as permission-model]
            [pc.rollbar :as rollbar]
            [pc.profile :as profile]
            [pc.less :as less]
            [pc.views.content :as content]
            [pc.views.blog :as blog]
            [pc.utils :refer (inspect)]
            [pc.convert :refer (svg->png)]
            [pc.render :refer (render-layers)]
            [ring.middleware.anti-forgery :refer (wrap-anti-forgery)]
            [ring.middleware.reload :refer (wrap-reload)]
            [ring.middleware.session :refer (wrap-session)]
            [ring.middleware.session.cookie :refer (cookie-store)]
            [ring.util.response :refer (redirect)]
            [slingshot.slingshot :refer (try+ throw+)])
  (:import java.util.UUID))

(defn ssl? [req]
  (or (= :https (:scheme req))
      (= "https" (get-in req [:headers "x-forwarded-proto"]))))

(def bucket-doc-ids (atom #{}))

(defn clean-bucket-doc-ids []
  (swap! bucket-doc-ids (fn [b]
                          (set/intersection b (set (keys @sente/document-subs))))))

;; TODO: make this reloadable without reloading the server
(defn app [sente-state]
  (routes
   (POST "/api/entity-ids" request
         (datomic/entity-id-request (-> request :body slurp edn/read-string :count)))

   (GET "/document/:document-id.svg" [document-id :as req]
        (let [db (pcd/default-db)
              doc (doc-model/find-by-id db (Long/parseLong document-id))]
          (cond (nil? doc)
                (if-let [doc (doc-model/find-by-invalid-id db (Long/parseLong document-id))]
                  (redirect (str "/document/" (:db/id doc) ".svg"))

                  {:status 404
                   ;; TODO: Maybe return a "not found" image.
                   :body "Document not found."})
<commit_msg>hack to make server not redirect haproxy health checks<commit_after>(ns pc.server
  (:require [cemerick.url :as url]
            [cheshire.core :as json]
            [clojure.core.async :as async]
            [clojure.set :as set]
            [clojure.tools.logging :as log]
            [clojure.tools.reader.edn :as edn]
            [clj-time.core :as time]
            [clj-time.format :as time-format]
            [compojure.core :refer (defroutes routes GET POST ANY)]
            [compojure.handler :refer (site)]
            [compojure.route]
            [crypto.equality :as crypto]
            [datomic.api :refer [db q] :as d]
            [org.httpkit.server :as httpkit]
            [pc.admin.db :as db-admin]
            [pc.datomic :as pcd]
            [pc.http.datomic :as datomic]
            [pc.http.doc :refer (duplicate-doc)]
            [pc.http.lb :as lb]
            [pc.http.sente :as sente]
            [pc.auth :as auth]
            [pc.auth.google :refer (google-client-id)]
            [pc.models.access-grant :as access-grant-model]
            [pc.models.cust :as cust]
            [pc.models.doc :as doc-model]
            [pc.models.layer :as layer]
            [pc.models.permission :as permission-model]
            [pc.rollbar :as rollbar]
            [pc.profile :as profile]
            [pc.less :as less]
            [pc.views.content :as content]
            [pc.views.blog :as blog]
            [pc.utils :refer (inspect)]
            [pc.convert :refer (svg->png)]
            [pc.render :refer (render-layers)]
            [ring.middleware.anti-forgery :refer (wrap-anti-forgery)]
            [ring.middleware.reload :refer (wrap-reload)]
            [ring.middleware.session :refer (wrap-session)]
            [ring.middleware.session.cookie :refer (cookie-store)]
            [ring.util.response :refer (redirect)]
            [slingshot.slingshot :refer (try+ throw+)])
  (:import java.util.UUID))

(defn ssl? [req]
  (or (= :https (:scheme req))
      (= "https" (get-in req [:headers "x-forwarded-proto"]))
      (seq (get-in req [:headers "x-haproxy-server-state"]))))

(def bucket-doc-ids (atom #{}))

(defn clean-bucket-doc-ids []
  (swap! bucket-doc-ids (fn [b]
                          (set/intersection b (set (keys @sente/document-subs))))))

;; TODO: make this reloadable without reloading the server
(defn app [sente-state]
  (routes
   (POST "/api/entity-ids" request
         (datomic/entity-id-request (-> request :body slurp edn/read-string :count)))

   (GET "/document/:document-id.svg" [document-id :as req]
        (let [db (pcd/default-db)
              doc (doc-model/find-by-id db (Long/parseLong document-id))]
          (cond (nil? doc)
                (if-let [doc (doc-model/find-by-invalid-id db (Long/parseLong document-id))]
                  (redirect (str "/document/" (:db/id doc) ".svg"))

                  {:status 404
                   ;; TODO: Maybe return a "not found" image.
                   :body "Document not found."})

{'fraction_non_alphanumeric': 0.14486139283299526, 'fraction_numerical': 0.0010141987829614604, 'mean_word_length': 1.8338122605363985, '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': '720027', 'n_tokens_mistral': 1928, 'n_tokens_neox': 1913, 'n_words': 457}