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
Flutterを言い訳にしない!アプリの使い心地改善テクニック5選🔥
kno3a87
1
180
AWS IaCの注目アップデート 2024年10月版
konokenj
3
3.3k
GitHub Actionsのキャッシュと手を挙げることの大切さとそれに必要なこと
satoshi256kbyte
5
430
Duckdb-Wasmでローカルダッシュボードを作ってみた
nkforwork
0
130
型付き API リクエストを実現するいくつかの手法とその選択 / Typed API Request
euxn23
8
2.2k
LLM生成文章の精度評価自動化とプロンプトチューニングの効率化について
layerx
PRO
2
190
Remix on Hono on Cloudflare Workers
yusukebe
1
290
Macとオーディオ再生 2024/11/02
yusukeito
0
370
Tauriでネイティブアプリを作りたい
tsucchinoko
0
370
What’s New in Compose Multiplatform - A Live Tour (droidcon London 2024)
zsmb
1
470
AI時代におけるSRE、 あるいはエンジニアの生存戦略
pyama86
6
1.1k
受け取る人から提供する人になるということ
little_rubyist
0
230
Featured
See All Featured
Raft: Consensus for Rubyists
vanstee
136
6.6k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
126
18k
Designing the Hi-DPI Web
ddemaree
280
34k
Adopting Sorbet at Scale
ufuk
73
9.1k
RailsConf 2023
tenderlove
29
900
Automating Front-end Workflow
addyosmani
1366
200k
Building Better People: How to give real-time feedback that sticks.
wjessup
364
19k
Practical Orchestrator
shlominoach
186
10k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
Mobile First: as difficult as doing things right
swwweet
222
8.9k
Into the Great Unknown - MozCon
thekraken
32
1.5k
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୯७ͳߴ֊ؔ Έ୯७͚ͩͲڧྗ طʹ࠶ར༻ՄೳͳϥΠϒϥϦ͕ࢁ͋Δ ͨͩ͠ɺཚ༻͢ΔͱԿॲ͔ΒԿ͕ೖ͖͍ͬͯͯ Δͷ͔͔Βͳ͘ͳΔͷͰରࡦ͕ඞཁ