Documentation
¶
Index ¶
- Constants
- func BuildLinkHeaders(r *http.Request, w http.ResponseWriter, serverURLWithProtocol, path string, ...) error
- func ErrInvalidRequest(err error) render.Renderer
- func ErrRender(err error) render.Renderer
- func GetBody(w http.ResponseWriter, r *http.Request, record interface{}) error
- func HasCode(err error, code int) bool
- func NewHTTPError(err error, code int) error
- func Param(r *http.Request, name string) string
- func QS(r *http.Request, name string) string
- func QSAll(r *http.Request, name string) []string
- func QSDefault(r *http.Request, name string, defaultValue string) string
- func Reply(r *http.Request, w http.ResponseWriter, data interface{}, statusCode int)
- func ReplyBytes(r *http.Request, w http.ResponseWriter, data []byte, statusCode int, ...)
- func ReplyBytesGzip(r *http.Request, w http.ResponseWriter, data []byte, statusCode int, ...)
- func ReplyErr(w http.ResponseWriter, r *http.Request, err error)
- func ReplyGzip(r *http.Request, w http.ResponseWriter, data interface{}, statusCode int, ...)
- func ReplyRaw(r *http.Request, w http.ResponseWriter, src io.Reader, statusCode int, ...)
- func SetResponseHeaders(w http.ResponseWriter, headers map[string]string)
- func WrapRender(w http.ResponseWriter, r *http.Request, v render.Renderer)
- type Cursor
- type HTTPErrorCoder
- type ListResponse
- type PaginationOptions
- type ResponseError
- type Result
- type SingleResponse
Constants ¶
const ( DefaultLimit = 1000 DefaultCursor = "" ParamCursor = "cursor" ParamLimit = "limit" ParamSort = "sort" ParamSortField = "sortField" )
const ( MaxGzipSize = 1024 * 1025 * 5 ContentTypeJSON = "application/json" ContentTypeGzip = "gzip" HeaderContentType = "Content-Type" HeaderContentEncoding = "Content-Encoding" )
const (
MaxBodySize = 1048576 // 1MB
)
Variables ¶
This section is empty.
Functions ¶
func BuildLinkHeaders ¶
func BuildLinkHeaders(r *http.Request, w http.ResponseWriter, serverURLWithProtocol, path string, cursor Cursor) error
BuildLinkHeaders adds pagination Link headers to the HTTP response.
func ErrInvalidRequest ¶
ErrInvalidRequest creates a ResponseError for invalid requests.
func GetBody ¶
func GetBody(w http.ResponseWriter, r *http.Request, record interface{}) error
GetBody deserializes the request body into the provided record or returns an error.
func NewHTTPError ¶
NewHTTPError creates a new ResponseError with the given error and HTTP status code.
func Reply ¶
func Reply(r *http.Request, w http.ResponseWriter, data interface{}, statusCode int)
Reply sends a JSON response with the given data and status code.
func ReplyBytes ¶
func ReplyBytes(r *http.Request, w http.ResponseWriter, data []byte, statusCode int, contentType string)
ReplyBytes sends a response with the given byte data and status code.
func ReplyBytesGzip ¶
func ReplyBytesGzip(r *http.Request, w http.ResponseWriter, data []byte, statusCode int, contentType string)
ReplyBytesGzip sends a gzipped response with the given byte data and status code.
func ReplyErr ¶
func ReplyErr(w http.ResponseWriter, r *http.Request, err error)
ReplyErr sends an error response with the given error.
func ReplyGzip ¶
func ReplyGzip(r *http.Request, w http.ResponseWriter, data interface{}, statusCode int, pretty bool)
ReplyGzip sends a gzipped JSON response with the given data and status code.
func ReplyRaw ¶
func ReplyRaw(r *http.Request, w http.ResponseWriter, src io.Reader, statusCode int, contentType string)
ReplyRaw sends a raw response with the given reader and status code.
func SetResponseHeaders ¶
func SetResponseHeaders(w http.ResponseWriter, headers map[string]string)
SetResponseHeaders sets the given headers on the response.
func WrapRender ¶
WrapRender wraps the render.Render function and handles errors.
Types ¶
type HTTPErrorCoder ¶
type HTTPErrorCoder interface {
HTTPCode() int
}
type ListResponse ¶
type ListResponse[DataType any] struct { Status Result `json:"status"` Cursor Cursor `json:"cursor"` Count int `json:"count"` Data []DataType `json:"data"` }
ListResponse simple class to make standard response objects for list of elements.
type PaginationOptions ¶
func GetPagingOpts ¶
func GetPagingOpts(r *http.Request) PaginationOptions
GetPagingOpts extracts pagination options from the HTTP request.
type ResponseError ¶
type ResponseError struct {
CallerInfo string `json:"-"` // typically "$file:$line"
Err error `json:"-"` // low-level runtime error
HTTPStatusCode int `json:"-"` // http response status code
StatusText string `json:"status"` // user-level status message
AppCode *int64 `json:"code,omitempty"` // application-specific error code
ErrorText string `json:"error,omitempty"` // application-level error message, for debugging
}
func GetResponseError ¶
func GetResponseError(err error) (re *ResponseError, ok bool)
GetResponseError extracts a ResponseError from a given error.
func NewErrNotFound ¶
func NewErrNotFound() *ResponseError
NewErrNotFound creates a ResponseError for resource not found errors.
func NewErrServer ¶
func NewErrServer() *ResponseError
NewErrServer creates a ResponseError for internal server errors.
func (*ResponseError) AsFields ¶
func (e *ResponseError) AsFields() map[string]interface{}
AsFields returns the ResponseError as zerolog.Fields for structured logging.
func (*ResponseError) Error ¶
func (e *ResponseError) Error() string
Error returns a string representation of the ResponseError.
func (*ResponseError) HTTPCode ¶
func (e *ResponseError) HTTPCode() int
func (*ResponseError) Render ¶
func (e *ResponseError) Render(w http.ResponseWriter, r *http.Request) error
Render sets the HTTP status code for the response.
type SingleResponse ¶
type SingleResponse[DataType any] struct { Status Result `json:"status"` Data DataType `json:"data"` }
SingleResponse simple class to make standard response objects for single element gets