chiclientip/README.md

54 lines
1.1 KiB
Markdown

# chiclientip [![Build Status](https://woodpecker.dwysokinski.me/api/badges/5/status.svg)](https://woodpecker.dwysokinski.me/repos/5)
## Getting started
### Installation
```shell
go get gitea.dwysokinski.me/Kichiyaki/chiclientip
```
### 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("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)