Documentation
¶
Index ¶
- Constants
- func AuthHeaderFromContext(ctx context.Context) string
- func AuthHeaderToContext(ctx context.Context, r *http.Request) context.Context
- func AuthHeaderToContextMux() func(http.Handler) http.Handler
- func AuthToContext(ctx context.Context, value string) context.Context
- func DecodeJSON[T any](reader io.ReadCloser, v *T) error
- func DecodeRequestJSON[T any](r *http.Request, v *T) error
- func Encode[T any](w http.ResponseWriter, status int, v T) error
- func EncodeJSON[T any](w http.ResponseWriter, status int, v T) error
- func GenerateOrCopyRequestID(ctx context.Context, r *http.Request) context.Context
- func GenerateOrCopyRequestIDMux() func(http.Handler) http.Handler
- func GenerateRequestIDToContext(r *http.Request) context.Context
- func GenericErrorHandler(w http.ResponseWriter, r *http.Request, err error)
- func InternalOnly(next http.Handler) http.HandlerFunc
- func IsInternal(r *http.Request) bool
- func IsProxied(r *http.Request) bool
- func MethodNotAllowedHandler() http.HandlerFunc
- func NewErrorMessage(message string, err error, args ...any) *common.ErrorMessage
- func NewMessage(message string, args ...any) *common.Message
- func NotFoundHandler() http.HandlerFunc
- func RequestIDFromContext(ctx context.Context) string
- func RequestIDRawToContext(ctx context.Context, requestID string) context.Context
- func RequestIDToContext(ctx context.Context, r *http.Request) context.Context
- func RequestIDToContextMux() func(http.Handler) http.Handler
- func SendErrorMessage(w http.ResponseWriter, message string, err error, args ...any)
- func SendErrorMessageWithStatus(w http.ResponseWriter, status int, message string, err error, args ...any)
- func SendMessage(w http.ResponseWriter, message string, args ...any)
- func SendMessageWithStatus(w http.ResponseWriter, status int, message string, args ...any)
- func UnauthorizedHandler() http.HandlerFunc
- type ContextKey
- type MockRateLimiter
- type MockWriterOpt
- type RateLimiter
- type ResponseWriter
- func (c *ResponseWriter) BytesWritten() uint64
- func (c *ResponseWriter) GetRequestDuration() time.Duration
- func (c *ResponseWriter) IsHeaderWritten() bool
- func (c *ResponseWriter) StatusCode() int
- func (c *ResponseWriter) Write(p []byte) (bytes int, err error)
- func (c *ResponseWriter) WriteHeader(code int)
- type WriterOpt
Constants ¶
const ( MsgNotFound = "Not found" MsgMethodNotAllowed = "Method not allowed" MsgBadRequest = "Bad request" HeaderContentType = "Content-Type" ContentTypeJSON = "application/json" )
Variables ¶
This section is empty.
Functions ¶
func AuthHeaderFromContext ¶
AuthHeaderFromContext returns the Authorization HTTP header value from the provided context. If the header was not set it returns an empty string.
func AuthHeaderToContext ¶
AuthHeaderToContext copies the Authorization HTTP header into the provided context.
func AuthHeaderToContextMux ¶
AuthHeaderToContextMux returns a gorilla mux middleware which copies the Authorization HTTP header into the provided context.
func AuthToContext ¶
AuthToContext puts the authorisation into the context directly.
func DecodeJSON ¶
func DecodeJSON[T any](reader io.ReadCloser, 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 GenerateRequestIDToContext ¶
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 ¶
IsInternal returns true if the request is internal.
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 ¶
NewMessage creates a new Message.
func NotFoundHandler ¶
func NotFoundHandler() http.HandlerFunc
NotFoundHandler returns a handler that returns a 404 response.
func RequestIDFromContext ¶
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 ¶
RequestIDRawToContext copies the provided request ID into the provided context.
func RequestIDToContext ¶
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 ¶
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 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 MockRateLimiter ¶
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 ¶
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 ¶
WithDefaultContentType sets the default content type for the response writer.
func WithDefaultHeader ¶
WithDefaultHeader sets the default headers for the response writer.
func WithDefaultHeaders ¶
func WithDefaultStatusCode ¶
WithDefaultStatusCode sets the default status code for the response writer.