Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Ring Middleware の基礎
Search
ayato
June 26, 2016
Programming
2
250
Ring Middleware の基礎
ayato
June 26, 2016
Tweet
Share
More Decks by ayato
See All by ayato
Clojureという言語が私逹にもたらしたもの
ayato0211
6
2.9k
3年間考え続けてきたWebアプリケーションにおけるテストの話
ayato0211
3
230
Re:REPL-Driven Development
ayato0211
3
1.2k
Meta Template Engine
ayato0211
2
1k
超変換! Hiccup data structure!!
ayato0211
2
560
About Integrant
ayato0211
0
500
Muscle Assert
ayato0211
0
220
Clojureを用いたWebアプリケーション開発
ayato0211
2
3k
翻訳にまつわるエトセトラ
ayato0211
6
1.2k
Other Decks in Programming
See All in Programming
testcontainers のススメ
sgash708
1
120
The Efficiency Paradox and How to Save Yourself and the World
hollycummins
1
440
テストケースの名前はどうつけるべきか?
orgachem
PRO
0
130
暇に任せてProxmoxコンソール 作ってみました
karugamo
2
720
快速入門可觀測性
blueswen
0
350
Jakarta EE meets AI
ivargrimstad
0
240
Stackless и stackful? Корутины и асинхронность в Go
lamodatech
0
730
HTTP compression in PHP and Symfony apps
dunglas
2
1.7k
rails statsで大解剖 🔍 “B/43流” のRailsの育て方を歴史とともに振り返ります
shoheimitani
2
930
MCP with Cloudflare Workers
yusukebe
2
220
見えないメモリを観測する: PHP 8.4 `pg_result_memory_size()` とSQL結果のメモリ管理
kentaroutakeda
0
330
【re:Growth 2024】 Aurora DSQL をちゃんと話します!
maroon1st
0
770
Featured
See All Featured
4 Signs Your Business is Dying
shpigford
181
21k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
A Modern Web Designer's Workflow
chriscoyier
693
190k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
29
2k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
8.3k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
Producing Creativity
orderedlist
PRO
341
39k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
169
50k
Site-Speed That Sticks
csswizardry
2
190
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Transcript
3JOH.JEEMFXBSF ͷجૅ -JTQ.FFU6QQSFTFOUFECZ4IJCVZBMJTQ !@BZBUP@Q$ZCP[V4UBSUVQT *OD
͋ͽʔ !@BZBUP@Q $ZCP[V4UBSUVQT *OD +BQBO$MPKVSJBOT "ENJO
3JOHͷίϯηϓτ Handler Request Response Middleware ࠓ.JEEMFXBSFͷ͓
3JOH.JEEMFXBSF
3JOH.JEEMFXBSF (defn wrap-member [handler] (fn [req] (->> (get-in req [:params
:member-id]) find-member (assoc req :member) handler))) ߴ֊ؔ ϋϯυϥΛड͚औΔ ϦΫΤετΛऔΔؔΛฦ͢ ৽͍͠ϋϯυϥ
(defn wrap-member [handler] (fn [req] (->> (get-in req [:params :member-id])
find-member (assoc req :member) handler))) 3JOH.JEEMFXBSF ߴ֊ؔ ϋϯυϥΛड͚औΔ ϦΫΤετΛऔΔؔΛฦ͢ ৽͍͠ϋϯυϥ ϋϯυϥ ϦΫΤετ ؔ
.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))
.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)))
ϦΫΤετͷྲྀΕ Handler Middleware A Middleware B Middleware C Request Response
ϦΫΤετͷྲྀΕ Handler Middleware A Middleware B Middleware C Request Response
.JEEMFXBSF$͕ϦΫΤετϚοϓΛॲཧ͠ ৽ͨͳϦΫΤετϚοϓΛ࣍ͷϋϯυϥ͢
ϦΫΤετͷྲྀΕ Handler Middleware A Middleware B Middleware C Request Response
.JEEMFXBSF#͕ϦΫΤετϚοϓΛॲཧ͠ ৽ͨͳϦΫΤετϚοϓΛ࣍ͷϋϯυϥ͢
ϦΫΤετͷྲྀΕ Handler Middleware A Middleware B Middleware C Request Response
.JEEMFXBSF"͕ϦΫΤετϚοϓΛॲཧ͠ ৽ͨͳϦΫΤετϚοϓΛ࣍ͷϋϯυϥ͢
ϦΫΤετͷྲྀΕ Handler Middleware A Middleware B Middleware C Request Response
)BOEMFS͕ϦΫΤετϚοϓΛॲཧ͠ ϨεϙϯεϚοϓΛੜฦ٫͢Δ
ϦΫΤετͷྲྀΕ Handler Middleware A Middleware B Middleware C Request Response
.JEEMFXBSF"͕ϨεϙϯεϚοϓΛॲཧ͠ ৽ͨͳϨεϙϯεϚοϓΛฦ٫͢Δ
ϦΫΤετͷྲྀΕ Handler Middleware A Middleware B Middleware C Request Response
.JEEMFXBSF#͕ϨεϙϯεϚοϓΛॲཧ͠ ৽ͨͳϨεϙϯεϚοϓΛฦ٫͢Δ
ϦΫΤετͷྲྀΕ Handler Middleware A Middleware B Middleware C Request Response
.JEEMFXBSF$͕ϨεϙϯεϚοϓΛॲཧ͠ ৽ͨͳϨεϙϯεϚοϓΛฦ٫͢Δ
ϛυϧΣΞͲͷΑ͏ʹಇ͔͘ ઌʹద༻ͨ͠ϛυϧΣΞ͕ଆʹͳΔ ֎ଆͷϛυϧΣΞ͕ઌʹϦΫΤετϚοϓΛ ॲཧͰ͖Δ ଆͷϛυϧΣΞ͕ઌʹϨεϙϯεϚοϓΛ ॲཧͰ͖Δ
3JOH.JEEMFXBSF ࣮ύλʔϯ
࣮ύλʔϯ ϦΫΤετϚοϓߋ৽ύλʔϯ ϨεϙϯεϚοϓߋ৽ύλʔϯ ݅ذύλʔϯ Ϋϩʔδϟ ดแ ύλʔϯ
ϦΫΤετϚοϓߋ৽ύλʔϯ (defn wrap-member [handler] (fn [req] (->> (get-in req [:params
:member-id]) find-member (assoc req :member) handler))) ϦΫΤετϚοϓΛߋ৽ͯ͠ϋϯυϥ͢ Ұൠతʹϋϯυϥ͢લͷڞ௨ॲཧΛ୲͏ ϦΫΤετϚοϓʹՃ
(defn wrap-html [handler] (fn [req] (-> (handler req) (res/content-type "text/html"))))
ϨεϙϯεϚοϓߋ৽ύλʔϯ ϋϯυϥͷฦ٫ͨ͠ϨεϙϯεϚοϓΛߋ৽͢Δ Ϩεϙϯεͷ.*.&λΠϓΛ ౷Ұͨ͠Γ͢Δͷʹศར ϨεϙϯεϚοϓʹՃ
(defn wrap-auth [handler] (fn [req] (if (authenticated? req) (handler req)
(auth-error-handler req)))) ݅ذύλʔϯ ࣮ߦ͢ΔϋϯυϥΛ݅ʹΑͬͯมߋ͢Δ ϧʔςΟϯάೝূೝՄͷॲཧΛ͢Δ ڞ௨ॲཧͰར༻͞ΕΔ ݅Ͱ࣮ߦରΛมߋ
(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))))) Ϋϩʔδϟ ดแ ύλʔϯ ڥΛด͡ࠐΊͯ৽͍͠ϋϯυϥΛฦ٫͢Δ ॏ͍ॲཧͳͲΛϝϞԽͨ͠ΓɺԿܭࢉ͢Δҙຯ ͕ͳ͍ͷΛด͡ࠐΊͯ͠·͑Δ ฦ٫͢Δؔʹด͡ࠐΊΔ
ى͜Γ͕ͪͳ
(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)))
ڊେͳϦΫΤετϚοϓ ϛυϧΣΞΛཔΓ͗ͯ͢ؾ͕͍ͨΒ ϦΫΤετϚοϓ͕େ͖͘ͳΓ͍͗ͯ͢Δ (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)))
શͯݟ͑ͯ͠·͏ ϛυϧΣΞͰϦΫΤετϚοϓʹӨڹΛ༩͑ Εશͯͷϋϯυϥ͔Βத͕ݟ͑ͯ͠·͏ (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)))
(def app (-> handler wrap-keyword-params wrap-nested-params wrap-multipart-params wrap-params))
ӅΕͨґଘؔ खಈͰॱ൪Λܾఆ͠ͳ͚ΕͳΒͳ͍ ґଘؔͷଘࡏ͕ʹݟ͑ͳ͍ (def app (-> handler wrap-keyword-params wrap-nested-params wrap-multipart-params
wrap-params)) ґଘ͕ؔੜ·Ε͍ͯΔ
·ͱΊ
·ͱΊ 3JOH.JEEMFXBSF୯७ͳߴ֊ؔ Έ୯७͚ͩͲڧྗ طʹ࠶ར༻ՄೳͳϥΠϒϥϦ͕ࢁ͋Δ ͨͩ͠ɺཚ༻͢ΔͱԿॲ͔ΒԿ͕ೖ͖͍ͬͯͯ Δͷ͔͔Βͳ͘ͳΔͷͰରࡦ͕ඞཁ