server

package
v1.0.14 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 31 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package server provides generic HTTP and gRPC server implementations with configuration-driven setup, lifecycle management, and graceful shutdown support.

Index

Constants

View Source
const (
	// RecommendedHomeDir defines the default directory used to place all service configurations.
	RecommendedHomeDir = ".component"

	// RecommendedEnvPrefix defines the ENV prefix used by all services.
	RecommendedEnvPrefix = "COMPONENT"
)
View Source
const (
	// ServerStateCreated indicates the server is created but not yet running.
	ServerStateCreated int32 = iota
	// ServerStateRunning indicates the server is currently running.
	ServerStateRunning
	// ServerStateClosing indicates the server is in the process of closing.
	ServerStateClosing
	// ServerStateStopped indicates the server has been stopped.
	ServerStateStopped
)

Server state constants.

Variables

This section is empty.

Functions

func LoadConfig

func LoadConfig(cfg string, defaultName string, homeDir string, envPrefix string)

LoadConfig reads in config file and ENV variables if set.

Types

type APIServerBizHooks

type APIServerBizHooks[Svc any, Store any] interface {
	// InitStore initializes the storage layer.
	InitStore() (Store, error)

	// InitService initializes the service layer with the store instance.
	InitService(Store) (Svc, error)

	// InitController registers all routes and controllers with the router.
	InitController(router *gin.Engine, svc Svc) error

	// RegisteredMiddlewares returns the middleware registry for the service type.
	RegisteredMiddlewares() (mwv1.Middlewares[Svc], error)

	// Startup is called after all layers are initialized and before the server starts listening.
	// Use this hook to start business resources such as connection pools, background workers, or schedulers.
	Startup() error

	// Shutdown is called during server graceful shutdown to clean up business resources.
	// Use this hook to stop background workers, close connections, or release resources.
	Shutdown() error
}

APIServerBizHooks defines the business lifecycle hooks for the API server.

type APIServerConfig

type APIServerConfig[Svc any, Store any] struct {
	SecureServing   *SecureServingInfo
	InsecureServing *InsecureServingInfo
	Middlewares     []string
	Healthz         bool
	EnableProfiling bool
	EnableMetrics   bool
	Mode            string
	HealthCheckPath string
	PingTimeout     time.Duration
	ShutdownTimeout time.Duration
}

APIServerConfig is a structure used to configure a GenericAPIServer. Its members are sorted roughly in order of importance for composers.

func NewAPIServerConfig

func NewAPIServerConfig[Svc any, Store any]() *APIServerConfig[Svc, Store]

NewAPIServerConfig returns a APIServerConfig struct with the default values.

func (*APIServerConfig[Svc, Store]) Complete

func (c *APIServerConfig[Svc, Store]) Complete() CompletedAPIServerConfig[Svc, Store]

Complete fills in any fields not set that are required to have valid data and can be derived from other fields. If you're going to `ApplyOptions`, do that first. It's mutating the receiver.

type CertKey

type CertKey struct {
	// CertFile is a file containing a PEM-encoded certificate, and possibly the complete certificate chain
	CertFile string
	// KeyFile is a file containing a PEM-encoded private key for the certificate specified by CertFile
	KeyFile string
}

CertKey contains configuration items related to certificate.

type CompletedAPIServerConfig

type CompletedAPIServerConfig[Svc any, Store any] struct {
	*APIServerConfig[Svc, Store]
}

CompletedAPIServerConfig is the completed configuration for GenericAPIServer.

func (CompletedAPIServerConfig[Svc, Store]) NewAPIServer

func (c CompletedAPIServerConfig[Svc, Store]) NewAPIServer(ctx context.Context, bizHooks APIServerBizHooks[Svc, Store]) *GenericAPIServer[Svc, Store]

NewAPIServer returns a new GenericAPIServer instance from the given config.

type CompletedGRPCServerConfig

type CompletedGRPCServerConfig[Txp, Ep, Svc, Store any] struct {
	*GRPCServerConfig[Txp, Ep, Svc, Store]
}

