التعمق في Ring Middleware
تعلّم كيفية تغليف Ring للمعالجات باستخدام Middleware لإضافة سلوكيات مشتركة مثل التسجيل وتحليل المعاملات وإدارة الجلسات في تطبيقات الويب Compojure.
التعمق في Ring Middleware درس مجاني في Clojure Functional Programming & JVM Backend Development على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Clojure Functional Programming & JVM Backend Development، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Clojure Functional Programming & JVM Backend Development 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
What Is Middleware?
In Ring, a handler is a function that takes a request map and returns a response map. Middleware is a higher-order function that takes a handler and returns a new handler.
This lets you wrap behavior around your core logic without modifying it.
(defn wrap-example [handler]
(fn [request]
(handler request)))The Wrapping Pattern
Every middleware follows the same shape: receive a handler, return a function that takes a request. You can act before or after calling the inner handler.
- Before: inspect or modify the request
- After: inspect or modify the response
(defn wrap-logging [handler]
(fn [request]
(println "Incoming:" (:uri request))
(let [response (handler request)]
(println "Status:" (:status response))
response)))Applying Middleware
You apply middleware by calling it on your handler. The result is a new handler that you pass to the server.
(def app
(-> handler
wrap-logging))The Threading Macro
The -> threading macro is the idiomatic way to stack middleware. Each layer wraps the one before it.
Order matters: the last middleware listed is the outermost layer that sees the request first.
(def app
(-> routes
wrap-params
wrap-session
wrap-logging))Built-in Middleware
The ring-defaults library bundles common middleware so you do not write it by hand.
- wrap-params parses query and form params
- wrap-session manages sessions
- wrap-keyword-params turns string keys into keywords
(require '[ring.middleware.defaults :refer [wrap-defaults site-defaults]])
(def app
(wrap-defaults routes site-defaults))Modifying the Request
To add data for downstream handlers, assoc a key onto the request before calling the inner handler.
(defn wrap-user [handler]
(fn [request]
(let [user (lookup-user (:session request))]
(handler (assoc request :current-user user)))))Modifying the Response
To set headers or transform output, work on the response after calling the inner handler.
(defn wrap-content-type [handler]
(fn [request]
(let [response (handler request)]
(assoc-in response [:headers "Content-Type"] "text/html"))))Conditional Middleware
Sometimes you only want middleware on certain routes. You can apply it selectively rather than globally.
(defroutes app
(GET "/public" [] public-handler)
(-> (GET "/admin" [] admin-handler)
wrap-authentication))Exception Handling Middleware
A common pattern is wrapping all handlers in a try/catch to return a clean 500 response instead of leaking stack traces.
(defn wrap-errors [handler]
(fn [request]
(try
(handler request)
(catch Exception e
{:status 500 :body "Internal Server Error"}))))Order of Execution
Think of middleware as an onion. The request travels inward through each layer to the handler, then the response travels outward back through them.
- Outermost middleware runs first on request, last on response
- Innermost runs last on request, first on response
; request: wrap-logging -> wrap-session -> wrap-params -> handler
; response: handler -> wrap-params -> wrap-session -> wrap-loggingComposing Reusable Middleware
Because middleware are just functions, you can group them into a single composed function and reuse it across multiple apps.
(defn wrap-common [handler]
(-> handler
wrap-logging
wrap-errors
wrap-params))Quick Check
Test your understanding of middleware ordering.
Recap
You learned that middleware are higher-order functions wrapping handlers to add cross-cutting behavior.
- They can modify the request before, or the response after
- The
->macro stacks them like an onion - Libraries like
ring-defaultsbundle common ones
Next you can combine custom middleware with Compojure routes for full-featured apps.
الأسئلة الشائعة
هل درس «التعمق في Ring Middleware» مجاني؟
نعم — نص درس «التعمق في Ring Middleware» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Clojure Functional Programming & JVM Backend Development، انتقل إلى CoddyKit PRO. تتضمن دورة Clojure Functional Programming & JVM Backend Development 4 دروس في المجموع.
ماذا ستتعلم في «التعمق في Ring Middleware»؟
تعلّم كيفية تغليف Ring للمعالجات باستخدام Middleware لإضافة سلوكيات مشتركة مثل التسجيل وتحليل المعاملات وإدارة الجلسات في تطبيقات الويب Compojure. تتمرن على Clojure Functional Programming & JVM Backend Development مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Clojure Functional Programming & JVM Backend Development؟
لا تُشترط خبرة سابقة. Clojure Functional Programming & JVM Backend Development على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «التعمق في Ring Middleware»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Clojure Functional Programming & JVM Backend Development هذا؟
نعم. كل درس في Clojure Functional Programming & JVM Backend Development يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- مقدمة إلى Ring وأساسيات HTTP
- التوجيه باستخدام Compojure
- معالجة الطلبات والاستجابات
- التعمق في Ring Middleware