httpserver

package
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultHost              = "127.0.0.1"
	DefaultReadTimeout       = time.Second * 30
	DefaultReadHeaderTimeout = time.Second * 10
	DefaultWriteTimeout      = time.Second * 30
	DefaultIdleTimeout       = time.Second * 120
	DefaultPort              = 8080
)
View Source
const StartupCheckTimeout = 100 * time.Millisecond

Variables

View Source
var (
	ErrInvalidHost     = errors.New("httpserver: host must be a valid network address")
	ErrInvalidBasePath = errors.New(
		"httpserver: base path must be an absolute path without trailing slash",
	)
	ErrMissingCertFile = errors.New(
		"httpserver: cert file must be specified if key file is specified",
	)
	ErrMissingKeyFile = errors.New(
		"httpserver: key file must be specified if cert file is specified",
	)
	ErrUnreadableCertFile       = errors.New("httpserver: cert file must be readable")
	ErrUnreadableKeyFile        = errors.New("httpserver: key file must be readable")
	ErrInvalidReadTimeout       = errors.New("httpserver: read timeout must be positive")
	ErrInvalidReadHeaderTimeout = errors.New("httpserver: read header timeout must be positive")
	ErrInvalidPort              = errors.New("httpserver: port must be between 1 and 65535")
)
View Source
var (
	ErrInvalidContext = errors.New("httpserver: context must not be nil or cancelled")
)
View Source
var ErrInvalidFileHeader = errors.New("invalid file header")

Functions

func GetFormValue added in v1.2.2

func GetFormValue[T any](
	req *http.Request,
	key string,
	defaultValue T,
	validateFunc func(T) error,
) (T, error)

GetFormValue retrieves and converts a form value from an HTTP request to the specified type. If the key is empty or the form value is missing, the defaultValue is returned. The validateFunc is optional and performs additional validation on the value.

func GetQueryParam added in v1.2.2

func GetQueryParam[T any](
	req *http.Request,
	key string,
	defaultValue T,
	validateFunc func(T) error,
) (T, error)

GetQueryParam retrieves and converts a query parameter from an HTTP request to the specified type. If the key is empty or the form value is missing, the defaultValue is returned. The validateFunc is optional and performs additional validation on the value.

func SaveBodyToFile added in v1.2.2

func SaveBodyToFile(
	resp http.ResponseWriter,
	req *http.Request,
	magicBytes []byte,
) (dir, filePath string, err error)

Types

type Config

type Config struct {
	// Host represents network host address.
	Host string `json:"host" yaml:"host"`

	// BasePath represents the prefixed path in the URL.
	BasePath string `json:"basePath" yaml:"basePath"`

	// CertFile represents the path to the certificate file.
	CertFile string `json:"certFile" yaml:"certFile"`

	// KeyFile represents the path to the key file.
	KeyFile string `json:"keyFile" yaml:"keyFile"`

	// ReadTimeout represents the maximum duration before timing out read of the request.
	ReadTimeout time.Duration `json:"readTimeout" yaml:"readTimeout"`

	// ReadHeaderTimeout represents the amount of time allowed to read request headers.
	ReadHeaderTimeout time.Duration `json:"readHeaderTimeout" yaml:"readHeaderTimeout"`

	// WriteTimeout represents the maximum duration before timing out writes of the response.
	WriteTimeout time.Duration `json:"writeTimeout" yaml:"writeTimeout"`

	// IdleTimeout represents the maximum amount of time to wait for the next request when keep-alive is enabled.
	IdleTimeout time.Duration `json:"idleTimeout" yaml:"idleTimeout"`

	// Port specifies the port to be used for connections.
	Port int `json:"port" yaml:"port"`

	// EnableH2C indicates whether HTTP/2 Cleartext (H2C) protocol support is enabled for the Server.
	// Use this only if you have configured a reverse proxy that terminates TLS.
	EnableH2C bool `json:"enableH2C" yaml:"enableH2C"`
}

Config defines the essential parameters for serving an http Server.

func (*Config) SetDefaults

func (r *Config) SetDefaults()

SetDefaults initializes the default values for the relevant fields in the struct.

func (*Config) Validate

func (r *Config) Validate() error

Validate ensures the all necessary configurations are filled and within valid confines.

type HTTPServer

type HTTPServer struct {
	Log log.Logger

	Server *http.Server
	// contains filtered or unexported fields
}

func New

func New(cfg *Config, opts ...Option) *HTTPServer

func (*HTTPServer) Start

func (s *HTTPServer) Start(ctx context.Context) error

func (*HTTPServer) Stop

func (s *HTTPServer) Stop(ctx context.Context) error

type Middleware

type Middleware func(http.Handler) http.Handler

type Option

type Option func(*HTTPServer)

Option is a functional option for configuring HTTPServer.

func WithHandler

func WithHandler(handler http.Handler) Option

func WithLogger

func WithLogger(logger log.Logger) Option

type Router

type Router struct {
	*http.ServeMux
	// contains filtered or unexported fields
}

func NewRouter

func NewRouter() *Router

func (*Router) Group

func (r *Router) Group(configure func(r *Router))

func (*Router) Handle

func (r *Router) Handle(pattern string, handler http.Handler)

func (*Router) HandleFunc

func (r *Router) HandleFunc(pattern string, handler http.HandlerFunc)

func (*Router) ServeHTTP

func (r *Router) ServeHTTP(resp http.ResponseWriter, req *http.Request)

func (*Router) Use

func (r *Router) Use(middlewares ...Middleware)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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