CompletedGRPCServerConfig is the completed configuration for GenericGRPCServer.

func (CompletedGRPCServerConfig[Txp, Ep, Svc, Store]) NewGRPCServer

func (c CompletedGRPCServerConfig[Txp, Ep, Svc, Store]) NewGRPCServer(ctx context.Context, bizHooks GRPCServerBizHooks[Txp, Ep, Svc, Store]) *GenericGRPCServer[Txp, Ep, Svc, Store]

NewGRPCServer returns a new GenericGRPCServer instance from the given config.

type GRPCServerBizHooks

type GRPCServerBizHooks[Txp, Ep, Svc, Store any] interface {
	// InitStore initializes the storage layer.
	InitStore() (Store, error)

	// InitService initializes the service layer with the store instance.
	InitService(Store) (Svc, error)

	// RegisteredMiddlewares returns the middleware registry for the service type.
	RegisteredMiddlewares() (grpcmwv1.Middlewares[Svc], error)

	// InitEndpoints initializes the endpoint layer from the middleware-wrapped service factory.
	InitEndpoints(svc Svc) (Ep, error)

	// InitTransport initializes the transport layer from the endpoint set. chunkSize
	// is injected from the generic server config (GRPCServing.ChunkSize).
	InitTransport(eps Ep, chunkSize int) (Txp, error)

	// ServiceRegistrations declares the business services to register from the
	// initialized transport factory. It is the gRPC counterpart of
	// APIServerBizHooks.InitController: the framework service_register package turns
	// the declarations into register funcs (owning the registration control flow),
	// and the generic server registers them to all configured servings and to the RA.
	ServiceRegistrations(txp Txp) ([]svcregisterv1.ServiceRegistration, error)

	// Startup is called after all layers are initialized and before the server starts listening.
	// Use this hook to start business resources such as connection pools, background workers, or schedulers.
	Startup() error

	// Shutdown is called during server graceful shutdown to clean up business resources.
	// Use this hook to stop background workers, close connections, or release resources.
	Shutdown() error
}

GRPCServerBizHooks defines the business lifecycle hooks for the gRPC server. Ep is the endpoint-layer aggregate and Txp is the transport-layer factory; both are initialized under the generic server's control flow and injected into the downstream hooks.

type GRPCServerConfig

type GRPCServerConfig[Txp, Ep, Svc, Store any] struct {
	Namespace       string
	SecureServing   *SecureServingInfo
	InsecureServing *InsecureServingInfo
	RA              *RAInfo
	GRPCServing     *GRPCServingInfo
	Middlewares     []string
	Healthz         bool
	EnableProfiling bool
	EnableMetrics   bool
	PingTimeout     time.Duration
	ShutdownTimeout time.Duration
}

GRPCServerConfig is a structure used to configure a GenericGRPCServer. Its members are sorted roughly in order of importance for composers. The endpoint/transport type parameters are carried here so that the config chain can fluently build the server via CompletedGRPCServerConfig.NewGRPCServer.

func NewGRPCServerConfig

func NewGRPCServerConfig[Txp, Ep, Svc, Store any]() *GRPCServerConfig[Txp, Ep, Svc, Store]

NewGRPCServerConfig returns a GRPCServerConfig struct with the default values.

func (*GRPCServerConfig[Txp, Ep, Svc, Store]) Complete

func (c *GRPCServerConfig[Txp, Ep, Svc, Store]) Complete() CompletedGRPCServerConfig[Txp, Ep, Svc, Store]

Complete fills in any fields not set that are required to have valid data and can be derived from other fields. If you're going to `ApplyOptions`, do that first. It's mutating the receiver.

type GRPCServingInfo

type GRPCServingInfo struct {
	ChunkSize    int
	RecvTimeoutM int
}

GRPCServingInfo holds configuration for gRPC serving.

func NewGRPCServingInfo

func NewGRPCServingInfo() *GRPCServingInfo

