Can someone point me to a web server written in Go using just net/http (no 3rd party framework)?

blov · · 659 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hi all,</p> <p>I&#39;m learning Go these days, and have written a basic one-file web server in Go. I would like to now look at what other people have written, and learn about best practices on structuring code.</p> <p>Specifically, I&#39;m interested in studying a Go project or code example that implements a JSON REST API, and does not serve HTML pages.</p> <p>Ideally, this project/example would illustrate the following concepts:</p> <ol> <li>Best practices for structuring a multi-package/file Go web server</li> <li>Best practices of writing a slightly more complex Go project in general</li> <li>Use case of concurrency in a web server</li> </ol> <p>Any help would be greatly appreciated!</p> <hr/>**评论:**<br/><br/>earthboundkid: <pre><p><a href="https://github.com/benbjohnson/wtf">https://github.com/benbjohnson/wtf</a> is what you&#39;re looking for. It has a long blog series too.</p></pre>deviantony: <pre><p>This one. Helped me a lot! </p></pre>STOP_SCREAMING_AT_ME: <pre><p>Thanks, I&#39;ll look through the blog series as well!</p></pre>brusbilis: <pre><p><a href="https://github.com/josephspurrier/gowebapp" rel="nofollow">https://github.com/josephspurrier/gowebapp</a>, Basic MVC Web Application in Go </p></pre>xyproto: <pre><p>I wrote a small <a href="https://algernon.roboticoverlords.org" rel="nofollow">web server</a> in Go with support for Lua, templates, several database backends and auto-reloading webpages in the browser upon save (using the <code>-a</code> flag).</p> <p>If anything is unclear, I would be happy to update the documentation.</p></pre>F41LUR3: <pre><p>Broken link :(</p></pre>mistretzu: <pre><p>For learning authentication &amp; redirecting to login:</p> <p><a href="https://play.golang.org/p/mov3sLp9Ic" rel="nofollow">https://play.golang.org/p/mov3sLp9Ic</a></p></pre>ChasingLogic: <pre><p><a href="https://github.com/mattermost/platform" rel="nofollow">https://github.com/mattermost/platform</a></p> <p><a href="https://github.com/praelatus/backend" rel="nofollow">https://github.com/praelatus/backend</a></p> <p>Both of these are using gorilla mux for routing but that&#39;s stdlib compatible so I would think it&#39;d be pretty easy to extrapolate from there</p></pre>STOP_SCREAMING_AT_ME: <pre><p>Another commenter also gave an example with gorilla/mux for routing. Is it necessary?</p></pre>dobegor: <pre><p>if you are just starting, I&#39;d strongly prefer choosing pressly/chi nowadays. <a href="https://github.com/pressly/chi">https://github.com/pressly/chi</a></p> <p>Light, idiomatic and strongly stdlib-compatible. It uses 1.7 context.</p></pre>STOP_SCREAMING_AT_ME: <pre><p>Thanks for the reference, will go with the instead of gorilla/mux</p></pre>fern4lvarez: <pre><p><a href="https://golang.org/pkg/net/http/#ServeMux" rel="nofollow"><code>net/http.ServeMux</code></a> is very very limited. In most of the cases it is necessary to try 3rd party routers.</p></pre>dazzford: <pre><p>This is absolutely incorrect.</p> <p>I&#39;ve build dozens of high performance services currently serving high volumes of traffic with http.ServeMux. </p> <p>Even with resources with a few values in the path, it was trivial to extract them.</p></pre>neoasterisk: <pre><blockquote> <p>it was trivial to extract them.</p> </blockquote> <p>Can you show how to extract them?</p></pre>dazzford: <pre><p>Sure. Below I do some slicing with specific lengths, but I could just as easily have done a split on the &#34;/&#34;.</p> <p>Note, this is from a side project I&#39;m working on, and not from my place of employment; but the concept is similar.</p> <pre><code>func games(w http.ResponseWriter, request *http.Request) { gamerId := request.URL.Path[len(&#34;/&#34;):strings.Index(request.URL.Path, &#34;/game/&#34;)] gameId := request.URL.Path[strings.Index(request.URL.Path, &#34;/game/&#34;)+6:] token := authentication.Authenticate(request.Header.Get(&#34;Authorization&#34;)) if request.Method != &#34;OPTIONS&#34; &amp;&amp; token.Claims[&#34;gamer&#34;].(string) != gamerId { w.WriteHeader(http.StatusForbidden) return } switch request.Method { case &#34;OPTIONS&#34;: w.Header().Add(&#34;Access-Control-Allow-Methods&#34;, &#34;PUT,DELETE&#34;) w.Header().Add(&#34;Access-Control-Allow-Headers&#34;, &#34;Content-Type,Accept,Authorization&#34;) w.WriteHeader(http.StatusOK) case &#34;DELETE&#34;: deleteGame(w, request, gamerId, gameId) case &#34;PUT&#34;: addGame(w, request, gamerId, gameId) default: http.Error(w, &#34;Method Not Allowed&#34;, 405) } } </code></pre></pre>STOP_SCREAMING_AT_ME: <pre><blockquote> <p>Even with resources with a few values in the path, it was trivial to extract them.</p> </blockquote> <p>Any example? Speaking as a noob.</p></pre>dazzford: <pre><p>Sure, take a look at my reply above.</p></pre>jjheiselman: <pre><p>Maybe Caddy fits the bill? <a href="https://github.com/mholt/caddy/" rel="nofollow">https://github.com/mholt/caddy/</a></p> <p>I haven&#39;t really looked at a lot of his code, but it appears as though it is all his self developed framework so it isn&#39;t technically third party, but maybe not what you&#39;re looking for. Also, maybe <a href="/u/mholt" rel="nofollow">/u/mholt</a> could clarify any points I&#39;ve misstated.</p></pre>fern4lvarez: <pre><p>I just released today the first version of <a href="https://www.piladb.org" rel="nofollow">piladb</a>, and its daemon, <code>pilad</code>, is a JSON REST API fully implemented with <code>net/url</code> (plus <code>gorilla/mux for routing</code>): <a href="https://github.com/fern4lvarez/piladb/tree/master/pilad" rel="nofollow">https://github.com/fern4lvarez/piladb/tree/master/pilad</a></p></pre>

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

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