httpserver

package
v1.3.12 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2026 License: MIT Imports: 14 Imported by: 0

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

View Source
const (
	ErrCodeValidation     = "VALIDATION_ERROR"
	ErrCodeNotFound       = "NOT_FOUND"
	ErrCodeUnauthorized   = "UNAUTHORIZED"
	ErrCodeForbidden      = "FORBIDDEN"
	ErrCodeConflict       = "CONFLICT"
	ErrCodeInternal       = "INTERNAL_ERROR"
	ErrCodeBadRequest     = "BAD_REQUEST"
	ErrCodeServiceUnavail = "SERVICE_UNAVAILABLE"
)
View Source
const (
	DefaultPage     = 1
	DefaultPageSize = 50
	MaxPageSize     = 100
)

Variables

This section is empty.

Functions

func BadRequestError

func BadRequestError(err error, details ...string) *echo.HTTPError

func ConflictError

func ConflictError(err error, details ...string) *echo.HTTPError

func ExecuteStandardized

func ExecuteStandardized[REQ any, RES any](
	c *echo.Context,
	request *REQ,
	handlerName string,
	delegate HandlerFunc[REQ, RES],
) (resp any, httpErr *echo.HTTPError)

func ForbiddenError

func ForbiddenError(err error, details ...string) *echo.HTTPError

func HTTPError

func HTTPError(code int, err error, details ...string) *echo.HTTPError

func InternalError

func InternalError(err error, details ...string) *echo.HTTPError

func NormalizePage

func NormalizePage(page, pageSize int) (normalizedPage, normalizedSize, offset int)

func NotFoundError

func NotFoundError(err error, details ...string) *echo.HTTPError

func RequestIDSkipper

func RequestIDSkipper(skip bool) echomiddleware.Skipper

func SafeLogFieldsExtractor added in v1.3.0

func SafeLogFieldsExtractor(ctx *echo.Context) map[string]any

func ServiceUnavailableError

func ServiceUnavailableError(err error, details ...string) *echo.HTTPError

func UnauthorizedError

func UnauthorizedError(err error, details ...string) *echo.HTTPError

func Wrapper

func Wrapper[TREQ any](wrapped func(*echo.Context, *TREQ) (any, *echo.HTTPError)) echo.HandlerFunc

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 Config

type Config struct {
	Host         string
	Port         int
	EnableCors   bool
	AllowOrigins []string
	BodyLimit    string
	ReadTimeout  time.Duration
	WriteTimeout time.Duration
	GracePeriod  time.Duration
}

type HandlerFunc

type HandlerFunc[REQ any, RES any] func(
	log zerolog.Logger,
	c *echo.Context,
	request *REQ,
) (*HandlerResponse[RES], *echo.HTTPError)

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

type ResponseError struct {
	Message  string `json:"message"`
	Internal string `json:"internal"`
}

func (ResponseError) Error

func (e ResponseError) Error() string

type Server

type Server struct {
	Echo *echo.Echo
	Root *echo.Group
	// contains filtered or unexported fields
}

func New

func New(cfg *Config) *Server

func (*Server) Name

func (s *Server) Name() string

func (*Server) Start

func (s *Server) Start(_ context.Context) error

func (*Server) Stop

func (s *Server) Stop() error

Jump to

Keyboard shortcuts

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