Documentation
¶
Index ¶
- Variables
- func CheckMethod(w http.ResponseWriter, r *http.Request, method string) bool
- func CheckMethodOneOf(w http.ResponseWriter, r *http.Request, methods ...string) bool
- func DecodeBase64(s string) ([]byte, error)
- func DecodeJSON(r *http.Request, v any) error
- func DecodeJSONStrict(r *http.Request, v any) error
- func EncodeBase64(data []byte) string
- func ExtractAPIKey(r *http.Request) string
- func ExtractBasicAuth(r *http.Request) (username, password string, ok bool)
- func ExtractBearerToken(r *http.Request) string
- func ExtractNamespaceHeader(r *http.Request) string
- func ExtractWalletHeader(r *http.Request) string
- func HasAuthHeader(r *http.Request) bool
- func IsEmpty(s string) bool
- func IsJWT(token string) bool
- func IsNotEmpty(s string) bool
- func NormalizeWalletAddress(wallet string) string
- func QueryParam(r *http.Request, key, defaultValue string) string
- func QueryParamBool(r *http.Request, key string, defaultValue bool) bool
- func QueryParamInt(r *http.Request, key string, defaultValue int) int
- func ReadBody(r *http.Request, maxBytes int64) ([]byte, error)
- func RequireNotEmpty(w http.ResponseWriter, value, fieldName string) bool
- func ValidateCID(cid string) bool
- func ValidateDMapName(dmap string) bool
- func ValidateNamespace(ns string) bool
- func ValidateTopicName(topic string) bool
- func ValidateWalletAddress(wallet string) bool
- func WriteError(w http.ResponseWriter, code int, msg string)
- func WriteHTTPError(w http.ResponseWriter, err *HTTPError)
- func WriteJSON(w http.ResponseWriter, code int, v any)
- func WriteSuccess(w http.ResponseWriter)
- func WriteSuccessWithData(w http.ResponseWriter, data map[string]any)
- type HTTPError
Constants ¶
This section is empty.
Variables ¶
var ( ErrBadRequest = NewHTTPError(http.StatusBadRequest, "bad request") ErrForbidden = NewHTTPError(http.StatusForbidden, "forbidden") ErrNotFound = NewHTTPError(http.StatusNotFound, "not found") ErrMethodNotAllowed = NewHTTPError(http.StatusMethodNotAllowed, "method not allowed") ErrConflict = NewHTTPError(http.StatusConflict, "conflict") ErrInternalServerError = NewHTTPError(http.StatusInternalServerError, "internal server error") )
Common HTTP errors
Functions ¶
func CheckMethod ¶
CheckMethod validates that the request method matches the expected method. If it doesn't match, it writes a 405 Method Not Allowed error and returns false.
func CheckMethodOneOf ¶
CheckMethodOneOf validates that the request method is one of the allowed methods. If it doesn't match any, it writes a 405 Method Not Allowed error and returns false.
func DecodeBase64 ¶
DecodeBase64 decodes a base64-encoded string to bytes.
func DecodeJSON ¶
DecodeJSON decodes the request body as JSON into the provided value. Returns an error if decoding fails.
func DecodeJSONStrict ¶
DecodeJSONStrict decodes the request body as JSON with strict validation. It disallows unknown fields and returns an error if any are present.
func EncodeBase64 ¶
EncodeBase64 encodes bytes to a base64-encoded string.
func ExtractAPIKey ¶
ExtractAPIKey extracts an API key from various sources: 1. X-API-Key header (highest priority) 2. Authorization header with "ApiKey" scheme 3. Authorization header with "Bearer" scheme (if not a JWT) 4. Query parameter "api_key" 5. Query parameter "token"
Note: Bearer tokens that look like JWTs (have 2 dots) are skipped.
func ExtractBasicAuth ¶
ExtractBasicAuth extracts username and password from Basic authentication. Returns empty strings if Basic auth is not present or invalid.
func ExtractBearerToken ¶
ExtractBearerToken extracts a Bearer token from the Authorization header. Returns an empty string if no Bearer token is found.
func ExtractNamespaceHeader ¶
ExtractNamespaceHeader extracts the namespace from the X-Namespace header.
func ExtractWalletHeader ¶
ExtractWalletHeader extracts the wallet address from the X-Wallet header.
func HasAuthHeader ¶
HasAuthHeader checks if the request has any Authorization header.
func IsNotEmpty ¶
IsNotEmpty checks if a string is not empty after trimming whitespace.
func NormalizeWalletAddress ¶
NormalizeWalletAddress normalizes a wallet address by removing "0x" prefix and converting to lowercase.
func QueryParam ¶
QueryParam returns the value of a query parameter, or defaultValue if not present.
func QueryParamBool ¶
QueryParamBool returns the boolean value of a query parameter. Returns true if the parameter value is "true", "1", "yes", or "on" (case-insensitive). Returns defaultValue if the parameter is not present or has an invalid value.
func QueryParamInt ¶
QueryParamInt returns the integer value of a query parameter, or defaultValue if not present or invalid.
func ReadBody ¶
ReadBody reads the entire request body up to maxBytes. Returns the body bytes or an error if reading fails.
func RequireNotEmpty ¶
func RequireNotEmpty(w http.ResponseWriter, value, fieldName string) bool
RequireNotEmpty checks if a string value is empty after trimming whitespace. If empty, it writes a 400 Bad Request error with the field name and returns false.
func ValidateCID ¶
ValidateCID checks if a string is a valid IPFS CID.
func ValidateDMapName ¶
func ValidateNamespace ¶
func ValidateTopicName ¶
func ValidateWalletAddress ¶
func WriteError ¶
func WriteError(w http.ResponseWriter, code int, msg string)
WriteError writes a standardized JSON error response. The response format is: {"error": "message"}
func WriteHTTPError ¶
func WriteHTTPError(w http.ResponseWriter, err *HTTPError)
WriteHTTPError writes an HTTPError to the response.
func WriteJSON ¶
func WriteJSON(w http.ResponseWriter, code int, v any)
WriteJSON writes a JSON response with the given status code. It sets the Content-Type header to application/json and encodes the value as JSON. Any encoding errors are silently ignored (best-effort).
func WriteSuccess ¶
func WriteSuccess(w http.ResponseWriter)
WriteSuccess writes a standardized JSON success response. The response format is: {"status": "ok"}
func WriteSuccessWithData ¶
func WriteSuccessWithData(w http.ResponseWriter, data map[string]any)
WriteSuccessWithData writes a success response with additional data fields. The response format is: {"status": "ok", ...data}
Types ¶
type HTTPError ¶
HTTPError represents a structured HTTP error with a status code and message.
func NewHTTPError ¶
NewHTTPError creates a new HTTP error with the given code and message.