What does HackerNews think of httprouter?
A high performance HTTP request router that scales well
[1]: https://github.com/julienschmidt/httprouter
[2]: https://github.com/infogulch/pathmatcher
[3]: https://github.com/julienschmidt/go-http-routing-benchmark
- gotest
- No DI, but in hindsight this would have been the right thing to do. The dev team is 100% Python, so mocking was more the talk of the office than DI/IoC.
- For routing and HTTP the service used httprouter https://github.com/julienschmidt/httprouter
I think it's a fantastic language, gofmt and gotest are both great utilities, and the short time to create an executable made development turnaround a breeze.
However, I think for the purposes of a simple REST service I would probably use Python/Flask. Less boilerplate and for a Python team, it would have made more sense ...
- Testing: https://golang.org/pkg/testing/ + https://pkg.go.dev/mod/github.com/stretchr/testify
- Mocks: https://github.com/golang/mock
- Dependency Injection: None, current user of https://github.com/uber-go/dig and I regret it.
Appears to be faster, but I guess the difference wouldn't be noticeable until you start doing some pretty heavy stuff.
Let me render templates in a layout file with `yield` - https://github.com/unrolled/render
Let me quickly route with param support - https://github.com/julienschmidt/httprouter
Sessions, flash messages, and secure cookies - http://www.gorillatoolkit.org/pkg/sessions
POST parameter-to-structure binding (this is optional and easy enough to do by hand) - https://github.com/mholt/binding
I write my own middlewares for logging, auth, etc. Use viper for configuration, gorm for an ORM, and for anything more complicated, accept that you’re about to learn a whole lot about it because you’re gonna be writing it yourself.
I actually have learned a lot about HTTP/HTTPS servers by working on a lower level and building up my stack! I recommend just starting with the stdlib and importing 3rd party code where you feel yourself limited by it. The first thing people probably adopt is a more expressive router.
[1] https://github.com/gin-gonic/gin
Just that you might need a library for routing like https://github.com/julienschmidt/httprouter
- julienschmidt/httprouter [1] for routing,
- jmoiron/sqlx [2] for SQL access, and
- gorilla/websocket [3] for websockets
sqlx isn't an ORM; it's more a convenience wrapper around the standard library's database/sql package.
gorm is the nicest ORM I've found, but I think even the well-written ORM packages are unnatural to use because of restrictions in Go's type system.
(Sorry if this isn't the kind of answer you were hoping for.)
[1] https://github.com/julienschmidt/httprouter
- Effective and flexible middlewares flow control, create anything by middleware
- Powerful and smart error handler, make development easy
- Trie base gear.Router, it is as faster as [HttpRouter](https://github.com/julienschmidt/httprouter), but more powerful
- Integrated timeout context.Context
- Integrated response content compress
- Integrated structured logging middleware
- Integrated request body parser
- Integrated signed cookies
- Integrated JSON, JSONP, XML and HTML renderer
- Integrated CORS, Secure, Favicon and Static middlewares
- More useful methods on gear.Context to manipulate HTTP Request/Response
- Completely HTTP/2.0 supported
These libraries also support middleware wrapping [1], a technique that the default mux supports but doesn't help you with. For example, adding CORS headers or logging the request.
"In contrast to the default mux of Go's net/http package, this router supports variables in the routing pattern and matches against the request method. It also scales better."
https://gdstechnology.blog.gov.uk/2013/12/05/building-a-new-...
The Vulcan proxy from Mailgun does the same thing:
http://vulcand.github.io/proxy.html#route
It looks like it doesn't, so existing open source golang url routers will perform much better, contrary to this post. The one in vuland is interesting in that it supports both regex AND tries, so you get the best of both.
Additionally, httprouter (golang) uses a trie and is fast as hell:
https://github.com/julienschmidt/httprouter
Edit: Adding another one (thanks buro9!) that is used in production by CloudFlare:
(Or maybe the author, also the OP, could join and tell how Lion compares to HttpRouter)
Maybe it's personal preference, but there's something more satisfying about the modular approach of building up an app using only components I need and understand, rather than starting with a magical feature-laden framework but quickly realizing you don't need half of what it does. I think Go aligns particularly well with this philosophy given its emphasis on composition.
I'll definitely stick to httprouter as my default starting point, though. The params interface is much nicer, and it's pretty damn good with performance and allocation count: https://github.com/julienschmidt/httprouter
> I have very little interest in boosting Goji's router's benchmark scores. There is an obvious solution here--radix trees--and maybe if I get bored I'll implement one for Goji, but I think the API guarantees and conceptual simplicity Goji provides are more important (all routes are attempted, one after another, until a matching route is found). Even if I choose to optimize Goji's router, Goji's routing semantics will not change.
Maybe you can just use HttpRouter without reimplementing it yourself?
https://github.com/julienschmidt/httprouter
> The router is optimized for best performance and a small memory footprint. It scales well even with very long pathes and a large number of routes. A compressing dynamic trie (radix tree) structure is used for efficient matching.
goji.Get("/hello/:name", hello)
router := httprouter.New()
router.GET("/hello/:name", Hello)