http

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2026 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Overview

Package http provides a set of utilities for handling HTTP requests and responses within the Kite framework.

Package http provides a set of utilities for handling HTTP requests and responses within the Kite framework.

Index

Constants

View Source
const (
	DefaultSwaggerFileName = "openapi.json"
)
View Source
const (
	StatusClientClosedRequest = 499
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CodeResponder

type CodeResponder interface {
	Code() int
}

CodeResponder allows errors to specify a business error code. This is used in the JSON response "code" field. If not implemented, falls back to StatusCodeResponder.StatusCode(), or -1.

type ErrorClientClosedRequest

type ErrorClientClosedRequest struct{}

ErrorClientClosedRequest represents when client cancels the request.

func (ErrorClientClosedRequest) Error

func (ErrorClientClosedRequest) LogLevel

func (ErrorClientClosedRequest) StatusCode

func (ErrorClientClosedRequest) StatusCode() int

type ErrorEntityAlreadyExist

type ErrorEntityAlreadyExist struct{}

ErrorEntityAlreadyExist represents an error for when entity is already present in the storage and we are trying to make duplicate entry.

func (ErrorEntityAlreadyExist) Error

func (ErrorEntityAlreadyExist) LogLevel

func (ErrorEntityAlreadyExist) StatusCode

func (ErrorEntityAlreadyExist) StatusCode() int

type ErrorEntityNotFound

type ErrorEntityNotFound struct {
	Name  string
	Value string
}

ErrorEntityNotFound represents an error for when an entity is not found in the system.

func (ErrorEntityNotFound) Error

func (e ErrorEntityNotFound) Error() string

func (ErrorEntityNotFound) LogLevel

func (ErrorEntityNotFound) LogLevel() logging.Level

func (ErrorEntityNotFound) StatusCode

func (ErrorEntityNotFound) StatusCode() int

type ErrorInvalidParam

type ErrorInvalidParam struct {
	Params []string `json:"param,omitempty"` // Params contains the list of invalid parameter names.
}

ErrorInvalidParam represents an error for invalid parameter values.

func (ErrorInvalidParam) Error

func (e ErrorInvalidParam) Error() string

func (ErrorInvalidParam) LogLevel

func (ErrorInvalidParam) LogLevel() logging.Level

func (ErrorInvalidParam) StatusCode

func (ErrorInvalidParam) StatusCode() int

type ErrorInvalidRoute

type ErrorInvalidRoute struct{}

ErrorInvalidRoute represents an error for invalid route in a request.

func (ErrorInvalidRoute) Error

func (ErrorInvalidRoute) Error() string

func (ErrorInvalidRoute) LogLevel

func (ErrorInvalidRoute) LogLevel() logging.Level

func (ErrorInvalidRoute) StatusCode

func (ErrorInvalidRoute) StatusCode() int

type ErrorMissingParam

type ErrorMissingParam struct {
	Params []string `json:"param,omitempty"`
}

ErrorMissingParam represents an error for missing parameters in a request.

func (ErrorMissingParam) Error

func (e ErrorMissingParam) Error() string

func (ErrorMissingParam) LogLevel

func (ErrorMissingParam) LogLevel() logging.Level

func (ErrorMissingParam) StatusCode

func (ErrorMissingParam) StatusCode() int

type ErrorPanicRecovery

type ErrorPanicRecovery struct{}

ErrorPanicRecovery represents an error for request which panicked.

func (ErrorPanicRecovery) Error

func (ErrorPanicRecovery) Error() string

func (ErrorPanicRecovery) LogLevel

func (ErrorPanicRecovery) LogLevel() logging.Level

func (ErrorPanicRecovery) StatusCode

func (ErrorPanicRecovery) StatusCode() int

type ErrorRequestTimeout

type ErrorRequestTimeout struct{}

ErrorRequestTimeout represents an error for request which timed out.

func (ErrorRequestTimeout) Error

func (ErrorRequestTimeout) Error() string

func (ErrorRequestTimeout) LogLevel

func (ErrorRequestTimeout) LogLevel() logging.Level

func (ErrorRequestTimeout) StatusCode

func (ErrorRequestTimeout) StatusCode() int

type ErrorServiceUnavailable

type ErrorServiceUnavailable struct {
	Dependency   string
	ErrorMessage string
}

func (ErrorServiceUnavailable) Error

func (e ErrorServiceUnavailable) Error() string

func (ErrorServiceUnavailable) LogLevel

func (ErrorServiceUnavailable) StatusCode

func (ErrorServiceUnavailable) StatusCode() int

type ErrorTooManyRequests

type ErrorTooManyRequests struct{}

ErrorTooManyRequests represents an error when rate limit is exceeded.

func (ErrorTooManyRequests) Error

func (ErrorTooManyRequests) Error() string

func (ErrorTooManyRequests) LogLevel

func (ErrorTooManyRequests) LogLevel() logging.Level

func (ErrorTooManyRequests) StatusCode

func (ErrorTooManyRequests) StatusCode() int

type Metrics

type Metrics interface {
	IncrementCounter(ctx context.Context, name string, labels ...string)
}

Metrics represents an interface for registering the default metrics in Kite framework.

type Middleware

type Middleware func(handler http.Handler) http.Handler

type Request

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

Request is an abstraction over the underlying http.Request. This abstraction is useful because it allows us to create applications without being aware of the transport. cmd.Request is another such abstraction.

func NewRequest

func NewRequest(r *http.Request) *Request

NewRequest creates a new Kite Request instance from the given http.Request.

func (*Request) Bind

func (r *Request) Bind(i any) error

Bind parses the request body and binds it to the provided interface. It also validates the struct using "binding" or "validate" tags.

func (*Request) Context

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

Context returns the context of the request.

func (*Request) HostName

func (r *Request) HostName() string

HostName retrieves the hostname from the request.

func (*Request) Param

func (r *Request) Param(key string) string

Param returns the query parameter with the given key.

func (*Request) Params

func (r *Request) Params(key string) []string

Params returns a slice of strings containing the values associated with the given query parameter key. If the parameter is not present, an empty slice is returned.

func (*Request) PathParam

func (r *Request) PathParam(key string) string

PathParam retrieves a path parameter from the request.

type Responder

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

Responder encapsulates an http.ResponseWriter and is responsible for crafting structured responses.

func NewResponder

func NewResponder(w http.ResponseWriter, method string) *Responder

NewResponder creates a new Responder instance from the given http.ResponseWriter.

func (Responder) Respond

func (r Responder) Respond(data any, err error)

Respond sends a response with the given data and handles potential errors, setting appropriate status codes and formatting responses as JSON with {code, data, message, meta} format.

type ResponseMarshaller

type ResponseMarshaller interface {
	Response() map[string]any
}

ResponseMarshaller defines an interface for errors that can provide custom fields. This enables errors to extend the error response with additional fields.

type Router

type Router struct {
	mux.Router
	RegisteredRoutes *[]string
}

Router is responsible for routing HTTP request.

func NewRouter

func NewRouter() *Router

NewRouter creates a new Router instance.

func (*Router) Add

func (rou *Router) Add(method, pattern string, handler http.Handler)

Add adds a new route with the given HTTP method, pattern, and handler, wrapping the handler with OpenTelemetry instrumentation.

func (*Router) AddStaticFiles

func (rou *Router) AddStaticFiles(logger logging.Logger, endpoint, dirName string)

func (*Router) ServeHTTP

func (rou *Router) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler interface with path normalization.

func (*Router) UseMiddleware

func (rou *Router) UseMiddleware(mws ...Middleware)

UseMiddleware registers middlewares to the router.

type StatusCodeResponder

type StatusCodeResponder interface {
	StatusCode() int
}

StatusCodeResponder allows errors to specify the HTTP status code.

type ValidationError

type ValidationError struct {
	Errors validator.ValidationErrors
	// contains filtered or unexported fields
}

ValidationError wraps validator.ValidationErrors with struct type info for tag resolution.

func (*ValidationError) Error

func (e *ValidationError) Error() string

func (*ValidationError) StatusCode

func (e *ValidationError) StatusCode() int

StatusCode returns 400 Bad Request for validation errors.

Directories

Path Synopsis
Package middleware provides a collection of middleware functions that handles various aspects of request handling, such as authentication, logging, tracing, and metrics collection.
Package middleware provides a collection of middleware functions that handles various aspects of request handling, such as authentication, logging, tracing, and metrics collection.

Jump to

Keyboard shortcuts

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