Documentation
¶
Overview ¶
Package geolocation provides framework-agnostic geolocation extraction from Cloudflare headers and user agent parsing for browser, OS, device, and language information.
Index ¶
- func AddCountryData(countryCode string, data CountryData)
- func FakeCloudflareHeaders(countryCode string, options *SimulationOptions) map[string]string
- func GetAvailableCountries() []string
- func GetCookie(r *http.Request, name string) string
- func GetLanguageForCountry(r *http.Request, cfg *Config, countryCode string, ...) string
- func HTTPMiddleware(next http.Handler) http.Handler
- func IsLocalDevelopment(r *http.Request) bool
- func RandomCountry() string
- func SetCookie(w http.ResponseWriter, name, value string, opts *http.Cookie)
- func ShouldSetLanguage(r *http.Request, cookieName string) bool
- func Simulate(countryCode string, options *SimulationOptions) *http.Request
- func SimulateRequest(countryCode string, options *SimulationOptions) *http.Request
- type ClientInfo
- type Config
- type CountryData
- type GeoInfo
- type GeolocationSimulator
- type LanguageInfo
- type Location
- type Resolution
- type SimulationOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddCountryData ¶
func AddCountryData(countryCode string, data CountryData)
AddCountryData adds custom country data for simulation.
func FakeCloudflareHeaders ¶
func FakeCloudflareHeaders(countryCode string, options *SimulationOptions) map[string]string
FakeCloudflareHeaders generates fake Cloudflare headers for a specific country.
func GetAvailableCountries ¶
func GetAvailableCountries() []string
GetAvailableCountries returns a list of available country codes for simulation.
func GetCookie ¶
GetCookie retrieves a named cookie value from the request. Returns empty string if not found.
func GetLanguageForCountry ¶
func GetLanguageForCountry(r *http.Request, cfg *Config, countryCode string, availableSiteLanguages []string) string
GetLanguageForCountry returns the best language for a given country code, considering browser languages and available site languages.
Logic: 1. If browser preferred language matches a country language and is available, use it 2. Check all browser languages for a match with available languages 3. Use the first country language as fallback 4. Returns empty string if no match found
func HTTPMiddleware ¶
HTTPMiddleware attaches geolocation info to the request context.
func IsLocalDevelopment ¶
IsLocalDevelopment checks if we're in a local development environment. Returns true for localhost, local IPs, or missing Cloudflare headers.
func RandomCountry ¶
func RandomCountry() string
RandomCountry returns a random country code for simulation.
func SetCookie ¶
func SetCookie(w http.ResponseWriter, name, value string, opts *http.Cookie)
SetCookie sets a cookie with the given name and value on the response writer. You can pass additional options via the http.Cookie struct.
func ShouldSetLanguage ¶
ShouldSetLanguage returns true if the language cookie should be set (i.e., if no language cookie exists).
func Simulate ¶
func Simulate(countryCode string, options *SimulationOptions) *http.Request
Simulate creates a geolocation-enabled HTTP request with simulated Cloudflare headers for local development and testing.
Example:
req := geolocation.Simulate("DE", &geolocation.SimulationOptions{ UserAgent: "Custom Test Agent", }) info := geolocation.GetGeoInfo(req)
func SimulateRequest ¶
func SimulateRequest(countryCode string, options *SimulationOptions) *http.Request
SimulateRequest creates a new HTTP request with simulated Cloudflare headers.
Types ¶
type ClientInfo ¶
type ClientInfo struct { BrowserName string // e.g., Chrome, Firefox BrowserVersion string // e.g., 123.0.0.0 OS string // e.g., Windows NT 10.0 Device string // e.g., Mobile, Desktop, Tablet }
ClientInfo holds browser, OS, and device information parsed from the User-Agent header.
func ParseClientInfo ¶
func ParseClientInfo(r *http.Request) *ClientInfo
ParseClientInfo parses the User-Agent header for browser, OS, and device info.
Example:
info := geolocation.ParseClientInfo(r) fmt.Println(info.BrowserName, info.OS, info.Device)
type Config ¶
type Config struct { DefaultLanguage string `json:"default_language" yaml:"default_language"` CountryToLanguageMap map[string][]string `json:"country_to_language_map" yaml:"country_to_language_map"` CookieName string `json:"cookie_name" yaml:"cookie_name"` }
Config holds module configuration, including country-to-language mapping, defaults, and cookie name.
func LoadConfig ¶
LoadConfig loads configuration from a JSON or YAML file. The format is determined by the file extension (.json or .yaml/.yml).
func (*Config) ActiveLanguage ¶
ActiveLanguage returns the first language for a given country code, or the default if not mapped.
func (*Config) ActiveLanguages ¶
ActiveLanguages returns the list of languages for a given country code, or the default if not mapped.
type CountryData ¶
type CountryData struct { Country string `json:"country"` IPRanges []string `json:"ip_ranges"` Languages []string `json:"languages"` Timezone string `json:"timezone"` }
CountryData holds simulation data for a specific country.
type GeoInfo ¶
type GeoInfo struct { CountryCode string `json:"country_code"` IP string `json:"ip"` PreferredLanguage string `json:"preferred_language"` AllLanguages []string `json:"all_languages"` OS string `json:"os"` Browser string `json:"browser"` BrowserVersion string `json:"browser_version"` Device string `json:"device"` Resolution Resolution `json:"resolution"` }
GeoInfo holds all geolocation and client information.
func GetGeoInfo ¶
GetGeoInfo returns all geolocation and client information in a single struct.
Example:
info := geolocation.GetGeoInfo(r) fmt.Printf("Country: %s, Browser: %s, Device: %s", info.CountryCode, info.Browser, info.Device)
type GeolocationSimulator ¶
type GeolocationSimulator struct{}
GeolocationSimulator provides methods to simulate Cloudflare geolocation headers for local development when actual Cloudflare infrastructure is not available.
type LanguageInfo ¶
type LanguageInfo struct { Default string // The default (first) language Supported []string // All languages in order of preference }
LanguageInfo holds the user's preferred and supported languages from Accept-Language.
func ParseLanguageInfo ¶
func ParseLanguageInfo(r *http.Request) *LanguageInfo
ParseLanguageInfo parses the Accept-Language header for language preferences.
Example:
lang := geolocation.ParseLanguageInfo(r) fmt.Println(lang.Default, lang.Supported)
type Location ¶
type Location struct { IP string // The user's public IP address (from CF-Connecting-IP) Country string // The user's country code (from CF-IPCountry) }
Location represents a geolocation result, typically extracted from Cloudflare headers.
func FromContext ¶
FromContext retrieves the Location from context.
func FromRequest ¶
FromRequest extracts geolocation info from Cloudflare headers in the request.
Example:
loc := geolocation.FromRequest(r) fmt.Println(loc.IP, loc.Country)
type Resolution ¶
type Resolution struct { Width int // Screen width in pixels Height int // Screen height in pixels }
Resolution holds screen resolution information.
func GetResolution ¶
func GetResolution(r *http.Request) Resolution
GetResolution retrieves screen resolution from custom headers (if set by frontend JS).