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

httpbp: force http servers to define the methods their endpoints support #229

Merged
merged 6 commits into from
Jun 10, 2020

Conversation

pacejackson
Copy link
Contributor

No description provided.

// Use the http.Method* constants from "net/http" for the values in this slice
// to ensure that you are using methods that are supported and in the format
// we expect.
Methods []string
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔕 Would it be better to have this be 2 values? Method string which is required and AdditionalMethods []string which is an optional list of additional HTTP methods that your endpoint supports? I feel like most cases just want to support a single method so this might be a better option on average?

wrappers := make([]Middleware, 0, len(f.middlewares)+len(middlewares))
func (f httpHandlerFactory) NewHandler(endpoint Endpoint) http.Handler {
wrappers := make([]Middleware, 0, len(f.middlewares)+len(endpoint.Middlewares)+1)
wrappers = append(wrappers, SupportedMethods(endpoint.Methods[0], endpoint.Methods[1:]...))
Copy link
Contributor Author

@pacejackson pacejackson Jun 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note, this will panic if you pass in an empty slice... let it! We already check this earlier by calling endpoint.Validate

httpbp/server.go Show resolved Hide resolved
wrappers := make([]Middleware, 0, len(f.middlewares)+len(middlewares))
func (f httpHandlerFactory) NewHandler(endpoint Endpoint) http.Handler {
wrappers := make([]Middleware, 0, len(f.middlewares)+len(endpoint.Middlewares)+1)
wrappers = append(wrappers, SupportedMethods(endpoint.Methods[0], endpoint.Methods[1:]...))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔕 til this won't cause panic when the slice contains one item: https://play.golang.org/p/HtkVu8laHF_n

@pacejackson pacejackson merged commit ef6e61f into reddit:master Jun 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants