response

package
v0.0.0-...-10f60b2 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2025 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var EmptySyncResponse = &syncResponse{success: true, metadata: make(map[string]any)}

EmptySyncResponse represents an empty syncResponse.

Functions

func EtagCheck

func EtagCheck(r *http.Request, data any) error

EtagCheck validates the hash of the current state with the hash provided by the client.

func EtagHash

func EtagHash(data any) (string, error)

EtagHash hashes the provided data and returns the sha256.

func IsNotFoundError

func IsNotFoundError(err error) bool

IsNotFoundError returns true if the error is considered a Not Found error.

func With

func With(handler HandlerFunc, middlewares ...func(next HandlerFunc) HandlerFunc) http.HandlerFunc

Types

type HandlerFunc

type HandlerFunc func(r *http.Request) Response

type Response

type Response interface {
	Render(w http.ResponseWriter) error
	String() string
	Code() int
}

Response represents an API response.

func BadRequest

func BadRequest(err error) Response

BadRequest returns a bad request response (400) with the given error.

func Forbidden

func Forbidden(err error) Response

Forbidden returns a forbidden response (403) with the given error.

func InternalError

func InternalError(err error) Response

InternalError returns an internal error response (500) with the given error.

func ManualResponse

func ManualResponse(hook func(w http.ResponseWriter) error) Response

ManualResponse creates a new manual response responder.

func NotFound

func NotFound(err error) Response

NotFound returns a not found response (404) with the given error.

func NotImplemented

func NotImplemented(err error) Response

NotImplemented returns a not implemented response (501) with the given error.

func PreconditionFailed

func PreconditionFailed(err error) Response

PreconditionFailed returns a precondition failed response (412) with the given error.

func ReadCloserResponse

func ReadCloserResponse(r *http.Request, rc io.ReadCloser, compress bool, filename string, fileSize int, headers map[string]string) Response

ReadCloserResponse returns a new file taking the file content from a io.ReadCloser. If the fileSize is unknown, -1 should be passed in order to omit the Content-Length HTTP header. If compress is set to true and the client provides the HTTP header "Accept-Encoding: gzip", the file is streamed with "Content-Encoding: gzip". If compress is set to true but the client does not offer "Content-Encoding: gzip", the file is returned as .gz file.

func SmartError

func SmartError(err error) Response

SmartError returns the right error message based on err. It uses the stdlib errors package to unwrap the error and find the cause.

func SyncResponse

func SyncResponse(success bool, metadata any) Response

SyncResponse returns a new syncResponse with the success and metadata fields set to the provided values.

func SyncResponseETag

func SyncResponseETag(success bool, metadata any, etag any) Response

func SyncResponseLocation

func SyncResponseLocation(success bool, metadata any, location string) Response

SyncResponseLocation returns a new syncResponse with a location.

func SyncResponsePlain

func SyncResponsePlain(success bool, compress bool, metadata string) Response

SyncResponsePlain return a new syncResponse with plaintext.

func Unauthorized

func Unauthorized(err error) Response

Unauthorized return an unauthorized response (401) with the given error.

func Unavailable

func Unavailable(err error) Response

Unavailable return an unavailable response (503) with the given error.

Jump to

Keyboard shortcuts

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