Documentation
¶
Index ¶
- func AppURL(path string) string
- func ComparePassword(hashedPassword, password string) bool
- func CurrentMonthPeriod() (time.Time, time.Time)
- func DecodeAndValidate(r *http.Request, v any) error
- func DecodeJSON(r *http.Request, v any) error
- func Decrypt(secret, ciphertext string) (string, error)
- func DeleteCookie(w http.ResponseWriter, name string)
- func DeleteDir(dir string) error
- func DirExists(path string) bool
- func Encrypt(secret, plaintext string) (string, error)
- func EnsureDir(dirName string, mode int) error
- func FileExists(path string) bool
- func FormatValidationErrors(err error) string
- func GenerateSecureToken(length int) (string, error)
- func GenerateUUID() (string, error)
- func GetBaseDir(dirName string) string
- func GetCookie(r *http.Request, name string) string
- func GetValidator() *validator.Validate
- func GravatarURL(email string, size int) string
- func HandleFromName(name string, maxLength int) string
- func HasCookie(r *http.Request, name string) bool
- func HashPassword(password string) (string, error)
- func JSONRawFromString(raw *string) json.RawMessage
- func JSONSliceFromString[T any](raw *string) []T
- func LoadConfigs(path string) error
- func MapChecksum(m map[string]any) (string, error)
- func ParsePagination(r *http.Request) (limit, offset int)
- func ParseQueryLabels(r *http.Request) map[string]string
- func RandInt(min, max int) (int, error)
- func RandomHandle(min, max int) (string, error)
- func RandomHash() (string, error)
- func RemoveLabelFromJSON(labels *string, label string) (*string, bool)
- func SetCookie(w http.ResponseWriter, name, value string, options *CookieOptions)
- func ValidateStruct(s interface{}) error
- func WriteJSON(w http.ResponseWriter, statusCode int, data interface{}) error
- func WriteValidationError(w http.ResponseWriter, err error)
- type CookieOptions
- type ValidationError
- type ValidationErrors
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ComparePassword ¶
ComparePassword compares a bcrypt hashed password with a plain text password.
func CurrentMonthPeriod ¶
CurrentMonthPeriod returns the UTC start and end of the current calendar month.
func DecodeAndValidate ¶
DecodeAndValidate decodes JSON and validates the struct in one step.
func DecodeJSON ¶
DecodeJSON reads and decodes JSON from the request body.
func DeleteCookie ¶
func DeleteCookie(w http.ResponseWriter, name string)
DeleteCookie deletes a cookie by setting its MaxAge to -1.
func FileExists ¶
FileExists reports whether the named file exists
func FormatValidationErrors ¶
FormatValidationErrors returns the first validation error message
func GenerateSecureToken ¶
GenerateSecureToken returns a cryptographically secure random token.
func GenerateUUID ¶
GenerateUUID returns a new random UUID string.
func GetValidator ¶
GetValidator returns the shared validator instance.
func GravatarURL ¶
GravatarURL returns the Gravatar avatar URL for the given email.
func HandleFromName ¶
HandleFromName derives a dash-separated lowercase handle from a display name.
func HashPassword ¶
HashPassword generates a bcrypt hash from a plain text password at the default cost.
func JSONRawFromString ¶
func JSONRawFromString(raw *string) json.RawMessage
JSONRawFromString returns stored JSON text as a RawMessage for API responses.
func JSONSliceFromString ¶
JSONSliceFromString unmarshals a JSON-encoded slice from a stored string pointer.
func LoadConfigs ¶
LoadConfigs load configs for testing purposes using viper
func MapChecksum ¶
MapChecksum returns a deterministic SHA-256 hex checksum of a map.
func ParsePagination ¶
ParsePagination parses limit and offset from query parameters.
func ParseQueryLabels ¶
ParseQueryLabels extracts label pairs from query parameters, skipping reserved keys.
func RandomHandle ¶
RandomHandle returns a handle like abcd-1234 (min–max letters, dash, min–max digits).
func RandomHash ¶
RandomHash returns a 12-character cryptographically secure random hex string.
func RemoveLabelFromJSON ¶
RemoveLabelFromJSON removes a label from a JSON-encoded string array.
func SetCookie ¶
func SetCookie(w http.ResponseWriter, name, value string, options *CookieOptions)
SetCookie sets a cookie with the given name, value, and options.
func ValidateStruct ¶
func ValidateStruct(s interface{}) error
ValidateStruct validates the struct and returns any validation errors.
func WriteJSON ¶
func WriteJSON(w http.ResponseWriter, statusCode int, data interface{}) error
WriteJSON writes a value as JSON to an HTTP response.
func WriteValidationError ¶
func WriteValidationError(w http.ResponseWriter, err error)
WriteValidationError writes a decode or validation error as a 400 JSON response.
Types ¶
type CookieOptions ¶
type CookieOptions struct {
// MaxAge is the cookie age in seconds; zero is a session cookie and negative deletes it now.
MaxAge int
// Path specifies the cookie path. Defaults to "/".
Path string
// Domain specifies the cookie domain.
Domain string
// Secure indicates if the cookie should only be sent over HTTPS.
Secure bool
// HTTPOnly makes the cookie inaccessible to JavaScript.
HTTPOnly bool
// SameSite is the cookie's SameSite attribute, such as SameSiteLaxMode.
SameSite http.SameSite
}
CookieOptions holds configuration for cookie creation.
func DefaultCookieOptions ¶
func DefaultCookieOptions() *CookieOptions
DefaultCookieOptions returns sensible default options for cookies.
func SecureCookieOptions ¶
func SecureCookieOptions() *CookieOptions
SecureCookieOptions returns secure cookie options for production use.
type ValidationError ¶
type ValidationError struct {
Field string `json:"field"`
Message string `json:"message"`
Tag string `json:"tag,omitempty"`
Value string `json:"value,omitempty"`
}
ValidationError is one field error.
type ValidationErrors ¶
type ValidationErrors struct {
Errors []ValidationError `json:"errors"`
}
ValidationErrors is a list of validation errors.