Documentation
¶
Index ¶
- Constants
- Variables
- func GetFormValue[T any](req *http.Request, key string, defaultValue T, validateFunc func(T) error) (T, error)
- func GetQueryParam[T any](req *http.Request, key string, defaultValue T, validateFunc func(T) error) (T, error)
- func SaveBodyToFile(resp http.ResponseWriter, req *http.Request, magicBytes []byte) (dir, filePath string, err error)
- type Config
- type HTTPServer
- type Middleware
- type Option
- type Router
- func (r *Router) Group(configure func(r *Router))
- func (r *Router) Handle(pattern string, handler http.Handler)
- func (r *Router) HandleFunc(pattern string, handler http.HandlerFunc)
- func (r *Router) ServeHTTP(resp http.ResponseWriter, req *http.Request)
- func (r *Router) Use(middlewares ...Middleware)
Constants ¶
const ( DefaultHost = "127.0.0.1" DefaultReadTimeout = time.Second * 30 DefaultReadHeaderTimeout = time.Second * 10 DefaultWriteTimeout = time.Second * 30 DefaultIdleTimeout = time.Second * 120 DefaultPort = 8080 )
const StartupCheckTimeout = 100 * time.Millisecond
Variables ¶
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") )
var (
ErrInvalidContext = errors.New("httpserver: context must not be nil or cancelled")
)
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
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.
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
type Option ¶
type Option func(*HTTPServer)
Option is a functional option for configuring HTTPServer.
func WithHandler ¶
func WithLogger ¶
type Router ¶
func (*Router) HandleFunc ¶
func (r *Router) HandleFunc(pattern string, handler http.HandlerFunc)
func (*Router) Use ¶
func (r *Router) Use(middlewares ...Middleware)