Documentation
¶
Overview ¶
Package server provides generic HTTP and gRPC server implementations with configuration-driven setup, lifecycle management, and graceful shutdown support.
Index ¶
- Constants
- func LoadConfig(cfg string, defaultName string, homeDir string, envPrefix string)
- type APIServerBizHooks
- type APIServerConfig
- type CertKey
- type CompletedAPIServerConfig
- type CompletedGRPCServerConfig
- type GRPCServerBizHooks
- type GRPCServerConfig
- type GRPCServingInfo
- type GenericAPIServer
- type GenericGRPCServer
- type InsecureServingInfo
- type PreparedGenericAPIServer
- type PreparedGenericGRPCServer
- type RAInfo
- type SecureServingInfo
Constants ¶
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" )
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 ¶
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 ¶
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 ¶
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 ¶
PreparedGenericAPIServer is the interface for a prepared server ready to run.
type PreparedGenericGRPCServer ¶
PreparedGenericGRPCServer is the interface for a prepared server ready to run.
type SecureServingInfo ¶
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.