middleware

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 15, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package middleware resolves per-request country context and injects it into the gin context, mirroring modules/auth/middleware's c.Set("user", ...) pattern.

Index

Constants

View Source
const (
	// AccountCountryKey is the gin context key for the authenticated
	// user's home country — authoritative for anything money-shaped.
	// Absent if no AccountCountryResolver was registered, or the current
	// request is unauthenticated.
	AccountCountryKey = "accountCountry"

	// DetectedCountryKey is the gin context key for the country inferred
	// from the request's client IP — display/discovery convenience only,
	// never authoritative for billing. Always set (falling back to the
	// module's configured default) unless GeoIP detection is disabled and
	// no default could be resolved either.
	DetectedCountryKey = "detectedCountry"
)

Variables

This section is empty.

Functions

func FromContext

func FromContext(c *gin.Context, key string) (*models.Country, bool)

FromContext reads a resolved country back off the gin context. ok=false means that key was never set (see the doc comments on AccountCountryKey / DetectedCountryKey for when that happens).

func New

func New(
	countryResolver CountryResolver,
	geoResolver geoip.Resolver,
	getClientIP ClientIPFunc,
	getUserID AuthenticatedUserIDFunc,
	accountResolver AccountCountryResolverFunc,
) gin.HandlerFunc

New builds the country-resolution middleware. accountResolver and getUserID may be nil (AccountCountry resolution is then simply skipped for every request — DetectedCountry still works).

AccountCountry is always set for an authenticated request once a resolver/getUserID pair is registered: it resolves to the user's real country when that country is known and active, and otherwise falls back to countryResolver.Default() (the module's configured "Global" sentinel by default — see config.Default). Callers therefore never need to handle a third "nothing resolved" case; the fallback is explicit rather than the key being silently absent.

Types

type AccountCountryResolverFunc

type AccountCountryResolverFunc func(ctx context.Context, userID string) (countryCode string, ok bool)

AccountCountryResolverFunc looks up the ISO country code for an authenticated user's account/profile. Supplied by the host app — the localization module has no way to know a host app's own user/profile schema (see Module.WithAccountCountryResolver).

type AuthenticatedUserIDFunc

type AuthenticatedUserIDFunc func(c *gin.Context) (userID string, ok bool)

AuthenticatedUserIDFunc extracts the current request's authenticated user ID, if any (e.g. from whatever the auth middleware set on the gin context). Supplied by the host app for the same reason as AccountCountryResolverFunc.

type ClientIPFunc

type ClientIPFunc func(c *gin.Context) string

ClientIPFunc extracts the trusted client IP (or, for a CloudflareHeaderResolver, the pre-extracted header value) from the request. Deliberately not hardcoded to gin's c.ClientIP() — that call is only trustworthy once the host app has correctly configured SetTrustedProxies/ForwardedByClientIP for its actual proxy/LB, and this middleware should not silently paper over a host app that hasn't done so.

type CountryResolver

type CountryResolver interface {
	Resolve(ctx context.Context, code string) (*models.Country, error)
	Default(ctx context.Context) (*models.Country, error)
}

CountryResolver is a local, structural copy of the parent localization package's CountryResolver interface — defined here rather than imported to avoid an import cycle (the parent localization package imports this middleware package to wire it up in Module.Initialize). Any localization.CountryResolver implementation satisfies this automatically.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL