huma

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// SchemeHTTP scheme name.
	SchemeHTTP = "http"
	// SchemeHTTPS scheme name.
	SchemeHTTPS = "https"

	// HeaderCorrelationID correlation ID header name.
	HeaderCorrelationID = "X-Correlation-ID"
	// HeaderForwardedProto header name.
	HeaderForwardedProto = "X-Forwarded-Proto"
	// HeaderForwardedScheme header name.
	HeaderForwardedScheme = "X-Forwarded-Scheme"

	// ContextCorrelationID context correlation ID name.
	ContextCorrelationID = "correlation-id"

	ContentTypeTextPlain       = "text/plain"
	ContentTypeTextHTML        = "text/html"
	ContentTypeApplicationJSON = "application/json"
)

Variables

View Source
var (
	// ErrPkg error.
	ErrPkg = errors.New("huma")
	// ErrMarshal error.
	ErrMarshal = fmt.Errorf("%w: marshal", ErrPkg)
	// ErrUnmarshal error.
	ErrUnmarshal = fmt.Errorf("%w: unmarshal", ErrPkg)
)

Functions

This section is empty.

Types

type AccessLogMiddleware

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

AccessLogMiddleware middleware to log access events.

func NewAccessLogMiddleware

func NewAccessLogMiddleware(options ...AccessLogMiddlewareOption) *AccessLogMiddleware

NewAccessLogMiddleware factory function to crate a new access log middleware instance.

func (*AccessLogMiddleware) Handler

func (m *AccessLogMiddleware) Handler(ctx huma.Context, _ huma.API, next func(huma.Context))

Handler middleware handler.

type AccessLogMiddlewareOption

type AccessLogMiddlewareOption func(p *AccessLogMiddleware)

AccessLogMiddlewareOption function.

func WithAccessLogMiddlewareLogFunc

func WithAccessLogMiddlewareLogFunc(fn LogFunc) AccessLogMiddlewareOption

WithAccessLogMiddlewareLogFunc configuration option.

type Config

type Config struct {
	Title         string `env:"TITLE,default=API"                       json:"title"`
	Version       string `env:"VERSION,default=v0.0.0"                  json:"version"`
	OpenAPIPath   string `env:"OPEN_API_PATH,default=/openapi"          json:"openApiPath"`
	DocsPath      string `env:"DOCS_PATH,default=/docs"                 json:"docsPath"`
	SchemasPath   string `env:"SCHEMAS_PATH,default=/schemas"           json:"schemasPath"`
	DefaultFormat string `env:"DEFAULT_FORMAT,default=application/json" json:"defaultFormat"`
}

Config is a struct containing server configuration values.

type CorrelationIDMiddleware

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

CorrelationIDMiddleware middleware to handle correlation ID.

func NewCorrelationIDMiddleware

func NewCorrelationIDMiddleware(options ...CorrelationIDMiddlewareOption) *CorrelationIDMiddleware

NewCorrelationIDMiddleware factory function to crate a correlation ID handler middleware instance.

func (*CorrelationIDMiddleware) Handler

func (m *CorrelationIDMiddleware) Handler(ctx huma.Context, _ huma.API, next func(huma.Context))

Handler middleware handler.

type CorrelationIDMiddlewareOption

type CorrelationIDMiddlewareOption func(p *CorrelationIDMiddleware)

CorrelationIDMiddlewareOption function.

func WithCorrelationIDMiddlewareCorrelationIDProvider

func WithCorrelationIDMiddlewareCorrelationIDProvider(
	provider CorrelationIDProvider,
) CorrelationIDMiddlewareOption

WithCorrelationIDMiddlewareCorrelationIDProvider configuration option.

type CorrelationIDProvider

type CorrelationIDProvider interface {
	String() string
}

CorrelationIDProvider correlation ID provider.

type CorrelationIDProviderFunc

type CorrelationIDProviderFunc func() string

CorrelationIDProviderFunc correlation ID provider.

func NewUUIDCorrelationIDProvider

func NewUUIDCorrelationIDProvider() CorrelationIDProviderFunc

NewUUIDCorrelationIDProvider factory function.

func (CorrelationIDProviderFunc) String

func (f CorrelationIDProviderFunc) String() string

String method to get correlation ID.

type FormatApplicationJSON

type FormatApplicationJSON struct{}

FormatApplicationJSON implements the Huma Format interface for a specific content type.

func NewFormatApplicationJSON

func NewFormatApplicationJSON() *FormatApplicationJSON

NewFormatApplicationJSON factory function.

func (*FormatApplicationJSON) ContentType

func (f *FormatApplicationJSON) ContentType() string

ContentType returns the MIME type associated with this format.

func (*FormatApplicationJSON) Marshal

func (f *FormatApplicationJSON) Marshal(writer io.Writer, v any) error

Marshal writes the encoded form of v to writer.

