repositorypackage
0.19.0
Repository: https://github.com/tigerwill90/foxgeoip.git
Documentation: pkg.go.dev
# README
Foxgeoip
FoxGeoIP is an experimental middleware for Fox that filters incoming requests based on the client's IP address using GeoIP data. It blocks or allows access based on country codes. This middleware is intended to work with MaxMind GeoLite2 or GeoIP2 databases. It may work with other geolocation databases as well.
Disclaimer
FoxGeoIP's API is closely tied to the Fox router, and it will only reach v1 when the router is stabilized. During the pre-v1 phase, breaking changes may occur and will be documented in the release notes.
Getting started
Installation
go get -u github.com/tigerwill90/foxgeoip
Feature
- Filters requests based on country codes, either allowing or blocking them.
- Supports whitelists or blacklists mode.
- Tightly integrates with the Fox ecosystem for enhanced performance and scalability.
- Provides structured logging with
log/slog
.
Usage
db, err := geoip2.Open("GeoLite2-Country.mmdb")
if err != nil {
panic(err)
}
defer db.Close()
f := fox.New(
fox.DefaultOptions(),
fox.WithClientIPStrategy(
strategy.NewRightmostNonPrivate(strategy.XForwardedForKey),
),
fox.WithMiddleware(
foxgeoip.Middleware(
db,
foxgeoip.WithBlacklistedCountries("US", "CN", "AU"),
),
),
)
f.MustHandle(http.MethodGet, "/hello/{name}", func(c fox.Context) {
_ = c.String(http.StatusOK, "hello %s\n", c.Param("name"))
})
log.Fatalln(http.ListenAndServe(":8080", f))