server

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2025 License: MIT Imports: 16 Imported by: 2

README

server

HTTP/HTTPS server with advanced features built on Echo framework.

Installing

go get github.com/alexferl/golib/http/server

Usage

See examples/ for usage.

Documentation

Index

Constants

View Source
const (
	ServerName                         = "server-name"
	ServerVersion                      = "server-version"
	ServerGracefulTimeout              = "server-graceful-timeout"
	ServerHTTPBindAddr                 = "server-http-bind-addr"
	ServerHTTPIdleTimeout              = "server-http-idle-timeout"
	ServerHTTPReadTimeout              = "server-http-read-timeout"
	ServerHTTPReadHeaderTimeout        = "server-http-read-header-timeout"
	ServerHTTPWriteTimeout             = "server-http-write-timeout"
	ServerHTTPMaxHeaderBytes           = "server-http-max-header-bytes"
	ServerTLSEnabled                   = "server-tls-enabled"
	ServerTLSBindAddr                  = "server-tls-bind-addr"
	ServerTLSCertFile                  = "server-tls-cert-file"
	ServerTLSKeyFile                   = "server-tls-key-file"
	ServerTLSACMEEnabled               = "server-tls-acme-enabled"
	ServerTLSACMEEmail                 = "server-tls-acme-email"
	ServerTLSACMEHostWhitelist         = "server-tls-acme-host-whitelist"
	ServerTLSACMECachePath             = "server-tls-acme-cache-path"
	ServerTLSACMEDirectoryURL          = "server-tls-acme-directory-url"
	ServerCompressEnabled              = "server-compress-enabled"
	ServerCompressLevel                = "server-compress-level"
	ServerCompressMinLength            = "server-compress-min-length"
	ServerRedirectHTTPS                = "server-redirect-https"
	ServerRedirectCode                 = "server-redirect-code"
	ServerHealthcheckLivenessEndpoint  = "server-healthcheck-liveness-endpoint"
	ServerHealthcheckReadinessEndpoint = "server-healthcheck-readiness-endpoint"
	ServerHealthcheckStartupEndpoint   = "server-healthcheck-startup-endpoint"
	ServerPrometheusEnabled            = "server-prometheus-enabled"
	ServerPrometheusPath               = "server-prometheus-path"
)

Variables

View Source
var DefaultConfig = &Config{
	Name:            "app",
	Version:         "1.0.0",
	GracefulTimeout: 30 * time.Second,
	HTTP: HTTPConfig{
		BindAddr:          "localhost:8080",
		MaxHeaderBytes:    1 << 20,
		IdleTimeout:       60 * time.Second,
		ReadTimeout:       10 * time.Second,
		ReadHeaderTimeout: 5 * time.Second,
		WriteTimeout:      10 * time.Second,
	},
	TLS: TLSConfig{
		Enabled:  false,
		BindAddr: "localhost:8443",
		CertFile: "",
		KeyFile:  "",
		ACME: ACMEConfig{
			Enabled:       false,
			Email:         "",
			CachePath:     "./certs",
			HostWhitelist: []string{},
			DirectoryURL:  "",
		},
	},
	Compress: CompressConfig{
		Enabled:   false,
		Level:     6,
		MinLength: 1024,
	},
	Redirect: RedirectConfig{
		HTTPS: false,
		Code:  http.StatusMovedPermanently,
	},
	Healthcheck: HealthcheckConfig{
		LivenessEndpoint:  "/livez",
		LivenessHandler:   defaultHealthcheckHandler,
		ReadinessEndpoint: "/readyz",
		ReadinessHandler:  defaultHealthcheckHandler,
		StartupEndpoint:   "/startupz",
		StartupHandler:    defaultHealthcheckHandler,
	},
	Prometheus: PrometheusConfig{
		Enabled: false,
		Path:    "/metrics",
	},
}

DefaultConfig provides default server configuration.

Functions

This section is empty.

Types

type ACMEConfig

type ACMEConfig struct {
	// Enabled indicates whether ACME/Let's Encrypt is enabled.
	// Optional. Default value false.
	Enabled bool

	// Email specifies the ACME email address.
	// Optional. Default value "".
	Email string

	// HostWhitelist specifies the ACME host whitelist.
	// Optional. Default value empty slice.
	HostWhitelist []string

	// CachePath specifies the ACME cache path.
	// Optional. Default value "./certs".
	CachePath string

	// DirectoryURL specifies the ACME directory URL.
	// Optional. Default value "".
	DirectoryURL string
}

ACMEConfig holds ACME/Let's Encrypt configuration.

type CompressConfig

type CompressConfig struct {
	// Enabled indicates whether compression is enabled.
	// Optional. Default value false.
	Enabled bool

	// Level specifies the compression level.
	// Optional. Default value 6.
	Level int

	// MinLength specifies the minimum length for compression.
	// Optional. Default value 1024.
	MinLength int
}

CompressConfig holds compression configuration.

type Config