func (*FormatApplicationJSON) Unmarshal

func (f *FormatApplicationJSON) Unmarshal(data []byte, v any) error

Unmarshal decodes the provided data into v.

type FormatTextHTML

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

FormatTextHTML implements the Huma Format interface for a specific content type.

func NewFormatTextHTML

func NewFormatTextHTML(options ...FormatTextHTMLOption) *FormatTextHTML

NewFormatTextHTML factory function.

func (*FormatTextHTML) ContentType

func (f *FormatTextHTML) ContentType() string

ContentType returns the MIME type associated with this format.

func (*FormatTextHTML) Marshal

func (f *FormatTextHTML) Marshal(writer io.Writer, value any) error

Marshal writes the encoded form of v to writer.

func (*FormatTextHTML) Unmarshal

func (f *FormatTextHTML) Unmarshal(_ []byte, _ any) error

Unmarshal decodes the provided data into v. HTML input is not typically parsed into structs, so this is a no-op.

type FormatTextHTMLOption

type FormatTextHTMLOption func(p *FormatTextHTML)

FormatTextHTMLOption function.

func WithFormatTextHTMLTemplate

func WithFormatTextHTMLTemplate(tpl string) FormatTextHTMLOption

WithFormatTextHTMLTemplate configuration option.

type FormatTextPlain

type FormatTextPlain struct{}

FormatTextPlain implements the Huma Format interface for plain text content.

func NewFormatTextPlain

func NewFormatTextPlain() *FormatTextPlain

NewFormatTextPlain factory function.

func (*FormatTextPlain) ContentType

func (f *FormatTextPlain) ContentType() string

ContentType returns the MIME type associated with this format.

func (*FormatTextPlain) Marshal

func (f *FormatTextPlain) Marshal(writer io.Writer, v any) error

Marshal writes the encoded form of v to writer.

func (*FormatTextPlain) Unmarshal

func (f *FormatTextPlain) Unmarshal(_ []byte, _ any) error

Unmarshal decodes the provided data into v.

Plain text cannot be generically unmarshaled into structured Go types, so this returns an error for all cases.

type Formatter

type Formatter interface {
	ContentType() string
	Marshal(writer io.Writer, v any) error
	Unmarshal(data []byte, v any) error
}

Formatter input/output formatter.

type HTMLErrorView

type HTMLErrorView struct {
	Status int
	Title  string
	Detail string
}

HTMLErrorView type.

type Handler

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

Handler abstraction for the router to be used with Huma.

func NewHandler

func NewHandler(options ...HandlerOption) *Handler

NewHandler a factory function to create a new server.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(writer http.ResponseWriter, req *http.Request)

ServeHTTP an http handler.

type HandlerOption

type HandlerOption func(p *Handler)

HandlerOption function.

func WithConfig

func WithConfig(config *Config) HandlerOption

WithConfig configuration option.

func WithFormatter

func WithFormatter(formatter []Formatter) HandlerOption

WithFormatter configuration option.

func WithMiddleware

func WithMiddleware(middleware []MiddlewareFunc) HandlerOption

WithMiddleware configuration option.

func WithOperationWithHandler

func WithOperationWithHandler(operations []OperationWithHandlerRegisterer) HandlerOption

WithOperationWithHandler configuration option.

func WithRouter

func WithRouter(router Router) HandlerOption

WithRouter configuration option.

func WithSecuritySchemes

func WithSecuritySchemes(securityScheme []SecurityScheme) HandlerOption

WithSecuritySchemes configuration option.

type LogField

type LogField struct {
	Key   string
	Value any
}

LogField a custom field struct representation.

type LogFunc

type LogFunc func(message string, fields ...LogField)

LogFunc to allow log warnings and infos.

type MiddlewareFunc

type MiddlewareFunc func(ctx huma.Context, api huma.API, next func(huma.Context))

MiddlewareFunc middleware function type.

type MiddlewareHandler

type MiddlewareHandler interface {
	Handler(ctx huma.Context, api huma.API, next func(huma.Context))
}

MiddlewareHandler interface.

type OperationWithHandler

type OperationWithHandler[I, O any] struct {
	huma.Operation

	Handler func(context.Context, *I) (*O, error)
}

OperationWithHandler an operation type.

func (*OperationWithHandler[I, O]) Register

func (op *OperationWithHandler[I, O]) Register(api huma.API)

Register to register operation in huma API.

type OperationWithHandlerRegisterer

type OperationWithHandlerRegisterer interface {
	Register(api huma.API)
}

OperationWithHandlerRegisterer an operation with handler registerer.

type Router

type Router interface {
	chi.Router
}

Router common router interface.

type SecurityScheme

type SecurityScheme interface {
	Key() string
	Type() string
	Name() string
	Description() string
	Scheme() string
	In() string
}

SecurityScheme interface.

type SecuritySchemeAPIHeaderKeyAuth

