http

package
v0.0.0-20260608 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abort

func Abort(status int, message string)

Abort panics with an HttpException to stop execution and render an error response.

func AuthorizeFormRequest

func AuthorizeFormRequest(r *http.Request, request FormRequest) bool

AuthorizeFormRequest checks if the request is authorized

func HandleValidationErrors

func HandleValidationErrors(w http.ResponseWriter, err error)

HandleValidationErrors sends a validation error response

func Macro

func Macro(name string, fn ResponseMacroFunc)

Macro registers a global response macro.

func ValidateRequest

func ValidateRequest(r *http.Request, request FormRequest) (map[string]any, error)

ValidateRequest validates an incoming request against form request rules

func WriteMacroResponse

func WriteMacroResponse(w http.ResponseWriter, resp *MacroResponse)

WriteMacroResponse writes a MacroResponse to an http.ResponseWriter.

Types

type FormRequest

type FormRequest interface {
	Rules() map[string][]string
	Messages() map[string]string
	Authorize(r *http.Request) bool
}

FormRequest represents a form request with validation

type HttpException

type HttpException = exception.HttpException

HttpException is an alias for exception.HttpException for backward compatibility. Use exception.HttpException directly for new code.

type Kernel

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

Kernel handles incoming HTTP requests.

func NewKernel

func NewKernel(app *foundation.Application, router *routing.Router) *Kernel

NewKernel creates a new HTTP Kernel.

func (*Kernel) OnShutdown

func (k *Kernel) OnShutdown(fn func())

OnShutdown registers a function to be called during graceful shutdown. Hooks are called in the order they were registered, after the HTTP server stops accepting new connections but before the process exits. Use this to close database connections, flush logs, drain queue workers, etc.

func (*Kernel) Serve

func (k *Kernel) Serve(addr string) error

Serve starts the HTTP server and blocks until a shutdown signal is received. It listens for SIGINT and SIGTERM, then gracefully shuts down the server, allowing in-flight requests to complete within the configured timeout.

func (*Kernel) ServeHTTP

func (k *Kernel) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler. It uses the cached middleware pipeline and runs terminate hooks after the response is sent.

func (*Kernel) SetShutdownTimeout

func (k *Kernel) SetShutdownTimeout(d time.Duration)

SetShutdownTimeout configures the graceful shutdown timeout. Default is 30 seconds.

func (*Kernel) Terminate

func (k *Kernel) Terminate(fn func(http.ResponseWriter, *http.Request))

Terminate registers a callback to run after every response is sent. Use this for post-response cleanup: flushing logs, closing connections, etc.

func (*Kernel) Use

func (k *Kernel) Use(mw func(http.Handler) http.Handler)

Use adds global middleware to the kernel.

type MacroResponse

type MacroResponse struct {
	StatusCode int
	Body       any
	Headers    map[string]string
}

MacroResponse represents an HTTP response for macros.

func AcceptedResponse

func AcceptedResponse(data any, message ...string) *MacroResponse

AcceptedResponse creates a 202 Accepted response.

func ApiResponse

func ApiResponse(data any, meta ...map[string]any) *MacroResponse

ApiResponse creates a JSON API response wrapper.

func BadRequestResponse

func BadRequestResponse(message string) *MacroResponse

BadRequestResponse creates a 400 Bad Request response.

func CallMacro

func CallMacro(name string, args ...any) (*MacroResponse, error)

CallMacro calls a global response macro.

func CreatedResponse

func CreatedResponse(data any) *MacroResponse

CreatedResponse creates a 201 Created response.

func ErrorResponse

func ErrorResponse(message string, code int, errors ...map[string]any) *MacroResponse

ErrorResponse creates an error response.

func ForbiddenResponse

func ForbiddenResponse(message ...string) *MacroResponse

ForbiddenResponse creates a 403 Forbidden response.

func NoContentResponse

func NoContentResponse() *MacroResponse

NoContentResponse creates a 204 No Content response.

func NotFoundResponse

func NotFoundResponse(message ...string) *MacroResponse

NotFoundResponse creates a 404 Not Found response.

func PaginatedResponse

func PaginatedResponse(data any, total int, perPage int, currentPage int) *MacroResponse

PaginatedResponse creates a paginated response.

func TooManyRequestsResponse

func TooManyRequestsResponse(retryAfter ...int) *MacroResponse

TooManyRequestsResponse creates a 429 Too Many Requests response.

func UnauthorizedResponse

func UnauthorizedResponse(message ...string) *MacroResponse

UnauthorizedResponse creates a 401 Unauthorized response.

func ValidationFailedResponse

func ValidationFailedResponse(errors map[string][]string) *MacroResponse

ValidationFailedResponse creates a 422 Validation Failed response.

type ResponseMacro

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

ResponseMacro provides a way to add custom response methods.

func NewResponseMacro

func NewResponseMacro() *ResponseMacro

NewResponseMacro creates a new ResponseMacro.

func (*ResponseMacro) Call

func (rm *ResponseMacro) Call(name string, args ...any) (*MacroResponse, error)

Call calls a registered macro.

func (*ResponseMacro) Has

func (rm *ResponseMacro) Has(name string) bool

Has checks if a macro is registered.

func (*ResponseMacro) List

func (rm *ResponseMacro) List() []string

List returns all registered macro names.

func (*ResponseMacro) Macro

func (rm *ResponseMacro) Macro(name string, fn ResponseMacroFunc)

Macro registers a new response macro.

type ResponseMacroFunc

type ResponseMacroFunc func(args ...any) *MacroResponse

ResponseMacroFunc is a function that creates an HTTP response.

type TerminableMiddleware

type TerminableMiddleware interface {
	Terminate(w http.ResponseWriter, r *http.Request)
}

TerminableMiddleware is an optional interface that middleware can implement to run code after the response has been sent to the client.

type ValidationError

type ValidationError struct {
	Errors map[string][]string `json:"errors"`
}

ValidationError represents validation errors

func (*ValidationError) Error

func (e *ValidationError) Error() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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