container

package
v0.2.15 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultComponentPriority = 1000

DefaultComponentPriority defines the default priority for components. Components with lower priority values are initialized first. It is recommended to use values in increments of 100 to leave space for future adjustments.

Variables

This section is empty.

Functions

func WithAppInfo

func WithAppInfo(info interfaces.AppInfo) options.Option

WithAppInfo sets the application's metadata for the container.

func WithComponentFactory

func WithComponentFactory(name string, factory ComponentFactory) options.Option

WithComponentFactory registers a component factory with the container.

func WithContainer

func WithContainer(container Container) options.Option

func WithDefaultCacheName

func WithDefaultCacheName(name string) options.Option

WithDefaultCacheName sets the global default cache name.

func WithDefaultDatabaseName

func WithDefaultDatabaseName(name string) options.Option

WithDefaultDatabaseName sets the global default database name.

func WithDefaultObjectStoreName

func WithDefaultObjectStoreName(name string) options.Option

WithDefaultObjectStoreName sets the global default object store name.

func WithDefaultRegistrarName

func WithDefaultRegistrarName(name string) options.Option

WithDefaultRegistrarName sets the global default registrar name.

Types

type CacheProvider

type CacheProvider interface {
	Caches() (map[string]storage.Cache, error)
	Cache(name string) (storage.Cache, error)
	DefaultCache(globalDefaultName string) (storage.Cache, error)
	RegisterCache(name string, cache storage.Cache)
}

CacheProvider provides access to Cache components.

type ClientMiddlewareProvider

type ClientMiddlewareProvider interface {
	Names() []string
	ClientMiddlewares() (map[string]middleware.KMiddleware, error)
	ClientMiddleware(name string) (middleware.KMiddleware, error)
	RegisterClientMiddleware(name string, mw middleware.KMiddleware)
}

ClientMiddlewareProvider provides access to client-side middleware components.

type ComponentFactory

type ComponentFactory interface {
	// Priority determines the initialization order of the component.
	// Components with lower priority values are created and initialized first.
	Priority() int
	// NewComponent creates a new component instance.
	// It receives a component-specific configuration and the container instance,
	// allowing it to register other components or access other services.
	NewComponent(cfg interfaces.StructuredConfig, container Container, opts ...options.Option) (interfaces.Component, error)
}

ComponentFactory defines the interface for creating generic components. It includes a priority system to manage initialization order.

type ComponentFunc

type ComponentFunc func(cfg interfaces.StructuredConfig, container Container, opts ...options.Option) (interfaces.Component, error)

ComponentFunc is an adapter to allow the use of ordinary functions as ComponentFactory.

func (ComponentFunc) NewComponent

func (c ComponentFunc) NewComponent(cfg interfaces.StructuredConfig, container Container, opts ...options.Option) (interfaces.Component, error)

NewComponent calls the wrapped function.

func (ComponentFunc) Priority

func (c ComponentFunc) Priority() int

Priority returns the default priority for a ComponentFunc. This ensures that function-based components have a predictable, lower priority.

type Container

type Container interface {
	// Registry returns the service registry provider.
	Registry(opts ...options.Option) (RegistryProvider, error)
	// Middleware returns the middleware provider.
	Middleware(opts ...options.Option) (MiddlewareProvider, error)
	// Cache returns the cache provider.
	Cache(opts ...options.Option) (CacheProvider, error)
	// Database returns the database provider.
	Database(opts ...options.Option) (DatabaseProvider, error)
	// ObjectStore returns the object storage provider.
	ObjectStore(opts ...options.Option) (ObjectStoreProvider, error)

	// Component retrieves a generic component by its name, using a lazy-initialization strategy.
	// If the component is not yet created, the container will use its registered factory to create it.
	// The provided options are only used during the creation of the component.
	// This method is concurrency-safe.
	Component(name string, opts ...options.Option) (interfaces.Component, error)

	// RegisterComponent registers a pre-built component instance.
	RegisterComponent(name string, comp interfaces.Component)

	// RegisterFactory registers a factory for creating a component on demand.
	RegisterFactory(name string, factory ComponentFactory)

	// HasComponent checks if a component instance or factory has been registered.
	HasComponent(name string) bool

	// RegisteredComponents returns a slice of names for all registered components and factories.
	RegisteredComponents() []string

	// Logger returns the container's configured logger.
	Logger() runtimelog.Logger
	// AppInfo returns the application's metadata.
	AppInfo() interfaces.AppInfo

	// DefaultCache returns the default cache instance.
	DefaultCache() (storageiface.Cache, error)
	// DefaultDatabase returns the default database instance.
	DefaultDatabase() (storageiface.Database, error)
	// DefaultObjectStore returns the default object storage instance.
	DefaultObjectStore() (storageiface.ObjectStore, error)
	// DefaultRegistrar returns the default service registrar instance.
	DefaultRegistrar() (runtimeregistry.KRegistrar, error)
}

Container defines the interface for accessing various runtime components. It supports lazy initialization of components via factories and dependency injection.

func FromOptions

func FromOptions(opts []options.Option) Container

func New

func New(config interfaces.StructuredConfig, opts ...options.Option) Container

New creates a new, concurrency-safe Container instance. It receives the final AppInfo from the bootstrap process.

type DatabaseProvider

type DatabaseProvider interface {
	Databases() (map[string]storage.Database, error)
	Database(name string) (storage.Database, error)
	DefaultDatabase(globalDefaultName string) (storage.Database, error)
	RegisterDatabase(name string, db storage.Database)
}

DatabaseProvider provides access to Database components.

type MiddlewareProvider

type MiddlewareProvider interface {
	Names() []string
	ServerMiddlewareProvider
	ClientMiddlewareProvider
}

MiddlewareProvider provides access to both server and client middleware components.

type ObjectStoreProvider

type ObjectStoreProvider interface {
	ObjectStores() (map[string]storage.ObjectStore, error)
	ObjectStore(name string) (storage.ObjectStore, error)
	DefaultObjectStore(globalDefaultName string) (storage.ObjectStore, error)
	RegisterObjectStore(name string, store storage.ObjectStore)
}

ObjectStoreProvider provides access to ObjectStore components.

type RegistryProvider

type RegistryProvider interface {
	Discoveries() (map[string]registry.KDiscovery, error)
	Discovery(name string) (registry.KDiscovery, error)
	Registrars() (map[string]registry.KRegistrar, error)
	Registrar(name string) (registry.KRegistrar, error)
	DefaultRegistrar(globalDefaultName string) (registry.KRegistrar, error)
	RegisterDiscovery(name string, discovery registry.KDiscovery)
	RegisterRegistrar(name string, registrar registry.KRegistrar)
}

RegistryProvider provides access to Discovery and Registrar components. The provider is responsible for creating and managing the lifecycle of these components.

type ServerMiddlewareProvider

type ServerMiddlewareProvider interface {
	Names() []string
	ServerMiddlewares() (map[string]middleware.KMiddleware, error)
	ServerMiddleware(name string) (middleware.KMiddleware, error)
	RegisterServerMiddleware(name string, mw middleware.KMiddleware)
}

ServerMiddlewareProvider provides access to server-side middleware components.

Directories

Path Synopsis
internal
util
Package util implements the functions, types, and interfaces for the module.
Package util implements the functions, types, and interfaces for the module.

Jump to

Keyboard shortcuts

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