54 lines
1.2 KiB
Markdown
54 lines
1.2 KiB
Markdown
|
# chiclientip [![Build Status](https://drone.dwysokinski.me/api/badges/Kichiyaki/chiclientip/status.svg)](https://drone.dwysokinski.me/Kichiyaki/chiclientip)
|
||
|
|
||
|
## Getting started
|
||
|
|
||
|
### Installation
|
||
|
|
||
|
```shell
|
||
|
go get gitea.dwysokinski.me/Kichiyaki/chirealip
|
||
|
```
|
||
|
|
||
|
### Usage
|
||
|
|
||
|
```go
|
||
|
package main
|
||
|
|
||
|
import (
|
||
|
"log"
|
||
|
"net/http"
|
||
|
|
||
|
"gitea.dwysokinski.me/Kichiyaki/chiclientip"
|
||
|
"github.com/go-chi/chi/v5"
|
||
|
"github.com/realclientip/realclientip-go"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
stratXForwardedFor, err := realclientip.NewRightmostNonPrivateStrategy(http.CanonicalHeaderKey("X-Forwarded-For"))
|
||
|
if err != nil {
|
||
|
log.Fatalln("realclientip.NewRightmostNonPrivateStrategy", err)
|
||
|
}
|
||
|
strat := realclientip.NewChainStrategy(
|
||
|
stratXForwardedFor,
|
||
|
realclientip.RemoteAddrStrategy{},
|
||
|
)
|
||
|
|
||
|
router := chi.NewRouter()
|
||
|
router.Use(chiclientip.ClientIP(strat))
|
||
|
router.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
||
|
clientIP, _ := chiclientip.ClientIPFromContext(r.Context())
|
||
|
_, _ = w.Write([]byte(clientIP))
|
||
|
})
|
||
|
|
||
|
http.ListenAndServe(":8080", router)
|
||
|
}
|
||
|
|
||
|
```
|
||
|
|
||
|
## License
|
||
|
|
||
|
Distributed under the MIT License. See ``LICENSE`` for more information.
|
||
|
|
||
|
## Contact
|
||
|
|
||
|
Dawid Wysokiński - [contact@dwysokinski.me](mailto:contact@dwysokinski.me)
|