Documentation ¶
Overview ¶
Example ¶
package main import ( "context" "fmt" "github.com/bongnv/sen" ) func main() { app := sen.New() runHook := sen.OnRun(func(_ context.Context) error { fmt.Println("OnRun is executed") return nil }) shutdownHook := sen.OnShutdown(func(_ context.Context) error { fmt.Println("OnShutdown is executed") return nil }) afterRunHook := sen.AfterRun(func(_ context.Context) error { fmt.Println("AfterRun is executed") return nil }) _ = app.With(runHook, shutdownHook, afterRunHook) err := app.Run(context.Background()) if err != nil { fmt.Println(err) } }
Output: OnRun is executed OnShutdown is executed AfterRun is executed
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrComponentNotRegistered = errors.New("sen: the component is not registered")
ErrComponentNotRegistered is returned when the expected component isn't registered so it couldn't be found by name.
Functions ¶
This section is empty.
Types ¶
type Application ¶
type Application struct {
// contains filtered or unexported fields
}
Application represents an application. To construct an application from plugins use, Apply. For example:
app := sen.New() if err := app.Apply(plugin1, plugin2); err != nil { handleError(err) }
func (*Application) Run ¶
func (app *Application) Run(ctx context.Context) error
Run runs the application. It will execute all run hooks, then shutdown hooks and afterRun hooks.
func (*Application) Shutdown ¶
func (app *Application) Shutdown(ctx context.Context) error
Shutdown runs the application by executing all the registered hooks for this phase.
func (*Application) With ¶ added in v0.3.0
func (app *Application) With(plugins ...Plugin) error
With applies a plugin or multiple plugins. While applying a plugin, the plugin will be injected with dependencies and Init method will be called.
type Injector ¶ added in v0.3.0
type Injector interface { Register(name string, component interface{}) error Retrieve(name string) (interface{}, error) Inject(component interface{}) error }
Injector is a hub of components. It allows injecting components via tags or types.
type Lifecycle ¶ added in v0.3.0
type Lifecycle interface { OnRun(h Hook) OnShutdown(h Hook) AfterRun(h Hook) Run(ctx context.Context) error Shutdown(ctx context.Context) error }
Lifecycle manages the lifecycle of an application. An application starts with .Run(ctx) and will be stopped when .Shutdown(ctx) is called. It also allows to hook into the application via OnRun, OnShutdown and AfterRun.
type Plugin ¶
type Plugin interface { // Initialize initialises the plugin and installs the plugin into the application. Initialize() error }
Plugin represents a plugin in a sen application. It enhances the application by providing one or multiple functionalities. A plugin can have from zero to many dependencies and they can be injected by declaring "inject" tag.
func Component ¶
Component creates a new component plugin. It is a simple plugin to add a component into the application. The component will be registered with the given name. Init method will be called to initialize the component after dependencies are injected.
func GracefulShutdown ¶
func GracefulShutdown() Plugin
GracefulShutdown creates a new GracefulShutdownPlugin. The plugin will allow the application calling its Shutdown when an interrupt signal (Ctrl+C) is received.
func OnShutdown ¶ added in v0.3.0
OnShutdown adds multiple hooks to run with the application.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
pkg
|
|
plugins/echo
Module
|
|
plugins/envconfig
Module
|
|
plugins/postgres-gorm
Module
|
|
plugins/zap
Module
|
|
sen
Module
|