util

package
v0.30.2 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConstructURL

func ConstructURL(domain, path string) string

ConstructURL builds a proper URL from domain and path components

func ExtractPathFromURL

func ExtractPathFromURL(fullURL string) string

ExtractPathFromURL extracts just the path component from a full URL

func GetClientIP

func GetClientIP(r *http.Request) string

GetClientIP extracts the client IP address from a request, respecting proxy headers (X-Forwarded-For, X-Real-IP).

func IsSignificantRedirect

func IsSignificantRedirect(originalURL, redirectURL string) bool

IsSignificantRedirect checks if a redirect URL is meaningfully different from the original. Only the host and path are compared; query parameters and fragments are ignored. Returns false for trivial redirects like:

  • HTTP to HTTPS on same domain/path
  • www to non-www (or vice versa) on same path
  • Trailing slash differences
  • Default port differences (e.g., :443 for HTTPS, :80 for HTTP)

Returns true for redirects to different domains or different paths.

func NormaliseDomain

func NormaliseDomain(domain string) string

NormaliseDomain removes http/https prefix and www. from domain

func NormaliseURL

func NormaliseURL(rawURL string) string

NormaliseURL ensures a URL has proper https:// scheme and validates format

func SanitiseForJSON

func SanitiseForJSON(s string) string

SanitiseForJSON strips control characters (tabs, newlines, etc.) from a string so it can be safely embedded in a JSON value via a Go template. Supabase's email templates render {{ .Data.* }} without JSON-escaping, so any control character in user_metadata breaks the Loops payload.

func ValidateDomain

func ValidateDomain(domain string) error

ValidateDomain checks if a domain string is a valid domain format. Uses golang.org/x/net/publicsuffix for robust validation against the Public Suffix List. Returns an error describing why the domain is invalid, or nil if valid.

Types

type RequestMeta

type RequestMeta struct {
	IP        string
	UserAgent string
	Browser   string
	OS        string
	Device    string // e.g. "Chrome on macOS"
	City      string // From Cloudflare cf-ipcity header
	Region    string // From Cloudflare cf-region header
	Country   string // From Cloudflare cf-ipcountry header (ISO code)
	Timezone  string // From Cloudflare cf-timezone header
	Location  string // Formatted e.g. "Melbourne, Victoria, Australia"
	Timestamp time.Time
}

RequestMeta contains metadata extracted from an HTTP request. Useful for audit logging and contextual email content.

func ExtractRequestMeta

func ExtractRequestMeta(r *http.Request) *RequestMeta

ExtractRequestMeta extracts client metadata from an HTTP request. Location fields (City, Region, Country, Timezone) require the Cloudflare "Add visitor location headers" managed transform to be enabled.

func (*RequestMeta) FormattedTimestamp

func (m *RequestMeta) FormattedTimestamp() string

FormattedTimestamp returns the timestamp in a human-readable format.

Jump to

Keyboard shortcuts

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