chiclientip/README.md

54 lines
1.1 KiB
Markdown
Raw Normal View History

2023-08-20 05:28:04 +00:00
# chiclientip [![Build Status](https://woodpecker.dwysokinski.me/api/badges/5/status.svg)](https://woodpecker.dwysokinski.me/repos/5)
2023-05-12 04:54:44 +00:00
## Getting started
### Installation
```shell
2023-05-12 04:55:29 +00:00
go get gitea.dwysokinski.me/Kichiyaki/chiclientip
2023-05-12 04:54:44 +00:00
```
### 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")
2023-05-12 04:54:44 +00:00
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)