Skip to content
This repository has been archived by the owner on Feb 6, 2023. It is now read-only.

Add context to Provider interface #24

Open
sugarraysam opened this issue Oct 8, 2020 · 1 comment
Open

Add context to Provider interface #24

sugarraysam opened this issue Oct 8, 2020 · 1 comment

Comments

@sugarraysam
Copy link

It would be nice if the generated Provider interface would mimic what gRPC does and add a default ctx context.Context as the first argument.

// inside `provider.go`
type Provider interface {
    GetSomething(ctx context.Context, responses *GetSomethingResponses) (err error)
}

// and then in the Handler, simply use the context from the incoming request
// inside `server.go`
func HandleGetSomething(w http.ResponseWriter, r *http.Request) {
    // ... stuff
    err = provider.GetSomething(r.Context(), responses)
    // ... more stuff
}

This would allow passing context from an http.Client, or protecting your handler with a TimeoutMiddleware:

func TimeoutMiddleware(next http.Handler) http.Handler {
	timeout := 7 * time.Second
	msg := `{"error":  "your request timed out"}`
	return http.TimeoutHandler(next, timeout, msg)
}
@bvwells
Copy link
Contributor

bvwells commented Oct 17, 2020

Added a PR for this issue. How this is useful!

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

No branches or pull requests

2 participants