server

package
v0.23.0 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package server provides an HTTP/HTTPS server for Tinkerbell.

Index

Constants

View Source
const (
	// DefaultReadTimeout is the maximum duration for reading the entire request.
	DefaultReadTimeout = 30 * time.Second
	// DefaultReadHeaderTimeout is the maximum duration for reading request headers.
	DefaultReadHeaderTimeout = 10 * time.Second
	// DefaultWriteTimeout is the maximum duration before timing out writes of the response.
	DefaultWriteTimeout = 30 * time.Second
	// DefaultIdleTimeout is the maximum duration for keep-alive connections.
	DefaultIdleTimeout = 120 * time.Second
	// DefaultShutdownTimeout is the maximum duration for graceful shutdown.
	DefaultShutdownTimeout = 30 * time.Second
	// DefaultMaxHeaderBytes is the maximum size of request headers.
	DefaultMaxHeaderBytes = 1 << 20 // 1 MB
)

Variables

This section is empty.

Functions

func WithHTTPEnabled

func WithHTTPEnabled(enable bool) func(*Route)

WithHTTPEnabled controls whether the route is registered on the HTTP mux.

func WithHTTPSEnabled

func WithHTTPSEnabled(enable bool) func(*Route)

WithHTTPSEnabled controls whether the route is registered on the HTTPS mux.

func WithRewriteHTTPToHTTPS

func WithRewriteHTTPToHTTPS(enable bool) func(*Route)

WithRewriteHTTPToHTTPS causes the HTTP mux to serve a redirect to the HTTPS equivalent instead of the handler itself (only when TLS is enabled).

Types

type Config

type Config struct {
	// BindAddr is the IP address to bind to.
	BindAddr string
	// BindPort is the port for the HTTP server.
	BindPort int
	// TLSCerts are in-memory TLS certificates. Must be provided to enable the HTTPS server.
	TLSCerts []tls.Certificate
	// HTTPSPort is the optional port for an HTTPS server.
	HTTPSPort int
	// ReadTimeout is the maximum duration for reading the entire request.
	ReadTimeout time.Duration
	// ReadHeaderTimeout is the maximum duration for reading request headers.
	ReadHeaderTimeout time.Duration
	// WriteTimeout is the maximum duration before timing out writes of the response.
	WriteTimeout time.Duration
	// IdleTimeout is the maximum duration for keep-alive connections.
	IdleTimeout time.Duration
	// MaxHeaderBytes is the maximum size of request headers.
	MaxHeaderBytes int
	// ShutdownTimeout is the maximum duration for graceful shutdown.
	ShutdownTimeout time.Duration
}

Config is the configuration for the HTTP/HTTPS server.

func NewConfig

func NewConfig(opts ...Option) *Config

NewConfig returns a Config with sensible defaults, modified by the given options.

func (*Config) Serve

func (c *Config) Serve(ctx context.Context, log logr.Logger, httpHandler http.Handler, httpsHandler http.Handler) error

Serve starts the HTTP server (and optionally an HTTPS server) and blocks until ctx is cancelled. It performs a graceful shutdown when ctx is cancelled.

type Option

type Option func(*Config)

Option configures a Config.

type Route

type Route struct {
	Pattern            string       `json:"pattern"`
	Description        string       `json:"description"`
	RewriteHTTPToHTTPS bool         `json:"rewriteHttpToHttps"`
	HTTPEnabled        bool         `json:"httpEnabled"`
	HTTPSEnabled       bool         `json:"httpsEnabled"`
	Handler            http.Handler `json:"-"`
}

Route represents an HTTP route with its pattern, description, and handler.

type Routes

type Routes []Route

Routes is a collection of Route objects that can be registered with an HTTP server.

func (Routes) HasHTTPSRoutes

func (rs Routes) HasHTTPSRoutes() bool

HasHTTPSRoutes reports whether any registered route has HTTPS enabled.

func (Routes) LogValue

func (rs Routes) LogValue() slog.Value

LogValue implements slog.LogValuer so that logging a Routes value emits all route metadata without the Handler field.

func (*Routes) Muxes

func (rs *Routes) Muxes(log logr.Logger, httpsPort int, tlsEnabled bool) (*http.ServeMux, *http.ServeMux)

Muxes builds and returns separate HTTP and HTTPS http.ServeMux instances from the registered routes. When tlsEnabled is false, routes marked with RewriteHTTPToHTTPS are served normally over HTTP instead of redirecting, so endpoints remain reachable in no-TLS deployments.

func (*Routes) Register

func (rs *Routes) Register(pattern string, hh http.Handler, desc string, options ...func(*Route))

Register adds a new route to the Routes collection for later registration with an HTTP server. This allows tracking endpoint patterns and their handlers for use with http.ServeMux.Handle. Useful for logging information about registered routes and their descriptions. HTTP is enabled by default, overridable with options.

Jump to

Keyboard shortcuts

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