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

Improve documentation for custom response writer implementation #22

Merged
merged 2 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ func handle(c fox.Context) {
Fox itself implements the `http.Handler` interface which make easy to chain any compatible middleware before the router. Moreover, the router
provides convenient `fox.WrapF`, `fox.WrapH` and `fox.WrapM` adapter to be use with `http.Handler`.

The route parameters are being accessed by the wrapped handler through the `context.Context` when the adapter `fox.WrapF` and `fox.WrapH` are used.
The route parameters can be accessed by the wrapped handler through the `context.Context` when the adapters `fox.WrapF` and `fox.WrapH` are used.

Wrapping an `http.Handler`
```go
Expand All @@ -388,6 +388,13 @@ f.MustHandle(http.MethodGet, "/articles/{id}", func(c fox.Context) {
})
````

### Custom http.ResponseWriter Implementations
When using custom `http.ResponseWriter` implementations, it's important to ensure that these implementations expose the
required http interfaces. For HTTP/1.x requests, Fox expects the `http.ResponseWriter` to implement the `http.Flusher`,
`http.Hijacker`, and `io.ReaderFrom` interfaces. For HTTP/2 requests, the `http.ResponseWriter` should implement the
`http.Flusher` and `http.Pusher` interfaces. Fox will invoke these methods **without any prior assertion**.


## Middleware
Middlewares can be registered globally using the `fox.WithMiddleware` option. The example below demonstrates how
to create and apply automatically a simple logging middleware to all routes (including 404, 405, etc...).
Expand Down
7 changes: 7 additions & 0 deletions fox.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,13 @@ func defaultOptionsHandler(c Context) {
c.Writer().WriteHeader(http.StatusOK)
}

// ServeHTTP is the main entry point to serve a request. It handles all incoming HTTP requests and dispatches them
// to the appropriate handler function based on the request's method and path.
//
// It expects the http.ResponseWriter provided to implement the http.Flusher, http.Hijacker, and io.ReaderFrom
// interfaces for HTTP/1.x requests and the http.Flusher and http.Pusher interfaces for HTTP/2 requests.
// If a custom response writer is used, it is critical to ensure that these methods are properly exposed as Fox
// will invoke them without any prior assertion.
func (fox *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {

var (
Expand Down
8 changes: 5 additions & 3 deletions params.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ package fox

import netcontext "context"

type ctxKey struct{}

// paramsKey is the key that holds the Params in a context.Context.
var paramsKey = struct{}{}
var paramsKey = ctxKey{}

type Param struct {
Key string
Expand Down Expand Up @@ -44,9 +46,9 @@ func (p Params) Clone() Params {
return cloned
}

// ParamsFromContext allows extracting params from the given context.
// ParamsFromContext is a helper to retrieve params from context when a http.Handler
// is registered using WrapF or WrapH.
func ParamsFromContext(ctx netcontext.Context) Params {
p, _ := ctx.Value(paramsKey).(Params)

return p
}