<p>Hi all,</p>
<p>I'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'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'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'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 & 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's stdlib compatible so I would think it'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'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'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 "/".</p>
<p>Note, this is from a side project I'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("/"):strings.Index(request.URL.Path, "/game/")]
gameId := request.URL.Path[strings.Index(request.URL.Path, "/game/")+6:]
token := authentication.Authenticate(request.Header.Get("Authorization"))
if request.Method != "OPTIONS" && token.Claims["gamer"].(string) != gamerId {
w.WriteHeader(http.StatusForbidden)
return
}
switch request.Method {
case "OPTIONS":
w.Header().Add("Access-Control-Allow-Methods", "PUT,DELETE")
w.Header().Add("Access-Control-Allow-Headers", "Content-Type,Accept,Authorization")
w.WriteHeader(http.StatusOK)
case "DELETE":
deleteGame(w, request, gamerId, gameId)
case "PUT":
addGame(w, request, gamerId, gameId)
default:
http.Error(w, "Method Not Allowed", 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't really looked at a lot of his code, but it appears as though it is all his self developed framework so it isn't technically third party, but maybe not what you're looking for. Also, maybe <a href="/u/mholt" rel="nofollow">/u/mholt</a> could clarify any points I'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>
Can someone point me to a web server written in Go using just net/http (no 3rd party framework)?
blov · · 659 次点击这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传