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
ChatGPT とつくる PHP で OS 実装
memory1994
PRO
2
100
[JAWS-UG横浜 #76] イケてるアップデートを宇宙いち早く紹介するよ!
maroon1st
0
460
数十万行のプロジェクトを Scala 2から3に完全移行した
xuwei_k
0
280
アクターシステムに頼らずEvent Sourcingする方法について
j5ik2o
4
290
Effective Signals in Angular 19+: Rules and Helpers @ngbe2024
manfredsteyer
PRO
0
140
htmxって知っていますか?次世代のHTML
hiro_ghap1
0
340
rails statsで大解剖 🔍 “B/43流” のRailsの育て方を歴史とともに振り返ります
shoheimitani
2
940
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
150
nekko cloudにおけるProxmox VE利用事例
irumaru
3
430
命名をリントする
chiroruxx
1
410
Cloudflare MCP ServerでClaude Desktop からWeb APIを構築
kutakutat
1
550
ドメインイベント増えすぎ問題
h0r15h0
2
350
Featured
See All Featured
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.1k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
2
170
Designing for Performance
lara
604
68k
How To Stay Up To Date on Web Technology
chriscoyier
789
250k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
810
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
111
49k
It's Worth the Effort
3n
183
28k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
Thoughts on Productivity
jonyablonski
67
4.4k
Optimizing for Happiness
mojombo
376
70k
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୯७ͳߴ֊ؔ Έ୯७͚ͩͲڧྗ طʹ࠶ར༻ՄೳͳϥΠϒϥϦ͕ࢁ͋Δ ͨͩ͠ɺཚ༻͢ΔͱԿॲ͔ΒԿ͕ೖ͖͍ͬͯͯ Δͷ͔͔Βͳ͘ͳΔͷͰରࡦ͕ඞཁ