Categorygithub.com/tigerwill90/foxgeoip
modulepackage
0.19.0
Repository: https://github.com/tigerwill90/foxgeoip.git
Documentation: pkg.go.dev

# README

Go Reference tests Go Report Card codecov GitHub release (latest SemVer) GitHub go.mod Go version

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))

# Functions

DefaultBlockingResponse is the default response for blocked IPs.
Middleware creates a middleware function for the IP filter.
New creates a new IPFilter with the provided GeoIP2 reader and options.
WithBlacklistedCountries sets the blacklist with the provided country codes.
WithClientIPResolver sets a custom resolver to determine the client IP address.
WithFilter appends the provided filters to the middleware's filter list.
WithLogHandler sets a custom log handler for structured logging.
WithResponse sets a custom response handler for blocked requests.
WithWhitelistedCountries sets the whitelist with the provided country codes.

# Structs

No description provided by the author

# Interfaces

No description provided by the author

# Type aliases

No description provided by the author