httptools

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2024 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MessageInternalServerError = "the server encountered a problem and could not process your request"
	MessageTooManyRequests     = "rate limit exceeded"
	MessageForbidden           = "user has no access to this resource"
)

Variables

View Source
var (
	ErrRecordNotFound    = errors.New("record not found")
	ErrRecordUniqueValue = errors.New("record unique value already used")
)

Functions

func BadRequestResponse

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

func ConflictResponse

func ConflictResponse(
	w http.ResponseWriter,
	r *http.Request,
	err error,
	resourceName string,
	identifierValue any,
	jsonField string,
)

func ErrorResponse

func ErrorResponse(w http.ResponseWriter,
	_ *http.Request, status int, message any)

func FailedValidationResponse

func FailedValidationResponse(
	w http.ResponseWriter,
	r *http.Request,
	errors map[string]string,
)

func ForbiddenResponse

func ForbiddenResponse(w http.ResponseWriter, r *http.Request)

func NotFoundResponse

func NotFoundResponse(
	w http.ResponseWriter,
	r *http.Request,
	err error,
	resourceName string,
	identifierValue any,
	jsonField string,
)

func RateLimitExceededResponse

func RateLimitExceededResponse(w http.ResponseWriter,
	r *http.Request)

func ReadJSON

func ReadJSON(body io.Reader, dst any) error

func Serve

func Serve(port int, handler http.Handler, environment string) error

func ServerErrorResponse

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

func UnauthorizedResponse

func UnauthorizedResponse(w http.ResponseWriter,
	r *http.Request, message string)

func WSErrorResponse

func WSErrorResponse(
	w http.ResponseWriter,
	r *http.Request,
	conn *websocket.Conn,
	beforeClosingFunc func(conn *websocket.Conn),
	err error,
)

func WSUpgradeErrorResponse

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

func WriteCSV

func WriteCSV(w http.ResponseWriter, filename string, data [][]string) error

func WriteJSON

func WriteJSON(
	w http.ResponseWriter,
	status int,
	data any,
	headers http.Header,
) error

Types

type ErrorDto

type ErrorDto struct {
	Status  int    `json:"status"`
	Error   string `json:"error"`
	Message any    `json:"message"`

} //	@name	ErrorDto

type ISubjectMessageDto

type ISubjectMessageDto interface {
	validate.IValidatedType
	GetSubject() string
}

type OnCloseCallback

type OnCloseCallback = func(conn *websocket.Conn)

type ResponseWriter

type ResponseWriter struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

func NewResponseWriter

func NewResponseWriter(w http.ResponseWriter) *ResponseWriter

func (*ResponseWriter) Hijack

func (w *ResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error)

func (ResponseWriter) StatusCode

func (w ResponseWriter) StatusCode() int

func (*ResponseWriter) WriteHeader

func (w *ResponseWriter) WriteHeader(statusCode int)

type WebsocketHandler

type WebsocketHandler[T ISubjectMessageDto] struct {
	// contains filtered or unexported fields
}

func CreateWebsocketHandler

func CreateWebsocketHandler[T ISubjectMessageDto](url string) WebsocketHandler[T]

func (*WebsocketHandler[T]) AddSubjectHandler

func (h *WebsocketHandler[T]) AddSubjectHandler(
	subject string,
	handler func(
		w http.ResponseWriter,
		r *http.Request,
		conn *websocket.Conn,
		msg T,
	),
)

func (WebsocketHandler[T]) GetHandler

func (h WebsocketHandler[T]) GetHandler() http.HandlerFunc

func (*WebsocketHandler[T]) SetOnCloseCallback

func (h *WebsocketHandler[T]) SetOnCloseCallback(callback OnCloseCallback)

Jump to

Keyboard shortcuts

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