Hi all,
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.
Specifically, I'm interested in studying a Go project or code example that implements a JSON REST API, and does not serve HTML pages.
Ideally, this project/example would illustrate the following concepts:
- Best practices for structuring a multi-package/file Go web server
- Best practices of writing a slightly more complex Go project in general
- Use case of concurrency in a web server
Any help would be greatly appreciated!
评论:
earthboundkid:
deviantony:https://github.com/benbjohnson/wtf is what you're looking for. It has a long blog series too.
STOP_SCREAMING_AT_ME:This one. Helped me a lot!
brusbilis:Thanks, I'll look through the blog series as well!
xyproto:https://github.com/josephspurrier/gowebapp, Basic MVC Web Application in Go
F41LUR3:I wrote a small web server in Go with support for Lua, templates, several database backends and auto-reloading webpages in the browser upon save (using the
-a
flag).If anything is unclear, I would be happy to update the documentation.
mistretzu:Broken link :(
ChasingLogic:For learning authentication & redirecting to login:
STOP_SCREAMING_AT_ME:https://github.com/mattermost/platform
https://github.com/praelatus/backend
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
dobegor:Another commenter also gave an example with gorilla/mux for routing. Is it necessary?
STOP_SCREAMING_AT_ME:if you are just starting, I'd strongly prefer choosing pressly/chi nowadays. https://github.com/pressly/chi
Light, idiomatic and strongly stdlib-compatible. It uses 1.7 context.
fern4lvarez:Thanks for the reference, will go with the instead of gorilla/mux
dazzford:
net/http.ServeMux
is very very limited. In most of the cases it is necessary to try 3rd party routers.
neoasterisk:This is absolutely incorrect.
I've build dozens of high performance services currently serving high volumes of traffic with http.ServeMux.
Even with resources with a few values in the path, it was trivial to extract them.
dazzford:it was trivial to extract them.
Can you show how to extract them?
STOP_SCREAMING_AT_ME:Sure. Below I do some slicing with specific lengths, but I could just as easily have done a split on the "/".
Note, this is from a side project I'm working on, and not from my place of employment; but the concept is similar.
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) } }
dazzford:Even with resources with a few values in the path, it was trivial to extract them.
Any example? Speaking as a noob.
jjheiselman:Sure, take a look at my reply above.
fern4lvarez:Maybe Caddy fits the bill? https://github.com/mholt/caddy/
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 /u/mholt could clarify any points I've misstated.
I just released today the first version of piladb, and its daemon,
pilad
, is a JSON REST API fully implemented withnet/url
(plusgorilla/mux for routing
): https://github.com/fern4lvarez/piladb/tree/master/pilad