uhttp

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2024 License: GPL-3.0 Imports: 12 Imported by: 0

README

uHTTP

This is a simple http utility package for sending, receiving and handling http requests and responses.

Documentation

Index

Constants

View Source
const (
	MsgNotFound         = "Not found"
	MsgMethodNotAllowed = "Method not allowed"
	MsgUnauthorized     = "Unauthorized"
	MsgBadRequest       = "Bad request"

	HeaderContentType = "Content-Type"

	ContentTypeJSON = "application/json"
)

Variables

This section is empty.

Functions

func AuthHeaderFromContext

func AuthHeaderFromContext(ctx context.Context) string

AuthHeaderFromContext returns the Authorization HTTP header value from the provided context. If the header was not set it returns an empty string.

func AuthHeaderToContext

func AuthHeaderToContext(ctx context.Context, r *http.Request) context.Context

AuthHeaderToContext copies the Authorization HTTP header into the provided context.

func AuthHeaderToContextMux

func AuthHeaderToContextMux() func(http.Handler) http.Handler

AuthHeaderToContextMux returns a gorilla mux middleware which copies the Authorization HTTP header into the provided context.

func AuthToContext

func AuthToContext(ctx context.Context, value string) context.Context

AuthToContext puts the authorisation into the context directly.

func DecodeJSON

func DecodeJSON[T any](reader io.ReadCloser, v *T) error

func DecodeRequestJSON

func DecodeRequestJSON[T any](r *http.Request, v *T) error

func Encode

func Encode[T any](w http.ResponseWriter, status int, v T) error

Encode defaults to JSON encoding

func EncodeJSON

func EncodeJSON[T any](w http.ResponseWriter, status int, v T) error

EncodeJSON encodes a response as JSON

func GenerateOrCopyRequestID

func GenerateOrCopyRequestID(ctx context.Context, r *http.Request) context.Context

func GenerateOrCopyRequestIDMux

func GenerateOrCopyRequestIDMux() func(http.Handler) http.Handler

func GenerateRequestIDToContext

func GenerateRequestIDToContext(r *http.Request) context.Context

GenerateRequestIDToContext generates a new request ID and copies it into the provided context.

func GenericErrorHandler added in v0.0.2

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

func InternalOnly

func InternalOnly(next http.Handler) http.HandlerFunc

func IsInternal

func IsInternal(r *http.Request) bool

IsInternal returns true if the request is internal.

func IsProxied

func IsProxied(r *http.Request) bool

IsProxied returns true if the request is from kubernetes.

func MethodNotAllowedHandler

func MethodNotAllowedHandler() http.HandlerFunc

MethodNotAllowedHandler returns a handler that returns a 405 response.

func NewErrorMessage

func NewErrorMessage(message string, err error, args ...any) *common.ErrorMessage

func NewMessage

func NewMessage(message string, args ...any) *common.Message

NewMessage creates a new Message.

func NotFoundHandler

func NotFoundHandler() http.HandlerFunc

NotFoundHandler returns a handler that returns a 404 response.

func RequestIDFromContext

func RequestIDFromContext(ctx context.Context) string

RequestIDFromContext returns the request ID HTTP header value from the provided context. If the header was not set it returns an empty string.

func RequestIDRawToContext

func RequestIDRawToContext(ctx context.Context, requestID string) context.Context

RequestIDRawToContext copies the provided request ID into the provided context.

func RequestIDToContext

func RequestIDToContext(ctx context.Context, r *http.Request) context.Context

RequestIDToContext copies the request ID HTTP header into the provided context. This should be used as a param to the transport/http.ServerBefore() (go-kit) func.

func RequestIDToContextMux

func RequestIDToContextMux() func(http.Handler) http.Handler

RequestIDToContextMux returns a gorilla mux middleware which copies the request ID HTTP header into the provided context.

func SendErrorMessage

func SendErrorMessage(w http.ResponseWriter, message string, err error, args ...any)

func SendErrorMessageWithStatus

func SendErrorMessageWithStatus(w http.ResponseWriter, status int, message string, err error, args ...any)

func SendMessage

func SendMessage(w http.ResponseWriter, message string, args ...any)

func SendMessageWithStatus

