Documentation
¶
Overview ¶
Package httpserver provides an opinionated HTTP server wrapper around Echo v5 with integrated middleware.
The server automatically configures request logging, body size limiting, CORS (optional), validation, and error handling. It supports graceful shutdown and is production-ready.
Basic usage:
server := httpserver.New(&httpserver.Config{
Host: "0.0.0.0",
Port: 8080,
BodyLimit: "10M",
})
server.Echo.GET("/api/users", listUsersHandler)
if err := server.Start(ctx); err != nil {
return err
}
The underlying Echo instance is accessible via server.Echo for route registration and custom middleware. The server implements graceful shutdown with a configurable GracePeriod (default 15s).
Index ¶
- Constants
- func BadRequestError(err error, details ...string) *echo.HTTPError
- func ConflictError(err error, details ...string) *echo.HTTPError
- func ExecuteStandardized[REQ any, RES any](c *echo.Context, request *REQ, handlerName string, ...) (resp any, httpErr *echo.HTTPError)
- func ForbiddenError(err error, details ...string) *echo.HTTPError
- func HTTPError(code int, err error, details ...string) *echo.HTTPError
- func InternalError(err error, details ...string) *echo.HTTPError
- func NormalizePage(page, pageSize int) (normalizedPage, normalizedSize, offset int)
- func NotFoundError(err error, details ...string) *echo.HTTPError
- func RequestIDSkipper(skip bool) echomiddleware.Skipper
- func SafeLogFieldsExtractor(ctx *echo.Context) map[string]any
- func ServiceUnavailableError(err error, details ...string) *echo.HTTPError
- func UnauthorizedError(err error, details ...string) *echo.HTTPError
- func Wrapper[TREQ any](wrapped func(*echo.Context, *TREQ) (any, *echo.HTTPError)) echo.HandlerFunc
- type APIResponse
- type Config
- type HandlerFunc
- type HandlerResponse
- type Pagination
- type ResponseError
- type Server
Constants ¶
View Source
const ( ErrCodeValidation = "VALIDATION_ERROR" ErrCodeNotFound = "NOT_FOUND" ErrCodeForbidden = "FORBIDDEN" ErrCodeConflict = "CONFLICT" ErrCodeInternal = "INTERNAL_ERROR" ErrCodeBadRequest = "BAD_REQUEST" )
View Source
const ( DefaultPage = 1 DefaultPageSize = 50 MaxPageSize = 100 )
Variables ¶
This section is empty.
Functions ¶
func ExecuteStandardized ¶
func NormalizePage ¶
func RequestIDSkipper ¶
func RequestIDSkipper(skip bool) echomiddleware.Skipper
func SafeLogFieldsExtractor ¶ added in v1.3.0
func ServiceUnavailableError ¶
Types ¶
type APIResponse ¶
type APIResponse[T any] struct { RequestID string `example:"3bf74527-8097-4217-8485-ffe05d16f82e" json:"requestId,omitempty"` Data T `json:"data"` Pagination *Pagination `json:"pagination,omitempty"` }
type HandlerFunc ¶
type HandlerResponse ¶
type HandlerResponse[T any] struct { Data T `json:"data"` Pagination *Pagination `json:"pagination,omitempty"` }
func NewPaginatedResponse ¶
func NewPaginatedResponse[T any](data T, pagination *Pagination) *HandlerResponse[T]
func NewResponse ¶
func NewResponse[T any](data T) *HandlerResponse[T]
type Pagination ¶
type Pagination struct {
Page int `example:"1" json:"page"`
PageSize int `example:"10" json:"pageSize"`
TotalCount int `example:"42" json:"totalCount"`
TotalPages int `example:"5" json:"totalPages"`
}
func NewPagination ¶
func NewPagination(page, pageSize, totalCount int) *Pagination
type ResponseError ¶
func (ResponseError) Error ¶
func (e ResponseError) Error() string
Click to show internal directories.
Click to hide internal directories.