sen

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: May 26, 2023 License: MIT Imports: 10 Imported by: 4

README

sen is a Go library that allows to build an application by combining different plugins. The richer the community is, the more powerful sen is.

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

View Source
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 New

func New() *Application

New creates a new Application.

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 Hook

type Hook func(ctx context.Context) error

Hook represents a hook to add custom logic in the application life cycle.

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 AfterRun added in v0.3.0

func AfterRun(hooks ...Hook) Plugin

AfterRun adds multiple hooks to run with the application.

func Component

func Component(name string, c any) Plugin

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 Module

func Module(plugins ...Plugin) Plugin

Module is a collection of plugins.

func OnRun added in v0.3.0

func OnRun(hooks ...Hook) Plugin

OnRun adds multiple hooks to run with the application.

func OnShutdown added in v0.3.0

func OnShutdown(hooks ...Hook) Plugin

OnShutdown adds multiple hooks to run with the application.

Directories

Path Synopsis
pkg
plugins/echo Module
plugins/zap Module
sen Module

Jump to

Keyboard shortcuts

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