chiclientip
Getting started
Installation
go get gitea.dwysokinski.me/Kichiyaki/chiclientip
Usage
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.
Dawid Wysokiński - contact@dwysokinski.me