Documentation
¶
Index ¶
Constants ¶
const ( StartTimeout = 30 * time.Second StopTimeout = 30 * time.Second )
Variables ¶
This section is empty.
Functions ¶
func Inject ¶ added in v1.1.0
func Inject(dstPtr interface{}, mfuncs ...ModuleFunc) error
Inject creates a context and injects dependencies into public struct fields.
func MustInject ¶ added in v1.1.0
func MustInject(dstPtr interface{}, mfuncs ...ModuleFunc)
MustInject creates a context and injects dependencies into public struct fields, or panics on an error.
Types ¶
type App ¶ added in v1.1.0
type App struct {
Context *Context
Logger Logger
StartTimeout time.Duration
StopTimeout time.Duration
}
App provides a start/stop lifecycle and a graceful shutdown. Usually, users should call app.Run() which starts the services in toplogical order from dependencies to dependants. Then blocks until a SIGINT/SIGKILL signal arrives, and stops the services in reverse order.
func NewApp ¶ added in v1.1.0
func NewApp(modules ...ModuleFunc) (*App, error)
NewApp creates a new application from modules.
func (*App) Run ¶ added in v1.1.0
Run starts the application, awaits a stop signal and then stops the application.
type Context ¶ added in v1.1.0
type Context struct {
Modules map[string]*Module
Providers map[reflect.Type]*Provider
Instances map[reflect.Type]interface{}
InstanceSlice []interface{} // Ordered from dependencies to dependants.
}
Context is a dependency injection context.
func NewContext ¶ added in v1.1.0
func NewContext(mfuncs ...ModuleFunc) (*Context, error)
NewContext creates a context and initializes all instances from its providers.
type Logger ¶ added in v1.1.0
type Logger interface {
Println(v ...interface{})
}
Logger is an application logger.
type Module ¶
type Module struct {
Name string
Imports []ModuleFunc
Providers []*Provider
Deps []reflect.Type
}
Module groups providers, dependencies and imports.
func (*Module) AddInstance ¶
func (m *Module) AddInstance(instance interface{})
AddInstance adds a new instance provider.
func (*Module) Dep ¶ added in v1.1.0
func (m *Module) Dep(dep interface{})
Dep adds a dependency which will be provided at a context level, not via imported modules.
func (*Module) Import ¶
func (m *Module) Import(module ModuleFunc)
Import adds another module to this module dependencies.
type ModuleFunc ¶
type ModuleFunc func(*Module)
ModuleFunc defines a module provider.
func (ModuleFunc) Name ¶
func (m ModuleFunc) Name() string
type Provider ¶ added in v1.1.0
type Provider struct {
Module *Module
Name string
Type reflect.Type
Deps []reflect.Type
Func func(args []interface{}) (interface{}, error)
}
Provider creates a service instance.