handler

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package handler is part of the GoFastr framework. See https://github.com/DonaldMurillo/gofastr for documentation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bind

func Bind(r *http.Request, dst any) error

Bind populates dst by merging values from all sources: 1. Header values (via `header:"X-Request-ID"` tag) 2. Query parameters (via `query:"name"` tag) 3. Path parameters (via `path:"id"` tag) 4. JSON body (decodes entire struct — highest priority)

If the request has a JSON body and Content-Type is application/json, the body is decoded first. Then query/path/header values fill in any zero-valued fields.

If the body is present but not valid JSON, a 400 error is returned. If dst is not a pointer, Bind panics.

func GetLogger

func GetLogger(ctx context.Context) (any, bool)

GetLogger retrieves the logger from the context.

func GetRequestID

func GetRequestID(ctx context.Context) (string, bool)

GetRequestID retrieves the request ID from the context.

func GetTenant

func GetTenant(ctx context.Context) (any, bool)

GetTenant retrieves the tenant value from the context.

func GetUser

func GetUser(ctx context.Context) (any, bool)

GetUser retrieves the user value from the context.

func HandlerAdapter

func HandlerAdapter[I, O any](h Handler[I, O]) http.HandlerFunc

HandlerAdapter bridges a typed Handler into a standard http.HandlerFunc. It handles input binding, panic recovery, and output serialization.

func RequestFromContext

func RequestFromContext(ctx context.Context) (*http.Request, bool)

RequestFromContext retrieves the *http.Request stored by HandlerAdapter.

func Respond

func Respond(w http.ResponseWriter, r *http.Request, out any)

Respond writes out the response based on out's type:

  • nil → 204 No Content
  • ResponseType → delegates to the custom type
  • any other value → JSON serialization, 200 OK

func SSEStream

func SSEStream(w http.ResponseWriter, events <-chan SSE)

SSEStream writes a channel of SSE events as a streaming response.

func SetLogger

func SetLogger(ctx context.Context, logger any) context.Context

SetLogger stores a logger in the context.

func SetRequestID

func SetRequestID(ctx context.Context, id string) context.Context

SetRequestID stores a request ID in the context.

func SetTenant

func SetTenant(ctx context.Context, tenant any) context.Context

SetTenant stores a tenant value in the context.

func SetUser

func SetUser(ctx context.Context, user any) context.Context

SetUser stores a user value in the context.

func WriteError

func WriteError(w http.ResponseWriter, err error)

WriteError writes a structured error response to w.

An *Error is rendered verbatim (its Code, Message, and Fields are what the developer chose to expose). Any other error type is treated as an internal failure: the response message is a generic "internal server error" with status 500, and the original error stays out of the response body. Callers that *do* want the inner message to reach the client must wrap explicitly via Errorf or WrapError (the latter keeps the cause for `errors.Is/As` without leaking it).

This prevents accidental leaks of database errors, driver-specific strings ("pq: password authentication failed for user \"admin\""), or wrapped stack traces.

Types

type Error

type Error struct {
	Code    int                 // HTTP status code
	Message string              // human-readable message
	Err     error               // wrapped cause (optional)
	Fields  map[string][]string // field-level validation errors (optional)
}

Error is a structured HTTP error with optional field-level validation errors.

func Errorf

func Errorf(code int, format string, args ...any) *Error

Errorf creates a new Error with the given HTTP status code and formatted message.

func ValidationError

func ValidationError(fields map[string][]string) *Error

ValidationError creates a 400 error with field-level validation messages.

func WrapError

func WrapError(code int, message string, err error) *Error

WrapError wraps an existing error with an HTTP status code and message.

func (*Error) Error

func (e *Error) Error() string

Error implements the error interface.

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns the wrapped cause for errors.Is/As.

type HTML

type HTML string

HTML is a response type that writes text/html.

func (HTML) ContentType

func (h HTML) ContentType() string

func (HTML) WriteBody

func (h HTML) WriteBody(w http.ResponseWriter) error

type Handler

type Handler[I, O any] func(ctx context.Context, in I) (O, error)

Handler is a typed function that processes an input of type I and returns an output of type O. It receives a context (which carries the *http.Request via RequestFromContext) and a fully-bound input struct.

type RawBytes

type RawBytes struct {
	Data []byte
	CT   string // content type, e.g. "image/png"
}

RawBytes is a response type for raw bytes with an explicit content type.

func (RawBytes) ContentType

func (r RawBytes) ContentType() string

func (RawBytes) WriteBody

func (r RawBytes) WriteBody(w http.ResponseWriter) error

type ResponseType

type ResponseType interface {
	// ContentType returns the MIME type for the response.
	ContentType() string
	// WriteBody writes the response body to w.
	WriteBody(w http.ResponseWriter) error
}

ResponseType is an interface for custom response types that control how they are serialized and what Content-Type is used.

type SSE

type SSE struct {
	Event string
	Data  string
	ID    string // optional event ID
}

SSE is a Server-Sent Event response.

func (SSE) ContentType

func (s SSE) ContentType() string

func (SSE) WriteBody

func (s SSE) WriteBody(w http.ResponseWriter) error

Jump to

Keyboard shortcuts

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