Skip to content

Commit

Permalink
update documents
Browse files Browse the repository at this point in the history
  • Loading branch information
Songmu committed Apr 8, 2020
1 parent f56c6ef commit 85e3daf
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,21 @@ var middleware func(http.Handler) http.Handler = realip.MustMiddleware(&realip.C
RealIPHeader: realip.HeaderXForwardedFor,
RealIPRecursive: true,
})
var handler http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello World")
var handler http.HandlerFunc = func(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, req.Header.Get("X-Real-IP"))
})
handler = middleware(handler)
```

## Description

The realip package implements detecting client real IP mechanisms from request headers in Go's HTTP middleware layer.
This have a similar behavior as Nginx's ngx\_http\_realip\_module

This realizes the similar function as Nginx's `ngx_http_realip_module` in the layer inside Go.
Therefore, the setting property names and behaviors are also close to `ngx_http_realip_module`.

The realip provides Go's HTTP Middleware. It detects the client's real ip from the request and
sets it in the specified request header (Default: X-Real-IP).

## Installation

Expand Down
25 changes: 25 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Package realip provides cliant real ip detection mechanisms from http.Request
in Go's HTTP middleware layer.
_, ipnet, _ := net.ParseCIDR("192.168.0.0/16")
var middleware func(http.Handler) http.Handler = realip.MustMiddleware(&realip.Config{
RealIPFrom: []*net.IPNet{ipnet},
RealIPHeader: realip.HeaderXForwardedFor,
RealIPRecursive: true,
})
var handler http.HandlerFunc = func(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, req.Header.Get("X-Real-IP"))
})
handler = middleware(handler)
l, _ := net.Listen("tcp", "127.0.0.1:8080")
http.Serve(l, handler)
This realizes the same function as Nginx's ngx_http_realip_module in the layer inside Go.
Therefore, the setting property names and behaviors are also close to ngx_http_realip_module.
The realip provides Go's HTTP Middleware. It detects the client's real ip from the request and
sets it in the specified request header (Default: X-Real-IP).
*/
package realip
9 changes: 9 additions & 0 deletions realip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,13 @@ func TestMiddleware(t *testing.T) {
}
})
}

t.Run("error", func(t *testing.T) {
_, err := realip.Middleware(&realip.Config{
RealIPHeader: "Forwarded",
})
if err == nil {
t.Errorf("error should be occurred but nil")
}
})
}

0 comments on commit 85e3daf

Please sign in to comment.