Documentation
¶
Index ¶
Constants ¶
const ( // ErrorParamEmpty indicates that one or more required parameters are empty. ErrorParamEmpty liberr.CodeError = iota + libcfg.MinErrorComponentHttp // ErrorParamInvalid indicates that one or more parameters have invalid values. ErrorParamInvalid // ErrorComponentNotInitialized indicates that the component has not been properly initialized. // This typically means Init() was not called or required dependencies are missing. ErrorComponentNotInitialized // ErrorConfigInvalid indicates that the component configuration is invalid. // This can occur when configuration validation fails or required fields are missing. ErrorConfigInvalid // ErrorComponentStart indicates a failure during component startup. // This can be caused by configuration errors, missing dependencies, or server startup failures. ErrorComponentStart // ErrorComponentReload indicates a failure during component reload. // This can occur when new configuration is invalid or servers fail to restart. ErrorComponentReload // ErrorDependencyTLSDefault indicates that the TLS component could not be retrieved. // This typically means the TLS component is not registered or the TLS key is incorrect. ErrorDependencyTLSDefault )
Error codes for the HTTP component package. These codes are based on the MinErrorComponentHttp offset defined in the config package.
const ( // ComponentType is the identifier for the HTTP component type. // This constant is used by the configuration system to identify HTTP components. ComponentType = "http" )
const ( // DefaultTlsKey is the default key used to reference the TLS component. // This key is used when no custom TLS key is provided during component creation. DefaultTlsKey = "t" )
Variables ¶
This section is empty.
Functions ¶
func DefaultConfig ¶ added in v1.8.10
DefaultConfig returns the default HTTP server configuration with optional indentation.
Parameters:
- indent: Indentation string for formatting (empty for compact, " " for 2-space indent, etc.)
Returns:
- JSON-encoded array of HTTP server configurations
The default configuration includes:
- Status HTTP server (port 6080)
- API HTTP server (port 7080)
- Metrics HTTP server (port 8080)
Each server includes monitor configuration, TLS settings, and logger configuration.
Example:
// Get compact JSON
cfg := http.DefaultConfig("")
// Get formatted JSON with 2-space indentation
cfg := http.DefaultConfig(" ")
func Register ¶
Register registers an existing HTTP component with the configuration system.
Parameters:
- cfg: The configuration instance
- key: The key to use for this component
- cpt: The HTTP component to register
This function is typically used when you have created a component with New() and want to register it in the configuration system.
func RegisterNew ¶
func RegisterNew(ctx context.Context, cfg libcfg.Config, key string, tlsKey string, hdl srvtps.FuncHandler)
RegisterNew creates a new HTTP component and registers it with the configuration system.
Parameters:
- ctx: Function that returns the context for the component
- cfg: The configuration instance
- key: The key to use for this component
- tlsKey: Key to reference the TLS component
- hdl: Function that returns HTTP handlers for different routes
This is a convenience function that combines New() and Register().
func SetDefaultConfig ¶
func SetDefaultConfig(cfg []byte)
SetDefaultConfig replaces the default HTTP server configuration. This function is useful for customizing default server configurations in different deployment environments.
Parameters:
- cfg: JSON-encoded array of HTTP server configurations
The configuration should be a JSON array of server configurations. Each server configuration should include fields like name, handler_key, listen address, expose URL, TLS settings, etc.
Example:
customCfg := []byte(`[
{
"name": "api",
"handler_key": "api",
"listen": "0.0.0.0:8080",
"expose": "https://api.example.com"
}
]`)
http.SetDefaultConfig(customCfg)
Types ¶
type CptHttp ¶ added in v1.19.0
type CptHttp interface {
cfgtps.Component
// SetTLSKey sets the key used to reference the TLS component.
// This key is used to load TLS configuration from the component registry.
SetTLSKey(tlsKey string)
// SetHandler sets the function that returns HTTP handlers for different routes.
// The handler function is called when building the server pool.
SetHandler(fct srvtps.FuncHandler)
// GetPool returns the current HTTP server pool.
// Returns nil if the pool has not been initialized.
GetPool() htpool.Pool
// SetPool sets the HTTP server pool.
// If nil is passed, a new pool is created automatically.
SetPool(pool htpool.Pool)
}
CptHttp represents an HTTP server component that manages HTTP/HTTPS server pools. It extends the base Component interface with HTTP-specific functionality including TLS configuration, handler management, and server pool operations.
The component supports:
- Multiple HTTP/HTTPS servers with different configurations
- Dynamic TLS configuration through component dependencies
- Custom HTTP handlers for different routes
- Server pool lifecycle management (start, stop, reload)
- Health monitoring integration
Usage:
cpt := http.New(ctx, "tls-key", handlerFunc)
cpt.Init("http-server", ctx, getCpt, vpr, vrs, log)
err := cpt.Start()
if err != nil {
// Handle error
}
defer cpt.Stop()
func Load ¶
func Load(getCpt cfgtps.FuncCptGet, key string) CptHttp
Load retrieves an HTTP component from the configuration system by its key.
Parameters:
- getCpt: Function to retrieve components by key
- key: The key of the component to load
Returns:
- CptHttp: The HTTP component if found and of correct type, nil otherwise
This function performs type checking and returns nil if the component is not found or is not of type CptHttp.
func New ¶
New creates a new HTTP component instance with the specified context, TLS key, and handler function.
Parameters:
- ctx: Function that returns the context for the component
- tlsKey: Key to reference the TLS component (uses DefaultTlsKey if empty)
- hdl: Function that returns HTTP handlers for different routes (can be nil)
Returns:
- CptHttp: A new HTTP component instance
Example:
ctx := func() context.Context { return context.Background() }
handler := func() map[string]http.Handler {
return map[string]http.Handler{
"api": apiHandler,
"status": statusHandler,
}
}
cpt := http.New(ctx, "my-tls", handler)