application

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2023 License: MIT Imports: 22 Imported by: 8

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidObjectPath is returned when a key cannot be converted into
	// a nested object path like "foo...bar", ".foo", or "foo."
	ErrInvalidObjectPath = errors.New("invalid object path")
)

Functions

func Run

func Run(name, version string, opts ...Option) error

Run creates an application with the specified name and version, applies the provided options, and begins execution

Types

type Application

type Application struct {
	sync.Mutex

	Name       string
	Version    string
	Controller *Controller
	Settings   *config.Set
	Logger     *slog.Logger
	// contains filtered or unexported fields
}

Application defines an instance of an application

func FromContext

func FromContext(ctx context.Context) *Application

FromContext extracts the Appliation instance if it exists from the provided context or nil if not found

func New

func New(name, version string, opts ...Option) *Application

New creates a new application and executes the options

func (*Application) Exit

func (a *Application) Exit(err error)

Exit will shutdown the application with the specified error.

This call can be made from any go routine, only the first call to Exit will be read (first in) and shutdown the application

func (*Application) Install

func (a *Application) Install(ctx context.Context) error

Install will execute all modules that have an application.Initializer implementation, then all modules with that implement the application.Installer

func (*Application) Run

func (a *Application) Run(ctx context.Context) error

Run the application returning the error that terminated execution or nil if terminated normally

func (*Application) String

func (a *Application) String() string

String returns the application name/version

func (*Application) Validate

func (a *Application) Validate(ctx context.Context) error

Validate will validate the configuration and return any errors

type Configurable

type Configurable interface {
	// Config should return a pointer to an allocated configuration
	// structure. This structure will be written to directly with the
	// decoded configuration. If this returns nil, then it is as if
	// Configurable was not implemented.
	Config() (interface{}, error)
}

Configurable can be optionally implemented by any module to accept user configuration.

type ConfigurableNotify

type ConfigurableNotify interface {
	Configurable

	// ConfigSet is called with the value of the configuration after
	// decoding is complete successfully.
	ConfigSet(interface{}) error
}

ConfigurableNotify is an optional interface that can be implemented by any module to receive a notification that the configuration was decoded.

type Configuration

type Configuration struct{}

Configuration for the application based on github.com/hashicorp/hcl/v2

func (Configuration) Decode

func (c Configuration) Decode(ctx context.Context, filename string, src []byte) hcl.Diagnostics

func (Configuration) DecodeFile

func (c Configuration) DecodeFile(ctx context.Context, filename string) hcl.Diagnostics

DecodeFile will open and decode the provided file, returning an error when parsing fails

func (Configuration) EvalContext

func (Configuration) EvalContext(ctx context.Context) *hcl.EvalContext

EvalContext returns the hcl.EvalContext for loading hcl files

type Controller

type Controller struct {
	// contains filtered or unexported fields
}

Controller for modules

func (*Controller) Add

func (c *Controller) Add(name string, m Module)

Add the specified module, if a module with the same name exists, it will be overwritten

func (*Controller) Get

func (c *Controller) Get(name string) Module

Get the module with the specified name, will return nil if no module is found

func (*Controller) Range

func (c *Controller) Range(cb func(name string, module Module) bool)

Range over the modules

func (*Controller) Remove

func (c *Controller) Remove(name string)

Remove the specified module

func (*Controller) Run

func (c *Controller) Run(ctx context.Context) error

Run the added modules. This will run the lifetime on modules in the order they were added

Module lifetime is called in the following order: * if module is Initializer -> Initialize() * if module is PreStarter -> PreStart() * Start() * if module is PostStarter -> PostStart() * wait for context.Done() * Stop()

Stop() will be called on all module that Start() was successfully called on, even during error

type Error

type Error struct {
	Errors []error
}

Error for modules containing multiple errors

func (*Error) Append

func (e *Error) Append(err error) *Error

Append the error to the existing one, when the target is nil, it will create a new Error struct

func (Error) Err

func (e Error) Err() error

Err returns an error if any exist, or nil

func (Error) Error

func (e Error) Error() string

func (Error) Unwrap

func (e Error) Unwrap() error

Unwrap the error

type Initializer

type Initializer interface {
	Initialize(ctx context.Context) (context.Context, error)
}

Initializer allows for context modifications before start

type Installer

type Installer interface {
	Install(ctx context.Context) error
}

Installer allows for modules to install things before and modul.PreStart is called, and is used in the application.Install function

type Module

type Module interface {
	Start(ctx context.Context) error
	Stop(ctx context.Context) error
}

Module represents an interface into a start stop module

type Option

type Option func(app *Application)

Option for an Application

func WithConfigFile

func WithConfigFile(filename string) Option

WithConfigFile adds hcl parsing capability to the application and loads the provided filename

func WithLogger

func WithLogger(logger *slog.Logger) Option

WithLogger will set the internal slog.Logger instance

func WithModule

func WithModule(name string, m Module) Option

WithModule adds the specified module to the application for execution

type PostStarter

type PostStarter interface {
	PostStart(ctx context.Context) error
}

PostStarter allows for additional functionality after all Module.Start are called

type PreStarter

type PreStarter interface {
	PreStart(ctx context.Context) error
}

PreStarter allows for additional functionality before any Module.Start is called

Directories

Path Synopsis
Package confighcl allows decoding HCL configurations into config data structures.
Package confighcl allows decoding HCL configurations into config data structures.

Jump to

Keyboard shortcuts

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