respondkit

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrorGRPC

func ErrorGRPC[T any](ctx context.Context, err error) (T, error)

ErrorGRPC tries to map err to errkit.Error and based on result writes standard gRPC error with status statusCode to the response writer.

func ErrorHTTP

func ErrorHTTP(w http.ResponseWriter, r *http.Request, err error)

ErrorHTTP tries to map err to errkit.Error and based on result writes standard HTTP error with status statusCode to the response writer.

func HTML

func HTML(w http.ResponseWriter, r *http.Request, v []byte, options ...Option)

HTML tries to encode v into json representation and write it to response writer.

func JSON

func JSON(w http.ResponseWriter, r *http.Request, v any, options ...Option)

JSON tries to encode v into json representation and write it to response writer.

func SetGRPCErrorResponder

func SetGRPCErrorResponder(responder GRPCErrorResponder)

SetGRPCErrorResponder sets the given responder as errGRPCResponder.

func SetHTMLTemplateProvider

func SetHTMLTemplateProvider(templater HTMLTemplateProvider)

SetHTMLTemplateProvider sets the given htmlTemplater as htmlTemplater.

func SetHTTPErrorResponder

func SetHTTPErrorResponder(responder HTTPErrorResponder)

SetHTTPErrorResponder sets the given responder as errHTTPResponder.

func Status

func Status(w http.ResponseWriter, _ *http.Request, statusCode int, options ...Option)

Status writes an HTTP status to the w http.ResponseWriter.

func TEXT

func TEXT(w http.ResponseWriter, r *http.Request, v []byte, options ...Option)

TEXT tries to write v to response writer.

func TemplateHTML

func TemplateHTML(w http.ResponseWriter, r *http.Request, name string, v any, options ...Option)

Types

type GRPCErrorResponder

type GRPCErrorResponder func(err error) error

GRPCErrorResponder represents a function type that handles and responds to GRPC errors.

type HTMLTemplateProvider

type HTMLTemplateProvider interface {
	// Template renders the HTML templates by given name.
	Template(ctx context.Context, name string) (*template.Template, error)
}

HTMLTemplateProvider wraps a Template method to render requested HTML template.

type HTTPErrorResponder

type HTTPErrorResponder func(w http.ResponseWriter, err error)

HTTPErrorResponder represents a function which should be called to respond with an error on HTTP call.

type Option

type Option func(o *ResponseOptions)

func WithHeader

func WithHeader(key, value string) Option

func WithStatus

func WithStatus(status int) Option

type ResponseOptions

type ResponseOptions struct {
	// contains filtered or unexported fields
}

ResponseOptions represents the options for an HTTP response.

func Options

func Options(w http.ResponseWriter, options ...Option) *ResponseOptions

Options returns a pointer to a new ResponseOptions object with default values and applies the given options to it.

Jump to

Keyboard shortcuts

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