Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ring Middleware の基礎

5335315769ef28d5b1b2ddbd776cef2d?s=47 ayato
June 26, 2016

Ring Middleware の基礎

5335315769ef28d5b1b2ddbd776cef2d?s=128

ayato

June 26, 2016
Tweet

Transcript

  1. 3JOH.JEEMFXBSF ͷجૅ -JTQ.FFU6QQSFTFOUFECZ4IJCVZBMJTQ !@BZBUP@Q$ZCP[V4UBSUVQT *OD

  2. ͋΍ͽʔ !@BZBUP@Q $ZCP[V4UBSUVQT *OD +BQBO$MPKVSJBOT "ENJO

  3. 3JOHͷίϯηϓτ Handler Request Response Middleware ࠓ೔͸.JEEMFXBSFͷ͓࿩

  4. 3JOH.JEEMFXBSF

  5. 3JOH.JEEMFXBSF (defn wrap-member [handler] (fn [req] (->> (get-in req [:params

    :member-id]) find-member (assoc req :member) handler))) ߴ֊ؔ਺ ϋϯυϥΛड͚औΔ ϦΫΤετΛऔΔؔ਺Λฦ͢ ৽͍͠ϋϯυϥ
  6. (defn wrap-member [handler] (fn [req] (->> (get-in req [:params :member-id])

    find-member (assoc req :member) handler))) 3JOH.JEEMFXBSF ߴ֊ؔ਺ ϋϯυϥΛड͚औΔ ϦΫΤετΛऔΔؔ਺Λฦ͢ ৽͍͠ϋϯυϥ ϋϯυϥ ϦΫΤετ ؔ਺
  7. .JEEMFXBSFΛద༻͢Δ (defn wrap-answer [handler] (fn [req] (handler (assoc req :ans

    42)))) (defn handler [req] (res/response (get req :ans))) XSBQBOTXFSϛυϧ΢ΣΞΛIBOEMFS΁ద༻͢Δ (def app (wrap-answer handler))
  8. .JEEMFXBSFΛద༻͢Δ (let [handler (fn [req] (res/response (get req :ans))] (fn

    [req] (handler (assoc req :ans 42)))) XSBQBOTXFSϛυϧ΢ΣΞΛIBOEMFS΁ద༻͢Δ (fn [req] (res/response (get (assoc req :ans 42) :ans)))
  9. ϦΫΤετͷྲྀΕ Handler Middleware A Middleware B Middleware C Request Response

  10. ϦΫΤετͷྲྀΕ Handler Middleware A Middleware B Middleware C Request Response

    .JEEMFXBSF$͕ϦΫΤετϚοϓΛॲཧ͠ ৽ͨͳϦΫΤετϚοϓΛ࣍ͷϋϯυϥ΁౉͢
  11. ϦΫΤετͷྲྀΕ Handler Middleware A Middleware B Middleware C Request Response

    .JEEMFXBSF#͕ϦΫΤετϚοϓΛॲཧ͠ ৽ͨͳϦΫΤετϚοϓΛ࣍ͷϋϯυϥ΁౉͢
  12. ϦΫΤετͷྲྀΕ Handler Middleware A Middleware B Middleware C Request Response

    .JEEMFXBSF"͕ϦΫΤετϚοϓΛॲཧ͠ ৽ͨͳϦΫΤετϚοϓΛ࣍ͷϋϯυϥ΁౉͢
  13. ϦΫΤετͷྲྀΕ Handler Middleware A Middleware B Middleware C Request Response

    )BOEMFS͕ϦΫΤετϚοϓΛॲཧ͠ ϨεϙϯεϚοϓΛੜ੒ฦ٫͢Δ
  14. ϦΫΤετͷྲྀΕ Handler Middleware A Middleware B Middleware C Request Response

    .JEEMFXBSF"͕ϨεϙϯεϚοϓΛॲཧ͠ ৽ͨͳϨεϙϯεϚοϓΛฦ٫͢Δ
  15. ϦΫΤετͷྲྀΕ Handler Middleware A Middleware B Middleware C Request Response

    .JEEMFXBSF#͕ϨεϙϯεϚοϓΛॲཧ͠ ৽ͨͳϨεϙϯεϚοϓΛฦ٫͢Δ
  16. ϦΫΤετͷྲྀΕ Handler Middleware A Middleware B Middleware C Request Response

    .JEEMFXBSF$͕ϨεϙϯεϚοϓΛॲཧ͠ ৽ͨͳϨεϙϯεϚοϓΛฦ٫͢Δ
  17. ϛυϧ΢ΣΞ͸ͲͷΑ͏ʹಇ͔͘ ઌʹద༻ͨ͠ϛυϧ΢ΣΞ͕಺ଆʹͳΔ ֎ଆͷϛυϧ΢ΣΞ͕ઌʹϦΫΤετϚοϓΛ
 ॲཧͰ͖Δ ಺ଆͷϛυϧ΢ΣΞ͕ઌʹϨεϙϯεϚοϓΛ
 ॲཧͰ͖Δ

  18. 3JOH.JEEMFXBSF ࣮૷ύλʔϯ

  19. ࣮૷ύλʔϯ ϦΫΤετϚοϓߋ৽ύλʔϯ ϨεϙϯεϚοϓߋ৽ύλʔϯ ৚݅෼ذύλʔϯ Ϋϩʔδϟ ดแ ύλʔϯ

  20. ϦΫΤετϚοϓߋ৽ύλʔϯ (defn wrap-member [handler] (fn [req] (->> (get-in req [:params

    :member-id]) find-member (assoc req :member) handler))) ϦΫΤετϚοϓΛߋ৽ͯ͠ϋϯυϥ΁౉͢ Ұൠతʹϋϯυϥ΁౉͢લͷڞ௨ॲཧΛ୲͏ ϦΫΤετϚοϓʹ௥Ճ
  21. (defn wrap-html [handler] (fn [req] (-> (handler req) (res/content-type "text/html"))))

    ϨεϙϯεϚοϓߋ৽ύλʔϯ ϋϯυϥͷฦ٫ͨ͠ϨεϙϯεϚοϓΛߋ৽͢Δ Ϩεϙϯεͷ.*.&λΠϓΛ
 ౷Ұͨ͠Γ͢Δͷʹศར ϨεϙϯεϚοϓʹ௥Ճ
  22. (defn wrap-auth [handler] (fn [req] (if (authenticated? req) (handler req)

    (auth-error-handler req)))) ৚݅෼ذύλʔϯ ࣮ߦ͢ΔϋϯυϥΛ৚݅ʹΑͬͯมߋ͢Δ ϧʔςΟϯά΍ೝূೝՄͷॲཧΛ͢Δ
 ڞ௨ॲཧͰར༻͞ΕΔ ৚݅Ͱ࣮ߦର৅Λมߋ
  23. (defn wrap-fib [handler] (let [memoized-fib (memoize fib)] (fn [req] (let

    [n (->long (get-in req [:params :n]))] (-> (assoc req :fib-num (memoized-fib n)) handler))))) Ϋϩʔδϟ ดแ ύλʔϯ ؀ڥΛด͡ࠐΊͯ৽͍͠ϋϯυϥΛฦ٫͢Δ ॏ͍ॲཧͳͲΛϝϞԽͨ͠ΓɺԿ౓΋ܭࢉ͢Δҙຯ ͕ͳ͍΋ͷΛด͡ࠐΊͯ͠·͑Δ ฦ٫͢Δؔ਺ʹด͡ࠐΊΔ
  24. ى͜Γ͕ͪͳ໰୊

  25. ໰୊ (defn show-user [req] (let [user-id (get-in req [:params :user-id])

    db (:db req) (-> (find-user-by-id db user-id) res/ok
 res/html)))
  26. ໰୊ڊେͳϦΫΤετϚοϓ ϛυϧ΢ΣΞΛཔΓ͗ͯ͢ؾ͕෇͍ͨΒ
 ϦΫΤετϚοϓ͕େ͖͘ͳΓ͍͗ͯ͢Δ (defn show-user [req] (let [user-id (get-in req

    [:params :user-id]) db (:db req) (-> (find-user-by-id db user-id) res/ok
 res/html)))
  27. ໰୊શͯݟ͑ͯ͠·͏ ϛυϧ΢ΣΞͰϦΫΤετϚοϓʹӨڹΛ༩͑ Ε͹શͯͷϋϯυϥ͔Βத਎͕ݟ͑ͯ͠·͏ (defn show-user [req] (let [user-id (get-in req

    [:params :user-id]) db (:db req) (-> (find-user-by-id db user-id) res/ok
 res/html)))
  28. ໰୊ (def app (-> handler wrap-keyword-params wrap-nested-params wrap-multipart-params wrap-params))

  29. ໰୊ӅΕͨґଘؔ܎ खಈͰॱ൪Λܾఆ͠ͳ͚Ε͹ͳΒͳ͍ ґଘؔ܎ͷଘࡏ͕໨ʹݟ͑ͳ͍ (def app (-> handler wrap-keyword-params wrap-nested-params wrap-multipart-params

    wrap-params)) ґଘؔ܎͕ੜ·Ε͍ͯΔ
  30. ·ͱΊ

  31. ·ͱΊ 3JOH.JEEMFXBSF͸୯७ͳߴ֊ؔ਺ ࢓૊Έ͸୯७͚ͩͲڧྗ طʹ࠶ར༻ՄೳͳϥΠϒϥϦ͕୔ࢁ͋Δ ͨͩ͠ɺཚ༻͢ΔͱԿॲ͔ΒԿ͕ೖ͖͍ͬͯͯ Δͷ͔෼͔Βͳ͘ͳΔͷͰରࡦ͕ඞཁ