<p>Hi all!</p>
<p>I've recently started building 'real' Golang web apps / API services, and have come across the need for middleware.</p>
<p>I've spent quite a bit of time Googling it, and it appears that there are a lot of options: rolling things manually, using something like Interpose (<a href="https://github.com/carbocation/interpose">https://github.com/carbocation/interpose</a>), or using some of the other frameworks.</p>
<p>I'm currently building an API service using gorilla/mux, and would love to use something that works cleanly with this. Is interpose the best solution?</p>
<p>I'd love to hear from anyone who has built similar things.</p>
<p>Thanks!</p>
<hr/>**评论:**<br/><br/>dghubble: <pre><p>I like <a href="https://github.com/justinas/alice">https://github.com/justinas/alice</a> which chains ordinary http handlers together and is extremely minimal.</p>
<p>Stuff like negroni and many others feel the need to reinvent the Handler interface. Avoid.</p></pre>sethammons: <pre><p>agreed; I'm not a fan of the interface change required by Negroni</p></pre>webRat: <pre><p>+1 to alice.</p></pre>ecmdome: <pre><p><a href="https://github.com/codegangsta/negroni">https://github.com/codegangsta/negroni</a> is a really good middleware library</p></pre>zackkitzmiller: <pre><p>+1 for Negroni.</p></pre>DavidDavidsonsGhost: <pre><p>So what's the advantage of using this?</p></pre>ecmdome: <pre><p>Advantages over what? Its a very idiomatic middleware library. You could use it to create a logging middleware, authentication, etc. </p></pre>DavidDavidsonsGhost: <pre><p>Its not that hard to create these things anyway. What value does using the library add?</p></pre>ecmdome: <pre><p>The Go standard library has everything you need to write a Go web app. Using some vetted libraries like Gorilla's MUX and Context, negroni, logrus, etc. Just helps speed the development process along. I agree that you should stay away from depending on unnecessary libraries... But in some cases I prefer to take that risk. </p></pre>DavidDavidsonsGhost: <pre><p>Gorilla mux adds a bunch of functionality to the standard server mux. If your intention is just to add logging and sessions then that's easy to do.</p></pre>ecmdome: <pre><p>Can't really disagree with you. </p></pre>blueblank: <pre><p>There are always people carpet bombing positives for this as if they are paid to. </p>
<p>There is zero utility in using this as you can replicate it all with a bit of work, using the standard library -- which is the stock framework critique, but it is particularly apt here.</p></pre>ecmdome: <pre><p>The OP asked about a library for middleware that's a bit more general purpose than the example he mentioned.</p>
<p>Negroni in comparison is light and doesn't get in the way of anything. There are also many third party libraries that plug right into it.</p>
<p>Negroni serves a purpose. But I agree that one should be weary of relying on external libraries for tasks they could accomplish themselves.</p>
<p>I still think its a cleanly written library so I will use this implementation personally. </p></pre>rdegges: <pre><blockquote>
<p><a href="https://github.com/codegangsta/negroni" rel="nofollow">https://github.com/codegangsta/negroni</a></p>
</blockquote>
<p>This looks way better. Thanks <sup>^</sup> I think I'll most likely give this a go =)</p></pre>pinpinbo: <pre><p>I like <a href="https://github.com/carbocation/interpose" rel="nofollow">https://github.com/carbocation/interpose</a> because it conforms to <code>http.Handler</code> interface.</p></pre>om0tho: <pre><p>It really depends on what your "real" app is doing. But, it could be as easy as using <a href="http://golang.org/pkg/net/http">net/http</a>.</p>
<pre><code>func checkAuth(h http.HandlerFunc) http.HandlerFunc {
return func(resp http.ResponseWriter, req *http.Request) {
// check if authenticated
}
}
http.HandleFunc("/user-profile", checkAuth(serveUserProfile))
</code></pre>
<p>I've been able to use <a href="http://golang.org/pkg/net/http">net/http</a> for a lot of routing. Though, I would probably use something like <a href="https://github.com/gorilla/mux">gorilla/mux</a> or <a href="https://github.com/julienschmidt/httprouter">julienschmidt/httprouter</a> if you had to do any subrouting. For getting data from the URL, you can do this.</p>
<pre><code>// note the trailing slash
http.HandleFunc("/user-profile/", checkAuth(serveUserProfile))
// URL like /user-profile/12345
func serveUserProfile(w http.ResponseWriter, r *http.Request) {
key := r.URL.Path[len("/user-profile/"):]
userID, err := strconv.Atoi(key)
// etc...
}
</code></pre></pre>darrrrrren: <pre><p>This isn't a direct answer but have you seen <a href="https://github.com/zenazn/goji" rel="nofollow">Goji</a>? It handles middleware really gracefully but admittedly it is not 100% generic.</p></pre>emadera52: <pre><p>If you haven't looked at <a href="https://github.com/sclevine/infuse" rel="nofollow">infuse</a> yet, check it out. According to README</p>
<blockquote>
<p>An infuse.Handler is fully compatible with the Go standard library, supports flexible chaining, and provides a shared context between middleware handlers without relying on global state, locks, or shared closures.</p>
</blockquote></pre>tiancaiamao: <pre><p><a href="https://github.com/tiancaiamao/middleware" rel="nofollow">https://github.com/tiancaiamao/middleware</a> is what I like most so far, it conforms to http.Handler interface and retain the ability to pass context.</p></pre>blueblank: <pre><p>The one you build yourself.</p></pre>newimprovedoriginal: <pre><p>You can do a lot w/out much code, like this example</p>
<p><a href="http://laicos.com/writing-handsome-golang-middleware/" rel="nofollow">http://laicos.com/writing-handsome-golang-middleware/</a></p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传