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
510
Muscle Assert
ayato0211
0
230
Clojureを用いたWebアプリケーション開発
ayato0211
2
3k
翻訳にまつわるエトセトラ
ayato0211
6
1.2k
Other Decks in Programming
See All in Programming
テストコード書いてみませんか?
onopon
2
340
React 19でお手軽にCSS-in-JSを自作する
yukukotani
5
560
ドメインイベント増えすぎ問題
h0r15h0
2
570
Amazon Nova Reelの可能性
hideg
0
200
良いユニットテストを書こう
mototakatsu
11
3.6k
どうして手を動かすよりもチーム内のコードレビューを優先するべきなのか
okashoi
3
870
rails newと同時に型を書く
aki19035vc
5
710
Итераторы в Go 1.23: зачем они нужны, как использовать, и насколько они быстрые?
lamodatech
0
1.4k
return文におけるstd::moveについて
onihusube
1
1.4k
混沌とした例外処理とエラー監視に秩序をもたらす
morihirok
13
2.3k
PSR-15 はあなたのための ものではない? - phpcon2024
myamagishi
0
400
watsonx.ai Dojo #6 継続的なAIアプリ開発と展開
oniak3ibm
PRO
0
170
Featured
See All Featured
Thoughts on Productivity
jonyablonski
68
4.4k
Automating Front-end Workflow
addyosmani
1366
200k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
BBQ
matthewcrist
85
9.4k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
132
33k
A better future with KSS
kneath
238
17k
Facilitating Awesome Meetings
lara
51
6.2k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
How to Think Like a Performance Engineer
csswizardry
22
1.3k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
Product Roadmaps are Hard
iamctodd
PRO
50
11k
We Have a Design System, Now What?
morganepeng
51
7.3k
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୯७ͳߴ֊ؔ Έ୯७͚ͩͲڧྗ طʹ࠶ར༻ՄೳͳϥΠϒϥϦ͕ࢁ͋Δ ͨͩ͠ɺཚ༻͢ΔͱԿॲ͔ΒԿ͕ೖ͖͍ͬͯͯ Δͷ͔͔Βͳ͘ͳΔͷͰରࡦ͕ඞཁ