How would you prefer to use a middleware in your web application?

xuanbao · · 269 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;ve seen routers that allows you to use middleware like this:</p> <pre><code>router.Use(yourMiddleware) </code></pre> <p>and i&#39;ve been thinking that there is another approach and i want to know if it&#39;s useful or not, I am practicing on coding a mux in go <a href="https://github.com/emostafa/garson" rel="nofollow">Garson</a> and it occurred to me i could implement middleware usage like this</p> <pre><code>router := garson.New() // the next line registers two middlewares to be executed before each request router.Before(garson.LoggerMiddleware, garson.OnlyJSONMiddleware) // the next line registers one middleware to be executed after each request router.After(garson.SomeOtherMiddlware) // the next line registers one middleware to be executed only before this route router.Post(&#34;/api/comments&#34;).Before(AuthMiddleware) </code></pre> <p>what do you guys think ? in terms of real projects usage.</p> <hr/>**评论:**<br/><br/>Vlir: <pre><p>Check out the way Gin does it.. super succint and clear</p></pre>neoasterisk: <pre><p>It looks a little verbose to me and it doesn&#39;t seem to provide a good way to chain middleware (in 1 line).</p> <p>The only way you can be sure about the design is to use your own package to build something with a fair number of routers and middleware.</p> <p>Personally I never understood why people use external libraries for middleware but I digress.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

269 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传