func SendMessageWithStatus(w http.ResponseWriter, status int, message string, args ...any)

func UnauthorizedHandler

func UnauthorizedHandler() http.HandlerFunc

UnauthorizedHandler returns a handler that returns a 401 response.

Types

type ContextKey

type ContextKey string

ContextKey is key type for contexts.

type MockRateLimiter

type MockRateLimiter struct {
	mock.Mock
}

MockRateLimiter is an autogenerated mock type for the RateLimiter type

func NewMockRateLimiter

func NewMockRateLimiter(t interface {
	mock.TestingT
	Cleanup(func())
}) *MockRateLimiter

NewMockRateLimiter creates a new instance of MockRateLimiter. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. The first argument is typically a *testing.T value.

func (*MockRateLimiter) Allow

func (_m *MockRateLimiter) Allow(key string) bool

Allow provides a mock function with given fields: key

type MockWriterOpt

type MockWriterOpt struct {
	mock.Mock
}

MockWriterOpt is an autogenerated mock type for the WriterOpt type

func NewMockWriterOpt

func NewMockWriterOpt(t interface {
	mock.TestingT
	Cleanup(func())
}) *MockWriterOpt

NewMockWriterOpt creates a new instance of MockWriterOpt. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. The first argument is typically a *testing.T value.

func (*MockWriterOpt) Execute

func (_m *MockWriterOpt) Execute(w *ResponseWriter)

Execute provides a mock function with given fields: w

type RateLimiter

type RateLimiter interface {
	// Allow returns true if the request is allowed.
	Allow(key string) bool
}

func NewRateLimiter

func NewRateLimiter(rps float64) RateLimiter

NewRateLimiter creates a new rate limiter.

rps is the requests per second.

func NewRateLimiterWithBurst

func NewRateLimiterWithBurst(rps float64, burst int) RateLimiter

NewRateLimiterWithBurst creates a new rate limiter.

rps is the requests per second.

burst is the burst. This is the number of requests that can be made in one go. If the burst is 0, then the burst is set to the rps. If the burst is less than the rps, then the burst is set to the rps.

type ResponseWriter

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

ResponseWriter is a wrapper around http.ResponseWriter that provides additional information. It is used to capture the status code and the number of bytes written. It also provides a way to determine if the header has been written.

func NewResponseWriter

func NewResponseWriter(w http.ResponseWriter, opts ...WriterOpt) *ResponseWriter

NewResponseWriter creates a new ResponseWriter.

func (*ResponseWriter) BytesWritten

func (c *ResponseWriter) BytesWritten() uint64

BytesWritten returns the number of bytes written.

func (*ResponseWriter) GetRequestDuration

func (c *ResponseWriter) GetRequestDuration() time.Duration

GetRequestDuration gets the duration of the request

func (*ResponseWriter) IsHeaderWritten

func (c *ResponseWriter) IsHeaderWritten() bool

IsHeaderWritten returns true if the header has been written.

func (*ResponseWriter) StatusCode

func (c *ResponseWriter) StatusCode() int

StatusCode returns the status code.

func (*ResponseWriter) Write

func (c *ResponseWriter) Write(p []byte) (bytes int, err error)

Write writes the data to the connection as part of an HTTP reply.

func (*ResponseWriter) WriteHeader

func (c *ResponseWriter) WriteHeader(code int)

WriteHeader sends an HTTP response header with the provided status code.

type WriterOpt

type WriterOpt func(w *ResponseWriter)

func WithDefaultContentType

func WithDefaultContentType(contentType string) WriterOpt

WithDefaultContentType sets the default content type for the response writer.

func WithDefaultHeader

func WithDefaultHeader(header, value string) WriterOpt

WithDefaultHeader sets the default headers for the response writer.

func WithDefaultHeaders

func WithDefaultHeaders(headers map[string]string) WriterOpt

func WithDefaultStatusCode

func WithDefaultStatusCode(statusCode int) WriterOpt

WithDefaultStatusCode sets the default status code for the response writer.

Directories

Path Synopsis
Package common provides primitives to interact with the openapi HTTP API.
Package common provides primitives to interact with the openapi HTTP API.

Jump to

Keyboard shortcuts

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