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
550
About Integrant
ayato0211
0
490
Muscle Assert
ayato0211
0
210
Clojureを用いたWebアプリケーション開発
ayato0211
2
3k
翻訳にまつわるエトセトラ
ayato0211
6
1.2k
Other Decks in Programming
See All in Programming
ペアーズにおけるAmazon Bedrockを⽤いた障害対応⽀援 ⽣成AIツールの導⼊事例 @ 20241115配信AWSウェビナー登壇
fukubaka0825
6
2k
Jakarta EE meets AI
ivargrimstad
0
240
聞き手から登壇者へ: RubyKaigi2024 LTでの初挑戦が 教えてくれた、可能性の星
mikik0
1
130
Better Code Design in PHP
afilina
PRO
0
130
ActiveSupport::Notifications supporting instrumentation of Rails apps with OpenTelemetry
ymtdzzz
1
250
Flutterを言い訳にしない!アプリの使い心地改善テクニック5選🔥
kno3a87
1
200
Duckdb-Wasmでローカルダッシュボードを作ってみた
nkforwork
0
130
EMになってからチームの成果を最大化するために取り組んだこと/ Maximize team performance as EM
nashiusagi
0
100
距離関数を極める! / SESSIONS 2024
gam0022
0
290
Jakarta EE meets AI
ivargrimstad
0
620
みんなでプロポーザルを書いてみた
yuriko1211
0
280
.NET のための通信フレームワーク MagicOnion 入門 / Introduction to MagicOnion
mayuki
1
1.7k
Featured
See All Featured
What's in a price? How to price your products and services
michaelherold
243
12k
How to Think Like a Performance Engineer
csswizardry
20
1.1k
Documentation Writing (for coders)
carmenintech
65
4.4k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
25
1.8k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
370
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
Keith and Marios Guide to Fast Websites
keithpitt
409
22k
Navigating Team Friction
lara
183
14k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
250
21k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
109
49k
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୯७ͳߴ֊ؔ Έ୯७͚ͩͲڧྗ طʹ࠶ར༻ՄೳͳϥΠϒϥϦ͕ࢁ͋Δ ͨͩ͠ɺཚ༻͢ΔͱԿॲ͔ΒԿ͕ೖ͖͍ͬͯͯ Δͷ͔͔Βͳ͘ͳΔͷͰରࡦ͕ඞཁ