<p>The <ahref="dynamic_extensions.html">dynamic extensions</a> is a easy way to extend the system. To enable fast lookups into route data, we can compile them into any shape (records, functions etc.) we want, enabling fast access at request-time.</p>
<p>Still, we can do much better. As we know the exact route that middleware/interceptor is linked to, we can pass the (compiled) route information into the middleware/interceptor at creation-time. It can do local reasoning: extract and transform relevant data just for it and pass it into the actual request-handler via a closure - yielding much faster runtime processing. It can also decide not to mount itself by returning <code>nil</code>. Why mount a <code>wrap-enforce-roles</code> middleware for a route if there are no roles required for it?</p>
<p>To enable this we use <ahref="data_driven_middleware.html">middleware records</a><code>:gen-wrap</code> key instead of the normal <code>:wrap</code>. <code>:gen-wrap</code> expects a function of <code>route-meta router-opts => ?wrap</code>.</p>
<p>To demonstrate the two approaches, below are response coercion middleware written as normal ring middleware function and as middleware record with <code>:gen-wrap</code>. Actual codes can be found in <ahref="https://github.com/metosin/reitit/blob/master/src/reitit/ring/coercion.cljc"target="_blank"><code>reitit.ring.coercion</code></a>:</p>
match (<spanclass="hljs-name">ring/get-match</span> request)
responses (<spanclass="hljs-name"><spanclass="hljs-builtin-name">-></span></span> match <spanclass="hljs-symbol">:result</span> method <spanclass="hljs-symbol">:meta</span><spanclass="hljs-symbol">:responses</span>)
coercion (<spanclass="hljs-name"><spanclass="hljs-builtin-name">-></span></span> match <spanclass="hljs-symbol">:meta</span><spanclass="hljs-symbol">:coercion</span>)
opts (<spanclass="hljs-name"><spanclass="hljs-builtin-name">-></span></span> match <spanclass="hljs-symbol">:meta</span><spanclass="hljs-symbol">:opts</span>)]
match (<spanclass="hljs-name">ring/get-match</span> request)
responses (<spanclass="hljs-name"><spanclass="hljs-builtin-name">-></span></span> match <spanclass="hljs-symbol">:result</span> method <spanclass="hljs-symbol">:meta</span><spanclass="hljs-symbol">:responses</span>)
coercion (<spanclass="hljs-name"><spanclass="hljs-builtin-name">-></span></span> match <spanclass="hljs-symbol">:meta</span><spanclass="hljs-symbol">:coercion</span>)
opts (<spanclass="hljs-name"><spanclass="hljs-builtin-name">-></span></span> match <spanclass="hljs-symbol">:meta</span><spanclass="hljs-symbol">:opts</span>)]
gitbook.page.hasChanged({"page":{"title":"Compiling middleware","level":"1.4.5","depth":2,"next":{"title":"Performance","level":"1.5","depth":1,"path":"performance.md","ref":"performance.md","articles":[]},"previous":{"title":"Parameter coercion","level":"1.4.4","depth":2,"path":"ring/parameter_coercion.md","ref":"ring/parameter_coercion.md","articles":[]},"dir":"ltr"},"config":{"plugins":["editlink","github","highlight"],"root":"doc","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"editlink":{"label":"Edit This Page","multilingual":false,"base":"https://github.com/metosin/reitit/tree/master/doc"},"github":{"url":"https://github.com/metosin/reitit"},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"gitbook":"*"},"file":{"path":"ring/compiling_middleware.md","mtime":"2017-11-11T15:37:51.825Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2017-11-11T15:38:28.608Z"},"basePath":"..","book":{"language":""}});