type SecuritySchemeAPIHeaderKeyAuth struct{}

SecuritySchemeAPIHeaderKeyAuth represents an API key authentication scheme using a header.

func NewSecuritySchemeAPIHeaderKeyAuth

func NewSecuritySchemeAPIHeaderKeyAuth() *SecuritySchemeAPIHeaderKeyAuth

NewSecuritySchemeAPIHeaderKeyAuth creates a new header-based API key security scheme.

func (*SecuritySchemeAPIHeaderKeyAuth) Description

func (s *SecuritySchemeAPIHeaderKeyAuth) Description() string

Description returns a short description of this security scheme.

func (*SecuritySchemeAPIHeaderKeyAuth) In

In returns the location of the authentication parameter.

func (*SecuritySchemeAPIHeaderKeyAuth) Key

Key returns the unique identifier for this security scheme.

func (*SecuritySchemeAPIHeaderKeyAuth) Name

Name returns the parameter name for this scheme.

func (*SecuritySchemeAPIHeaderKeyAuth) Scheme

Scheme returns the HTTP authentication scheme, if any.

func (*SecuritySchemeAPIHeaderKeyAuth) Type

Type returns the security scheme type.

type SecuritySchemeAPIQueryKeyAuth

type SecuritySchemeAPIQueryKeyAuth struct{}

SecuritySchemeAPIQueryKeyAuth represents an API key authentication scheme using a query parameter.

func NewSecuritySchemeAPIQueryKeyAuth

func NewSecuritySchemeAPIQueryKeyAuth() *SecuritySchemeAPIQueryKeyAuth

NewSecuritySchemeAPIQueryKeyAuth creates a new query-based API key security scheme.

func (*SecuritySchemeAPIQueryKeyAuth) Description

func (s *SecuritySchemeAPIQueryKeyAuth) Description() string

Description returns a short description of this security scheme.

func (*SecuritySchemeAPIQueryKeyAuth) In

In returns the location of the authentication parameter.

func (*SecuritySchemeAPIQueryKeyAuth) Key

Key returns the unique identifier for this security scheme.

func (*SecuritySchemeAPIQueryKeyAuth) Name

Name returns the parameter name for this scheme.

func (*SecuritySchemeAPIQueryKeyAuth) Scheme

Scheme returns the HTTP authentication scheme, if any.

func (*SecuritySchemeAPIQueryKeyAuth) Type

Type returns the security scheme type.

type SecuritySchemeBasicAuth

type SecuritySchemeBasicAuth struct{}

SecuritySchemeBasicAuth represents a basic HTTP authentication scheme.

func NewSecuritySchemeBasicAuth

func NewSecuritySchemeBasicAuth() *SecuritySchemeBasicAuth

NewSecuritySchemeBasicAuth creates a new basic auth security scheme.

func (*SecuritySchemeBasicAuth) Description

func (s *SecuritySchemeBasicAuth) Description() string

Description returns a short description of this security scheme.

func (*SecuritySchemeBasicAuth) In

In returns the location of the authentication parameter.

func (*SecuritySchemeBasicAuth) Key

Key returns the unique identifier for this security scheme.

func (*SecuritySchemeBasicAuth) Name

func (s *SecuritySchemeBasicAuth) Name() string

Name returns the parameter name for this scheme, if any.

func (*SecuritySchemeBasicAuth) Scheme

func (s *SecuritySchemeBasicAuth) Scheme() string

Scheme returns the HTTP authentication scheme.

func (*SecuritySchemeBasicAuth) Type

func (s *SecuritySchemeBasicAuth) Type() string

Type returns the security scheme type.

type SecuritySchemeTokenAuth

type SecuritySchemeTokenAuth struct{}

SecuritySchemeTokenAuth represents a bearer token authentication scheme.

func NewSecuritySchemeTokenAuth

func NewSecuritySchemeTokenAuth() *SecuritySchemeTokenAuth

NewSecuritySchemeTokenAuth creates a new token-based authentication scheme.

func (*SecuritySchemeTokenAuth) Description

func (s *SecuritySchemeTokenAuth) Description() string

Description returns a short description of this security scheme.

func (*SecuritySchemeTokenAuth) In

In returns the location of the authentication parameter.

func (*SecuritySchemeTokenAuth) Key

Key returns the unique identifier for this security scheme.

func (*SecuritySchemeTokenAuth) Name

func (s *SecuritySchemeTokenAuth) Name() string

Name returns the parameter name for this scheme.

func (*SecuritySchemeTokenAuth) Scheme

func (s *SecuritySchemeTokenAuth) Scheme() string

Scheme returns the HTTP authentication scheme.

func (*SecuritySchemeTokenAuth) Type

func (s *SecuritySchemeTokenAuth) Type() string

Type returns the security scheme type.

Jump to

Keyboard shortcuts

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