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

Ring Middleware の基礎

ayato
June 26, 2016

Ring Middleware の基礎

ayato

June 26, 2016
Tweet

More Decks by ayato

Other Decks in Programming

Transcript

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  4. 3JOH.JEEMFXBSF

    View full-size slide

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

    View full-size slide

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

    ϋϯυϥ
    ϦΫΤετ
    ؔ਺

    View full-size slide

  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))

    View full-size slide

  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)))

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  17. ϛυϧ΢ΣΞ͸ͲͷΑ͏ʹಇ͔͘
    ઌʹద༻ͨ͠ϛυϧ΢ΣΞ͕಺ଆʹͳΔ
    ֎ଆͷϛυϧ΢ΣΞ͕ઌʹϦΫΤετϚοϓΛ

    ॲཧͰ͖Δ
    ಺ଆͷϛυϧ΢ΣΞ͕ઌʹϨεϙϯεϚοϓΛ

    ॲཧͰ͖Δ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  20. ϦΫΤετϚοϓߋ৽ύλʔϯ
    (defn wrap-member [handler]
    (fn [req]
    (->> (get-in req [:params :member-id])
    find-member
    (assoc req :member)
    handler)))
    ϦΫΤετϚοϓΛߋ৽ͯ͠ϋϯυϥ΁౉͢
    Ұൠతʹϋϯυϥ΁౉͢લͷڞ௨ॲཧΛ୲͏
    ϦΫΤετϚοϓʹ௥Ճ

    View full-size slide

  21. (defn wrap-html [handler]
    (fn [req]
    (-> (handler req)
    (res/content-type "text/html"))))
    ϨεϙϯεϚοϓߋ৽ύλʔϯ
    ϋϯυϥͷฦ٫ͨ͠ϨεϙϯεϚοϓΛߋ৽͢Δ
    Ϩεϙϯεͷ.*.&λΠϓΛ

    ౷Ұͨ͠Γ͢Δͷʹศར
    ϨεϙϯεϚοϓʹ௥Ճ

    View full-size slide

  22. (defn wrap-auth [handler]
    (fn [req]
    (if (authenticated? req)
    (handler req)
    (auth-error-handler req))))
    ৚݅෼ذύλʔϯ
    ࣮ߦ͢ΔϋϯυϥΛ৚݅ʹΑͬͯมߋ͢Δ
    ϧʔςΟϯά΍ೝূೝՄͷॲཧΛ͢Δ

    ڞ௨ॲཧͰར༻͞ΕΔ
    ৚݅Ͱ࣮ߦର৅Λมߋ

    View full-size slide

  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)))))
    Ϋϩʔδϟ ดแ
    ύλʔϯ
    ؀ڥΛด͡ࠐΊͯ৽͍͠ϋϯυϥΛฦ٫͢Δ
    ॏ͍ॲཧͳͲΛϝϞԽͨ͠ΓɺԿ౓΋ܭࢉ͢Δҙຯ
    ͕ͳ͍΋ͷΛด͡ࠐΊͯ͠·͑Δ
    ฦ٫͢Δؔ਺ʹด͡ࠐΊΔ

    View full-size slide

  24. ى͜Γ͕ͪͳ໰୊

    View full-size slide

  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)))

    View full-size slide

  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)))

    View full-size slide

  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)))

    View full-size slide

  28. ໰୊
    (def app
    (-> handler
    wrap-keyword-params
    wrap-nested-params
    wrap-multipart-params
    wrap-params))

    View full-size slide

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

    View full-size slide

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

    View full-size slide