httpserver

package module
v0.0.0-...-37c31ec Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2024 License: MIT Imports: 13 Imported by: 1

Documentation

Index

Constants

View Source
const (
	DefaultHost              = "127.0.0.1"
	DefaultBasePath          = "/"
	DefaultReadTimeout       = time.Second * 30
	DefaultReadHeaderTimeout = time.Second * 10
	DefaultPort              = 8080
)

Variables

View Source
var (
	ErrNoHost                   = errors.New("host cannot be empty")
	ErrNoBasePath               = errors.New("base path must be an absolute path")
	ErrInvalidBasePath          = errors.New("base path must end with a trailing slash")
	ErrNoCertFile               = errors.New("key_file is set but cert_file is empty")
	ErrNoKeyFile                = errors.New("cert_file is set but key_file is empty")
	ErrInvalidReadTimeout       = errors.New("read_timeout must be greater than 0")
	ErrInvalidReadHeaderTimeout = errors.New("read_header_timeout must be greater than 0")
	ErrInvalidPort              = errors.New("port must be a number between 1 and 65535")
	ErrNoLogger                 = errors.New("logger cannot be empty")
)

Functions

func Logger

func Logger(log *logger.Logger) gin.HandlerFunc

Types

type Config

type Config struct {

	// Host represents network host address.
	Host string `json:"host" yaml:"host" mapstructure:"host"`

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

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

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

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

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

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

	// Logger specifies the used logger instance.
	Logger *logger.Logger
}

Config defines the essential parameters for serving a Lambda broker service.

func NewConfig

func NewConfig(log *logger.Logger) *Config

NewConfig creates and returns a new Config having default values.

func (*Config) Validate

func (r *Config) Validate() error

Validate ensures the all necessary configurations are filled and within valid confines. This includes checks for host, certificates, port, and timeouts. Any misconfiguration results in well-defined standardized errors.

type HTTPServer

type HTTPServer struct {

	// Engine is an instance from Gin web framework for Go to handle HTTP requests.
	Engine *gin.Engine
	// contains filtered or unexported fields
}

HTTPServer encapsulates an HTTP server with some additional features.

func NewHTTPServer

func NewHTTPServer(config *Config) *HTTPServer

NewHTTPServer creates a new instance of HTTPServer with given configuration.

func (*HTTPServer) SetEngine

func (r *HTTPServer) SetEngine(engine *gin.Engine)

func (*HTTPServer) Start

func (r *HTTPServer) Start()

Start function starts the HTTP server in a separate goroutine.

func (*HTTPServer) Stop

func (r *HTTPServer) Stop()

Stop function stops the HTTP server gracefully within a second time limit.

type LoggerItem

type LoggerItem struct {
	ClientIP   string        `json:"client-ip"`
	Errors     string        `json:"errors"`
	Method     string        `json:"method"`
	Path       string        `json:"path"`
	Latency    time.Duration `json:"latency"`
	Size       int           `json:"size"`
	StatusCode int           `json:"status-code"`
}

func (LoggerItem) MethodColor

func (r LoggerItem) MethodColor() string

func (LoggerItem) StatusCodeColor

func (r LoggerItem) StatusCodeColor() string

func (LoggerItem) String

func (r LoggerItem) String() string

Jump to

Keyboard shortcuts

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