Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Proposal] Use gorilla/mux router V2 #204

Closed
wants to merge 1 commit into from

Conversation

marwan-at-work
Copy link
Contributor

This PR shows how much the SimpleServer would get cleaned up if we used gorilla/mux as the main router.

Background:

The SimpleServer has only one Router: gorilla/mux. However, there's a lot of abstraction built around it that limits its features. Such missing features are 1. Multiple Prefixes, 2. Group Middlewares, 3. Trailing Slashes, and more.

Evolution

With server/kit being the 2nd generation of Gizmo, we should make the SimpleServer be truly simple without losing any of the production-default features that it has provided in the past, this PR achieves that.

Notable changes:

  1. Removed most SimpleServer interfaces.
  2. No features are lost through removal (debug/health handlers, server shutdowns, recovery etc)
  3. User does not need to wait on us to give them access to gorilla/mux features
  4. The learning curve for gizmo is a lot less steep, win for new comers.
  5. You are not stuck with one Prefix and one global Middleware. Therefore, you can use gorilla/mux features to have multiple Sub Routers with varying prefixes, and apply Middlewares to groups of routes together (not just one for each handler, or global ones only)
  6. No need for user to implement any Service interfaces for a SimpleServer, here's an example of a how user writes a simple gizmo program:
import (
  "github.com/gorilla/mux"
  "github.com/nytimes/gizmo/v2/server"
)

func main() {
  r := mux.NewRouter()
  // register routes
  server.Init("my-api", &server.Config{HTTPPort: 3000})
  server.Register(r)
  server.Run()
}

Which makes the SimpleServer a lot quicker to learn, spin up, and work with.

TODO:

  1. Update simple_server_test.go
  2. Update all examples

Further design considerations:

  1. It'd be nice to make server.Run take an http.Handler, this way Register has one job (not two) and Run can take Handler-compositions.

  2. If we want to be more flexible and have the user pick a mux, we can make Register take a "registerer" function or interface that will register all the routes regardless of what the mux is.

@coveralls
Copy link

Coverage Status

Coverage decreased (-9.6%) to 37.478% when pulling 69b5ae4 on marwan-at-work:ss into 11877ba on NYTimes:master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants