Documentation
¶
Overview ¶
Package service provides a framework for creating and managing service processes with support for configuration loading, lifecycle management, and component handling. It offers flexible configuration options, including file-based, HTTP-based, and stdin-based configuration loading, as well as template processing capabilities.
Index ¶
- func Run(opts ...RunOption)
- func RunService(s Service) error
- type BaseService
- func (s *BaseService[T]) Config() *Config[T]
- func (s *BaseService[T]) GetComponent(uuid string) component.Component
- func (s *BaseService[T]) Init(ctx context.Context) error
- func (s *BaseService[T]) Logger() *slog.Logger
- func (s *BaseService[T]) SetVersionFunc(f func())
- func (s *BaseService[T]) Shutdown(ctx context.Context) error
- func (s *BaseService[T]) Start(ctx context.Context) error
- func (s *BaseService[T]) Uninit(ctx context.Context) error
- type Config
- type RunOption
- type Service
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Run ¶
func Run(opts ...RunOption)
Run is a convenience function for running a service with a default configuration. It creates and runs a BaseService with an empty context. This function always exits the program: - It exits with the error code if an error occurs. - It exits with code 0 if the service runs successfully. It is recommended to use this function unless you need to customize the Service or want to prevent the program from exiting.
func RunService ¶
RunService starts and manages the lifecycle of the given service. It handles initialization, starting, shutdown, and uninitialization of the service. This function returns any error encountered during the service lifecycle. Use this function if you need to run a custom Service implementation or if you want to handle errors without exiting the program.
Types ¶
type BaseService ¶
type BaseService[T any] struct { // contains filtered or unexported fields }
BaseService implements the Service interface with a generic context type T.
func NewBaseService ¶
func NewBaseService[T any](config Config[T]) *BaseService[T]
NewBaseService creates a new BaseService with the given configuration.
func (*BaseService[T]) Config ¶
func (s *BaseService[T]) Config() *Config[T]
Config returns the current configuration.
func (*BaseService[T]) GetComponent ¶
func (s *BaseService[T]) GetComponent(uuid string) component.Component
GetComponent returns a component by its UUID.
func (*BaseService[T]) Init ¶
func (s *BaseService[T]) Init(ctx context.Context) error
Init implements the Service Init method, setting up logging and initializing components.
func (*BaseService[T]) Logger ¶
func (s *BaseService[T]) Logger() *slog.Logger
Logger returns the logger instance for the service.
func (*BaseService[T]) SetVersionFunc ¶
func (s *BaseService[T]) SetVersionFunc(f func())
SetVersionFunc sets the version function to be called when the service is started with the -v flag. If set to nil, the version function is disabled.
func (*BaseService[T]) Shutdown ¶
func (s *BaseService[T]) Shutdown(ctx context.Context) error
Shutdown implements the Service Shutdown method, shutting down all components.
type Config ¶
type Config[T any] struct { Context T `json:",omitempty"` Components []component.Config `json:",omitempty"` }
Config represents a generic configuration structure for services. It includes a context of type T and a list of component configurations.
type RunOption ¶
type RunOption func(*runOptions)
RunOption is a functional option for configuring the Run function.
func WithDecoder ¶
WithDecoder sets the decoder function for the Run function.
func WithEncoder ¶
WithEncoder sets the encoder function for the Run function.