type Config struct {
	// Name specifies the application name.
	// Optional. Default value "app".
	Name string

	// Version specifies the application version.
	// Optional. Default value "1.0.0".
	Version string

	// GracefulTimeout specifies the duration for graceful shutdown.
	// Optional. Default value 30 seconds.
	GracefulTimeout time.Duration

	// HTTP holds HTTP server configuration.
	// Optional. Default value with localhost:8080 bind address.
	HTTP HTTPConfig

	// TLS holds TLS/HTTPS server configuration.
	// Optional. Default value with TLS disabled.
	TLS TLSConfig

	// Compress holds compression configuration.
	// Optional. Default value with compression disabled.
	Compress CompressConfig

	// Redirect holds redirect configuration.
	// Optional. Default value with HTTPS redirect disabled.
	Redirect RedirectConfig

	// Healthcheck holds healthcheck configuration.
	// Optional. Default value with standard endpoints enabled.
	Healthcheck HealthcheckConfig

	// Prometheus holds Prometheus metrics configuration.
	// Optional. Default value with metrics disabled.
	Prometheus PrometheusConfig
}

Config holds configuration for the HTTP server.

func (*Config) FlagSet

func (c *Config) FlagSet() *pflag.FlagSet

FlagSet returns a pflag.FlagSet for CLI configuration.

type HTTPConfig

type HTTPConfig struct {
	// BindAddr specifies the HTTP bind address.
	// Optional. Default value "localhost:8080".
	BindAddr string

	// IdleTimeout specifies the HTTP idle timeout.
	// Optional. Default value 60 seconds.
	IdleTimeout time.Duration

	// ReadTimeout specifies the HTTP read timeout.
	// Optional. Default value 10 seconds.
	ReadTimeout time.Duration

	// ReadHeaderTimeout specifies the HTTP read header timeout.
	// Optional. Default value 5 seconds.
	ReadHeaderTimeout time.Duration

	// WriteTimeout specifies the HTTP write timeout.
	// Optional. Default value 10 seconds.
	WriteTimeout time.Duration

	// MaxHeaderBytes specifies the maximum header bytes.
	// Optional. Default value 1MB.
	MaxHeaderBytes int
}

HTTPConfig holds HTTP server configuration.

type HealthcheckConfig added in v0.2.0

type HealthcheckConfig struct {
	// LivenessEndpoint specifies the liveness check endpoint.
	// Optional. Default value "/livez".
	LivenessEndpoint string

	// LivenessHandler specifies the liveness check handler.
	// Optional. Default value returns 200 OK.
	LivenessHandler echo.HandlerFunc

	// ReadinessEndpoint specifies the readiness check endpoint.
	// Optional. Default value "/readyz".
	ReadinessEndpoint string

	// ReadinessHandler specifies the readiness check handler.
	// Optional. Default value returns 200 OK.
	ReadinessHandler echo.HandlerFunc

	// StartupEndpoint specifies the startup check endpoint.
	// Optional. Default value "/startupz".
	StartupEndpoint string

	// StartupHandler specifies the startup check handler.
	// Optional. Default value returns 200 OK.
	StartupHandler echo.HandlerFunc
}

HealthcheckConfig holds healthcheck endpoint configuration.

type Option

type Option func(*Server)

Option is a function that configures a Server.

func WithEchoConfig

func WithEchoConfig(configFunc func(*echo.Echo)) Option

WithEchoConfig allows custom Echo configuration.

func WithLogger

func WithLogger(logger *logger.Logger) Option

WithLogger sets a custom logger for the server.

func WithMiddleware

func WithMiddleware(middlewares ...echo.MiddlewareFunc) Option

WithMiddleware adds middleware to the Echo instance.

type PrometheusConfig added in v0.3.0

type PrometheusConfig struct {
	// Enabled indicates whether Prometheus metrics are enabled.
	// Optional. Default value false.
	Enabled bool

	// Path specifies the HTTP path for Prometheus metrics.
	// Optional. Default value "/metrics".
	Path string
}

PrometheusConfig holds Prometheus metrics configuration.

type RedirectConfig

type RedirectConfig struct {
	// HTTPS indicates whether to redirect HTTP to HTTPS.
	// Optional. Default value false.
	HTTPS bool

	// Code specifies the redirect status code.
	// Optional. Default value 301 (Moved Permanently).
	Code int
}

RedirectConfig holds redirect configuration.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server represents an HTTP/HTTPS server with configurable middleware and TLS support.

func New

func New(config Config, options ...Option) *Server

New creates a new Server instance with the given configuration and options.

func (*Server) Echo

func (s *Server) Echo() *echo.Echo

Echo returns the underlying Echo instance.

func (*Server) Logger

func (s *Server) Logger() *logger.Logger

Logger returns the logger instance used by the server.

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

Shutdown gracefully shuts down the HTTP and HTTPS servers.

func (*Server) Start

func (s *Server) Start() <-chan error

Start starts the HTTP or HTTPS server and returns a channel for errors.

type TLSConfig

type TLSConfig struct {
	// Enabled indicates whether TLS/HTTPS is enabled.
	// Optional. Default value false.
	Enabled bool

	// BindAddr specifies the TLS bind address.
	// Optional. Default value "localhost:8443".
	BindAddr string

	// CertFile specifies the TLS certificate file path.
	// Optional. Default value "".
	CertFile string

	// KeyFile specifies the TLS key file path.
	// Optional. Default value "".
	KeyFile string

	// ACME holds ACME/Let's Encrypt configuration.
	// Optional. Default value with ACME disabled.
	ACME ACMEConfig
}

TLSConfig holds TLS/HTTPS server configuration.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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