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

Default logger middleware #33

Merged
merged 28 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
d50da89
feat: wip on default logger middleware
tigerwill90 Jun 23, 2024
8ea2025
ci: update go version to 1.21
tigerwill90 Jun 23, 2024
e6db904
feat: fix test
tigerwill90 Jun 23, 2024
c1b6fb7
feat: wip in recovery middleware
tigerwill90 Jun 24, 2024
804dbb0
Merge branch 'master' into feat/default-logger-middleware
tigerwill90 Jun 24, 2024
525d4af
feat: wip logging middleware
tigerwill90 Jun 25, 2024
fab647c
feat: improve recovery middleware tests
tigerwill90 Jun 25, 2024
07edbe8
feat: remove Context.Ctx method to reduce api surface & small optimis…
tigerwill90 Jun 25, 2024
4a94cfe
feat: rename context to cTx in to avoid the inconvenient rename of co…
tigerwill90 Jun 25, 2024
dbe3f6c
feat: add test for logger middleware
tigerwill90 Jun 25, 2024
340ea98
feat: improve test coverage
tigerwill90 Jun 25, 2024
b62cdf6
feat: improve test coverage
tigerwill90 Jun 25, 2024
fcce091
feat: implement Context.ClientIP as well as multi ClientIPStrategy
tigerwill90 Jun 26, 2024
7697fd2
feat: use min builtin function
tigerwill90 Jun 26, 2024
4183919
ci: update actions
tigerwill90 Jun 26, 2024
0a5a6db
ci: update actions
tigerwill90 Jun 26, 2024
8a0209a
ci: update actions
tigerwill90 Jun 26, 2024
7e3a6ae
feat: improve test coverage
tigerwill90 Jun 26, 2024
7c35c1e
feat: improving documentation
tigerwill90 Jun 26, 2024
20a1ea7
feat: improving documentation
tigerwill90 Jun 26, 2024
36fd3f7
feat: improving documentation
tigerwill90 Jun 26, 2024
7c6e666
feat: improving documentation
tigerwill90 Jun 26, 2024
229fe66
feat: improving test coverage
tigerwill90 Jun 26, 2024
8d8f5fc
feat: improve docs and naming
tigerwill90 Jun 26, 2024
0e694ec
feat: improve docs and naming
tigerwill90 Jun 26, 2024
2a22d64
docs: add proper credit
tigerwill90 Jun 26, 2024
c63e4e5
docs: fix documentation
tigerwill90 Jun 26, 2024
9876e11
docs: fix documentation
tigerwill90 Jun 26, 2024
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
19 changes: 10 additions & 9 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go: [ '>=1.19' ]
go: [ '>=1.21' ]
steps:
- name: Set up Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go }}
cache: false

- name: Check out code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Run tests
run: go test -v -coverprofile=coverage.txt -covermode=atomic ./...
Expand All @@ -27,24 +27,25 @@ jobs:
run: go test -v -race -run TestDataRace -count=10 ./...

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v4
with:
flags: coverage.txt
files: ./coverage.txt
token: ${{ secrets.CODECOV_TOKEN }}
lint:
name: Lint Fox
runs-on: ubuntu-latest
strategy:
matrix:
go: [ '>=1.19' ]
go: [ '>=1.21' ]
steps:
- name: Set up Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go }}
cache: false

- name: Check out code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Run linter
uses: golangci/golangci-lint-action@v3
uses: golangci/golangci-lint-action@v6
53 changes: 52 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ or missing trailing slash, at no extra cost.
**Automatic OPTIONS replies:** Inspired from [httprouter](https://github.com/julienschmidt/httprouter), the router has built-in native
support for [OPTIONS requests](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS).

**Client IP Derivation:** Accurately determine the "real" client IP address using best practices tailored to your network topology.

Of course, you can also register custom `NotFound` and `MethodNotAllowed` handlers.

## Getting started
Expand Down Expand Up @@ -484,6 +486,55 @@ f := fox.New(
)
````

## Client IP Derivation
The `WithClientIPStrategy` option allows you to set up strategies to resolve the client IP address based on your
use case and network topology. Accurately determining the client IP is hard, particularly in environments with proxies or
load balancers. For example, the leftmost IP in the `X-Forwarded-For` header is commonly used and is often regarded as the
"closest to the client" and "most real," but it can be easily spoofed. Therefore, you should absolutely avoid using it
for any security-related purposes, such as request throttling.

The strategy used must be chosen and tuned for your network configuration. This should result in the strategy never returning
an error and if it does, it should be treated as an application issue or a misconfiguration, rather than defaulting to an
untrustworthy IP.

The sub-package `github.com/tigerwill90/fox/strategy` provides a set of best practices strategies that should cover most use cases.

````go
f := fox.New(
fox.DefaultOptions(),
fox.WithClientIPStrategy(
// We are behind one or many trusted proxies that have all private-space IP addresses.
strategy.NewRightmostNonPrivate(fox.HeaderXForwardedFor),
),
)

f.MustHandle(http.MethodGet, "/foo/bar", func(c fox.Context) {
ipAddr, err := c.ClientIP()
if err != nil {
// If the current strategy is not able to derive the client IP, an error
// will be returned rather than falling back on an untrustworthy IP. It
// should be treated as an application issue or a misconfiguration.
panic(err)
}
fmt.Println(ipAddr.String())
})
````

It is also possible to create a chain with multiple strategies that attempt to derive the client IP, stopping when the first one succeeds.

````go
f := fox.New(
fox.DefaultOptions(),
fox.WithClientIPStrategy(strategy.NewChain(
strategy.NewLeftmostNonPrivate(fox.HeaderXForwardedFor),
strategy.NewRemoteAddr(),
)),
)
````

Note that there is no "sane" default strategy, so calling `Context.ClientIP` without a strategy configured will return an `ErrNoClientIPStrategy`.

See this [blog post](https://adam-p.ca/blog/2022/03/x-forwarded-for/) for general guidance on choosing a strategy that fit your needs.
## Benchmark
The primary goal of Fox is to be a lightweight, high performance router which allow routes modification at runtime.
The following benchmarks attempt to compare Fox to various popular alternatives, including both fully-featured web frameworks
Expand Down Expand Up @@ -623,7 +674,7 @@ BenchmarkPat_GithubAll 424 2899405 ns/op 1843501
- [x] [Update route syntax](https://github.com/tigerwill90/fox/pull/10#issue-1643728309) @v0.6.0
- [x] [Route overlapping](https://github.com/tigerwill90/fox/pull/9#issue-1642887919) @v0.7.0
- [x] [Route overlapping (catch-all and params)](https://github.com/tigerwill90/fox/pull/24#issue-1784686061) @v0.10.0
- [x] [Ignore trailing slash](https://github.com/tigerwill90/fox/pull/32) @v0.14.0
- [x] [Ignore trailing slash](https://github.com/tigerwill90/fox/pull/32), [Builtin Logger Middleware](https://github.com/tigerwill90/fox/pull/33), [Client IP Derivation](https://github.com/tigerwill90/fox/pull/33) @v0.14.0
- [ ] Improving performance and polishing

## Contributions
Expand Down
Loading
Loading