2025-03-11 23:42:37 +00:00
|
|
|
(ns com.biffweb.my-project.middleware
|
|
|
|
|
(:require [com.biffweb :as biff]
|
|
|
|
|
[muuntaja.middleware :as muuntaja]
|
2025-03-12 19:54:45 +00:00
|
|
|
[org.sqids.clojure :as sqids]
|
2025-03-11 23:42:37 +00:00
|
|
|
[ring.middleware.anti-forgery :as csrf]
|
2025-03-12 19:54:45 +00:00
|
|
|
[ring.middleware.defaults :as rd]
|
|
|
|
|
[clojure.string :as str]))
|
2025-03-11 23:42:37 +00:00
|
|
|
|
|
|
|
|
(defn wrap-redirect-signed-in [handler]
|
|
|
|
|
(fn [{:keys [session] :as ctx}]
|
|
|
|
|
(if (some? (:uid session))
|
|
|
|
|
{:status 303
|
|
|
|
|
:headers {"location" "/app"}}
|
|
|
|
|
(handler ctx))))
|
|
|
|
|
|
|
|
|
|
(defn wrap-signed-in [handler]
|
|
|
|
|
(fn [{:keys [session] :as ctx}]
|
|
|
|
|
(if (some? (:uid session))
|
|
|
|
|
(handler ctx)
|
|
|
|
|
{:status 303
|
|
|
|
|
:headers {"location" "/signin?error=not-signed-in"}})))
|
|
|
|
|
|
2025-03-12 19:54:45 +00:00
|
|
|
(defn wrap-session [handler]
|
|
|
|
|
(fn [{:keys [session] :as req}]
|
2025-03-12 21:31:44 +00:00
|
|
|
(let [req (update-in req [:path-params :code] #(when % (str/lower-case %)))]
|
2025-03-12 19:54:45 +00:00
|
|
|
(if (some? (:id session))
|
|
|
|
|
(do
|
|
|
|
|
(println "found session id" (:id session))
|
|
|
|
|
(handler req))
|
|
|
|
|
|
|
|
|
|
(let [new-id (random-uuid)]
|
|
|
|
|
(println "no session id, adding new one:" new-id)
|
|
|
|
|
(handler (assoc-in req [:session :id] new-id)))))))
|
|
|
|
|
|
2025-03-11 23:42:37 +00:00
|
|
|
;; Stick this function somewhere in your middleware stack below if you want to
|
|
|
|
|
;; inspect what things look like before/after certain middleware fns run.
|
|
|
|
|
#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
|
|
|
|
|
(defn wrap-debug [handler]
|
|
|
|
|
(fn [ctx]
|
|
|
|
|
(let [response (handler ctx)]
|
|
|
|
|
(println "REQUEST")
|
|
|
|
|
(biff/pprint ctx)
|
|
|
|
|
#_{:clj-kondo/ignore [:inline-def :clojure-lsp/unused-public-var]}
|
|
|
|
|
(def ctx* ctx)
|
|
|
|
|
(println "RESPONSE")
|
|
|
|
|
(biff/pprint response)
|
|
|
|
|
#_{:clj-kondo/ignore [:inline-def :clojure-lsp/unused-public-var]}
|
|
|
|
|
(def response* response)
|
|
|
|
|
response)))
|
|
|
|
|
|
|
|
|
|
(defn wrap-site-defaults [handler]
|
|
|
|
|
(-> handler
|
|
|
|
|
biff/wrap-render-rum
|
|
|
|
|
biff/wrap-anti-forgery-websockets
|
|
|
|
|
csrf/wrap-anti-forgery
|
|
|
|
|
biff/wrap-session
|
|
|
|
|
muuntaja/wrap-params
|
|
|
|
|
muuntaja/wrap-format
|
|
|
|
|
(rd/wrap-defaults (-> rd/site-defaults
|
|
|
|
|
(assoc-in [:security :anti-forgery] false)
|
|
|
|
|
(assoc-in [:responses :absolute-redirects] true)
|
|
|
|
|
(assoc :session false)
|
|
|
|
|
(assoc :static false)))))
|
|
|
|
|
|
|
|
|
|
(defn wrap-api-defaults [handler]
|
|
|
|
|
(-> handler
|
|
|
|
|
muuntaja/wrap-params
|
|
|
|
|
muuntaja/wrap-format
|
|
|
|
|
(rd/wrap-defaults rd/api-defaults)))
|
|
|
|
|
|
|
|
|
|
(defn wrap-base-defaults [handler]
|
|
|
|
|
(-> handler
|
|
|
|
|
biff/wrap-https-scheme
|
|
|
|
|
biff/wrap-resource
|
|
|
|
|
biff/wrap-internal-error
|
|
|
|
|
biff/wrap-ssl
|
2025-03-12 19:54:45 +00:00
|
|
|
|
|
|
|
|
;; biff/wrap-log-requests
|
|
|
|
|
))
|