<commit_before>(ns clojuredocs.main
  (:require [aleph.http :as ah]
            [somnium.congomongo :as mon]
            [clojuredocs.env :as env]
            [clojuredocs.entry :as entry]
            [clojuredocs.config :as config]))

(defn start-http-server [entry-point opts]
  (ah/start-http-server
    (ah/wrap-ring-handler
      (fn [r]
        (let [resp (entry-point r)]
          (if (:status resp)
            resp
            (assoc resp :status 200)))))
    opts))

(defn valid-env-or-exit []
  (let [res (->> [(env/int :port)                  "Port missing"
                  (:client-id config/gh-creds)     "GH client ID missing"
                  (:client-secret config/gh-creds) "GH client secret missing"
                  config/base-url                  "base url missing"]
                 (partition 2)
                 (map #(when (nil? (first %))
                         (println " !" (second %))
                         true))
                 (reduce #(or %1 %2)))]
    (when res
      (println)
      (println " ! Missing required config vars, exiting.")
      (println)
      #_(System/exit 1))))

(defn create-app []
  {:port (env/int :port 8080)
   :entry #'entry/routes
   :mongo-url (env/str :mongo-url)})

(defn start [{:keys [port mongo-url entry] :as opts}]
  (mon/set-connection! (mon/make-connection mongo-url))
  (mon/add-index! :examples [:ns :name :library-url])
  (mon/add-index! :vars [:ns :name :library-url])
  (let [stop-server (start-http-server entry
                      {:port port :join? false})]
    (println (format "Server running on port %d" port))
    (merge
      opts
      {:stop-server stop-server})))

(defn stop [{:keys [stop-server] :as opts}]
  (when stop-server
    @(stop-server))
  (dissoc opts :stop-server))

(defn -main []
  (valid-env-or-exit)
  (start (create-app)))<commit_msg>Add initialize indexes on app start<commit_after>(ns clojuredocs.main
  (:require [aleph.http :as ah]
            [somnium.congomongo :as mon]
            [clojuredocs.env :as env]
            [clojuredocs.entry :as entry]
            [clojuredocs.config :as config]))

(defn start-http-server [entry-point opts]
  (ah/start-http-server
    (ah/wrap-ring-handler
      (fn [r]
        (let [resp (entry-point r)]
          (if (:status resp)
            resp
            (assoc resp :status 200)))))
    opts))

(defn valid-env-or-exit []
  (let [res (->> [(env/int :port)                  "Port missing"
                  (:client-id config/gh-creds)     "GH client ID missing"
                  (:client-secret config/gh-creds) "GH client secret missing"
                  config/base-url                  "base url missing"]
                 (partition 2)
                 (map #(when (nil? (first %))
                         (println " !" (second %))
                         true))
                 (reduce #(or %1 %2)))]
    (when res
      (println)
      (println " ! Missing required config vars, exiting.")
      (println)
      #_(System/exit 1))))

(defn create-app []
  {:port (env/int :port 8080)
   :entry #'entry/routes
   :mongo-url (env/str :mongo-url)})

(defn start [{:keys [port mongo-url entry] :as opts}]
  (mon/set-connection! (mon/make-connection mongo-url))
  (mon/add-index! :examples [:ns :name :library-url])
  (mon/add-index! :vars [:ns :name :library-url])
  (mon/add-index! :namespaces [:name])
  (mon/add-index! :see-alsos [:name :ns :library-url])
  (mon/add-index! :libraries [:namespaces])
  (mon/add-index! :var-comments [:var.ns :var.name :var.library-url])
  (let [stop-server (start-http-server entry
                      {:port port :join? false})]
    (println (format "Server running on port %d" port))
    (merge
      opts
      {:stop-server stop-server})))

(defn stop [{:keys [stop-server] :as opts}]
  (when stop-server
    @(stop-server))
  (dissoc opts :stop-server))

(defn -main []
  (valid-env-or-exit)
  (start (create-app)))
{'fraction_non_alphanumeric': 0.15785460091509915, 'fraction_numerical': 0.005592272496187087, 'mean_word_length': 1.9878511769172362, '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': '1527193', 'n_tokens_mistral': 1355, 'n_tokens_neox': 1289, 'n_words': 334}