Documentation
¶
Index ¶
- func Abort(status int, message string)
- func AuthorizeFormRequest(r *http.Request, request FormRequest) bool
- func HandleValidationErrors(w http.ResponseWriter, err error)
- func Macro(name string, fn ResponseMacroFunc)
- func ValidateRequest(r *http.Request, request FormRequest) (map[string]any, error)
- func WriteMacroResponse(w http.ResponseWriter, resp *MacroResponse)
- type FormRequest
- type HttpException
- type Kernel
- func (k *Kernel) OnShutdown(fn func())
- func (k *Kernel) Serve(addr string) error
- func (k *Kernel) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (k *Kernel) SetShutdownTimeout(d time.Duration)
- func (k *Kernel) Terminate(fn func(http.ResponseWriter, *http.Request))
- func (k *Kernel) Use(mw func(http.Handler) http.Handler)
- type MacroResponse
- func AcceptedResponse(data any, message ...string) *MacroResponse
- func ApiResponse(data any, meta ...map[string]any) *MacroResponse
- func BadRequestResponse(message string) *MacroResponse
- func CallMacro(name string, args ...any) (*MacroResponse, error)
- func CreatedResponse(data any) *MacroResponse
- func ErrorResponse(message string, code int, errors ...map[string]any) *MacroResponse
- func ForbiddenResponse(message ...string) *MacroResponse
- func NoContentResponse() *MacroResponse
- func NotFoundResponse(message ...string) *MacroResponse
- func PaginatedResponse(data any, total int, perPage int, currentPage int) *MacroResponse
- func TooManyRequestsResponse(retryAfter ...int) *MacroResponse
- func UnauthorizedResponse(message ...string) *MacroResponse
- func ValidationFailedResponse(errors map[string][]string) *MacroResponse
- type ResponseMacro
- type ResponseMacroFunc
- type TerminableMiddleware
- type ValidationError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 ValidateRequest ¶
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 ¶
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 ¶
SetShutdownTimeout configures the graceful shutdown timeout. Default is 30 seconds.
type MacroResponse ¶
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 ¶
ValidationError represents validation errors
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string