<commit_before>(ns {{namespace}}.components.handler
  (:require [compojure.core :refer [defroutes routes wrap-routes]]
            [compojure.handler :refer [site]]
            [ring.middleware.defaults :refer [site-defaults wrap-defaults]]
            [ring.middleware.params :refer [wrap-params]]
            [ring.middleware.keyword-params :refer [wrap-keyword-params]]
            [ring.middleware.anti-forgery :refer [wrap-anti-forgery]]
            [ring.middleware.reload :refer [wrap-reload]]
            [ring.middleware.flash :refer [wrap-flash]]
            [compojure.route :as route]
            [com.stuartsierra.component :as comp]
            [taoensso.tempura :as tr]
            [buddy.auth.middleware :refer [wrap-authorization]]
            [buddy.auth.accessrules :refer [wrap-access-rules]]
            [{{namespace}}.service.auth :as auth]
            [{{namespace}}.routes.home :refer [home-routes]]
            [{{namespace}}.routes.user :refer [user-routes]]))

(defroutes base-routes
           (route/resources "/")
           (route/not-found "Not Found"))

(def locale-dict
  {:de {:__load-resource "i18n/de.edn"}
   :en {:__load-resource "i18n/en.edn"}})


(defn add-locale [handler]
  (fn [req]
    (let [accept-language (get-in req [:headers "accept-language"])
          short-languages (or (tr/parse-http-accept-header accept-language) ["en"])]
      (handler (assoc req :localize (partial tr/tr
                                             {:default-locale :en
                                              :dict           locale-dict}
                                             short-languages))))))


(defn wrap-base [route dev?]
  (let [handler (-> (route)
                    (wrap-access-rules {:rules auth/rules})
                    (wrap-authorization auth/auth-backend)
                    add-locale
                    (wrap-defaults site-defaults))]
    (if dev? (wrap-reload handler) handler)))

(defn get-handler [config {:keys [db]}]
  (let [dev? (= (:env config "") :dev)]
    (routes
      (wrap-base home-routes dev?)
      (wrap-base (partial user-routes config db) dev?)))


  #_(routes (-> (#'home-routes)
                (wrap-routes wrap-reload)                   ;TODO only when dev env
                (wrap-defaults site-defaults)
                (wrap-routes add-locale))
            base-routes))
;(routes (-> (#'home-routes)
;            (wrap-routes wrap-reload) ;TODO only when dev env
;            (wrap-defaults site-defaults)
;            (wrap-routes add-locale))
;        base-routes))
<commit_msg>fixed wrapping of middlewares added example for applying different middlewares to different routes<commit_after>(ns {{namespace}}.components.handler
  (:require [compojure.core :refer [defroutes routes wrap-routes]]
            [compojure.handler :refer [site]]
            [ring.middleware.defaults :refer [site-defaults wrap-defaults]]
            [ring.middleware.params :refer [wrap-params]]
            [ring.middleware.keyword-params :refer [wrap-keyword-params]]
            [ring.middleware.anti-forgery :refer [wrap-anti-forgery]]
            [ring.middleware.reload :refer [wrap-reload]]
            [ring.middleware.flash :refer [wrap-flash]]
            [compojure.route :as route]
            [com.stuartsierra.component :as comp]
            [taoensso.tempura :as tr]
            [buddy.auth.middleware :refer [wrap-authorization]]
            [buddy.auth.accessrules :refer [wrap-access-rules]]
            [{{namespace}}.service.auth :as auth]
            [{{namespace}}.routes.home :refer [home-routes]]
            [{{namespace}}.routes.user :refer [user-routes]]))

(defroutes base-routes
           (route/resources "/")
           (route/not-found "Not Found"))

(def locale-dict
  {:de {:__load-resource "i18n/de.edn"}
   :en {:__load-resource "i18n/en.edn"}})


(defn add-locale [handler]
  (fn [req]
    (let [accept-language (get-in req [:headers "accept-language"])
          short-languages (or (tr/parse-http-accept-header accept-language) ["en"])]
      (handler (assoc req :localize (partial tr/tr
                                             {:default-locale :en
                                              :dict           locale-dict}
                                             short-languages))))))

;(defn wrap-api [route db dev?]
;  (let [handler (-> route
;                    (wrap-json-response)
;                    add-locale
;                    (wrap-custom-authorization db)
;                    (wrap-json-body {:keywords? true :bigdecimals? true}))]
;    (if dev? (wrap-reload handler) handler)))

(defn wrap-base [route dev?]
  (let [handler (-> route
                    (wrap-access-rules {:rules auth/rules})
                    (wrap-authorization auth/auth-backend)
                    add-locale
                    (wrap-defaults site-defaults))]
    (if dev? (wrap-reload handler) handler)))

(defn get-handler [config {:keys [db]}]
  (let [dev? (= (:env config "") :dev)]
    (routes
      ;(wrap-api (api-routes db) db dev?)
      (wrap-base
        (routes (home-routes) (user-routes config db))
        dev?)))



  #_(routes (-> (#'home-routes)
                (wrap-routes wrap-reload)                   ;TODO only when dev env
                (wrap-defaults site-defaults)
                (wrap-routes add-locale))
            base-routes))
;(routes (-> (#'home-routes)
;            (wrap-routes wrap-reload) ;TODO only when dev env
;            (wrap-defaults site-defaults)
;            (wrap-routes add-locale))
;        base-routes))

{'fraction_non_alphanumeric': 0.14170403587443947, 'fraction_numerical': 0.0014349775784753362, 'mean_word_length': 1.8816537467700258, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3222884', 'n_tokens_mistral': 1633, 'n_tokens_neox': 1630, 'n_words': 363}