NewGRPCServingInfo returns a GRPCServingInfo with default values.

type GenericAPIServer

type GenericAPIServer[Svc any, Store any] struct {

	// SecureServingInfo holds configuration of the TLS server.
	SecureServingInfo *SecureServingInfo

	// InsecureServingInfo holds configuration of the insecure http server.
	InsecureServingInfo *InsecureServingInfo

	// HealthCheckPath is the path for the health check endpoint.
	HealthCheckPath string

	// PingTimeout is the timeout duration for health check pings.
	PingTimeout time.Duration

	// EnableMetrics indicates whether the metrics endpoint is enabled.
	EnableMetrics bool

	// Router is the gin engine.
	Router *gin.Engine

	// ShutdownTimeout is the timeout used for server shutdown.
	ShutdownTimeout time.Duration
	// contains filtered or unexported fields
}

GenericAPIServer contains state for a generic API server.

func (*GenericAPIServer[Svc, Store]) Close

func (s *GenericAPIServer[Svc, Store]) Close() error

Close gracefully shuts down the API server. Safe to call multiple times; concurrent calls are idempotent.

func (*GenericAPIServer[Svc, Store]) PrepareRun

func (s *GenericAPIServer[Svc, Store]) PrepareRun() PreparedGenericAPIServer

PrepareRun prepares the server for running by installing services and registering shutdown callbacks.

type GenericGRPCServer

type GenericGRPCServer[Txp, Ep, Svc, Store any] struct {

	// SecureServingInfo holds configuration of the TLS server.
	SecureServingInfo *SecureServingInfo

	// InsecureServingInfo holds configuration of the insecure grpc server.
	InsecureServingInfo *InsecureServingInfo

	// RAInfo holds configuration of the Registration Authority server.
	RAInfo *RAInfo

	// ChunkSize set chunk size of the grpc server.
	ChunkSize int

	// ReceiveTimeout is the timeout used for the grpc server receiving data.
	ReceiveTimeout time.Duration

	// PingTimeout is the timeout duration for health check pings.
	PingTimeout time.Duration

	// ShutdownTimeout is the timeout used for server shutdown.
	ShutdownTimeout time.Duration
	// contains filtered or unexported fields
}

GenericGRPCServer contains state for a generic gRPC server.

func (*GenericGRPCServer[Txp, Ep, Svc, Store]) Close

func (s *GenericGRPCServer[Txp, Ep, Svc, Store]) Close() error

Close gracefully shuts down the gRPC server. Safe to call multiple times; concurrent calls are idempotent.

func (*GenericGRPCServer[Txp, Ep, Svc, Store]) PrepareRun

func (s *GenericGRPCServer[Txp, Ep, Svc, Store]) PrepareRun() PreparedGenericGRPCServer

PrepareRun prepares the server for running by installing services and registering shutdown callbacks.

type InsecureServingInfo

type InsecureServingInfo struct {
	BindAddress string
	BindPort    int
}

InsecureServingInfo holds configuration of the insecure http server.

func (*InsecureServingInfo) Address

func (i *InsecureServingInfo) Address() string

Address join host IP address and host port number into an address string.

type PreparedGenericAPIServer

type PreparedGenericAPIServer interface {
	Run() error
	Close() error
}

PreparedGenericAPIServer is the interface for a prepared server ready to run.

type PreparedGenericGRPCServer

type PreparedGenericGRPCServer interface {
	Run() error
	Close() error
}

PreparedGenericGRPCServer is the interface for a prepared server ready to run.

type RAInfo

type RAInfo struct {
	Host string
	Port int
}

RAInfo holds configuration of the Registration Authority server.

type SecureServingInfo

type SecureServingInfo struct {
	BindAddress string
	BindPort    int
	CertKey     CertKey
}

SecureServingInfo holds configuration of the TLS server.

func (*SecureServingInfo) Address

func (s *SecureServingInfo) Address() string

Address join host IP address and host port number into an address string, like: 0.0.0.0:8443.

Jump to

Keyboard shortcuts

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