boot

package
v0.0.2-beta.2 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultTimezone = "Asia/Shanghai"

DefaultTimezone is the default timezone used for application initialization.

Variables

This section is empty.

Functions

func DefaultConfigParser

func DefaultConfigParser[T any](ver string, parser func(string) (*T, error)) *T

DefaultConfigParser provides a standard command-line configuration parser with version and help flags. It handles common CLI flags (config, version, help) and parses configuration from the specified file. The parser function should read and parse the configuration file into type T.

func InitTimezone

func InitTimezone(zone string) error

InitTimezone sets the application timezone to the specified zone. It loads the timezone location and sets both time.Local and TZ environment variable.

func InitVersionPrinter

func InitVersionPrinter(printer func(string))

InitVersionPrinter sets a custom version printer function to replace the default. This allows applications to customize how version information is displayed.

func Run

func Run[T any](conf *T, builder func(*T) (*Application, error), options ...Option) error

Run executes an application built from the provided configuration using the builder function. It handles the complete application lifecycle including startup, signal handling, and graceful shutdown. The builder function receives the configuration and should return a properly initialized Application. Returns an error if the application fails to build, start, or encounters issues during execution.

Types

type Application

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

Application represents the main application container that manages a group of tasks. It implements the Task interface, allowing it to be composed with other components.

func NewApplication

func NewApplication(tasks ...task.Task) *Application

NewApplication creates a new Application instance with the given tasks. All provided tasks will be managed as a group with coordinated lifecycle management.

func (*Application) Identifier

func (a *Application) Identifier() string

Identifier returns the application's identifier for logging and debugging.

func (*Application) Start

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

Start begins all managed tasks in the application. It monitors the context for cancellation and returns nil if cancelled gracefully. Returns an error if any task fails to start.

func (*Application) Stop

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

Stop gracefully shuts down all managed tasks in the application. Returns an error if any task fails to stop cleanly.

type Hook

type Hook = func(context.Context) error

Hook defines a function that can be executed at various lifecycle stages of the application. It receives a context and returns an error if the hook execution fails.

type Option

type Option func(*options)

Option defines a configuration function that modifies application runtime options.

func AddAfterStop

func AddAfterStop(f Hook) Option

AddAfterStop adds a hook that will be executed after the application has stopped. These hooks run after all tasks have been stopped and are useful for cleanup operations.

func AddBeforeStart

func AddBeforeStart(f Hook) Option

AddBeforeStart adds a hook that will be executed before the application starts. These hooks run sequentially and any failure will prevent the application from starting.

func AddBeforeStop

func AddBeforeStop(f Hook) Option

AddBeforeStop adds a hook that will be executed before the application begins shutdown. These hooks run after a shutdown signal is received but before stopping tasks.

func WithLoggerInit

func WithLoggerInit(ver string, conf *log.Config) Option

WithLoggerInit configures automatic logger initialization with the provided version and configuration. It adds hooks to initialize the logger before start and sync it after stop.

func WithShutdownSignals

func WithShutdownSignals(sigs ...os.Signal) Option

WithShutdownSignals configures which OS signals will trigger application shutdown. Replaces the default signals (SIGTERM, SIGQUIT, SIGINT) with the provided ones.

func WithShutdownTimeout

func WithShutdownTimeout(d time.Duration) Option

WithShutdownTimeout configures the maximum duration to wait for graceful shutdown. If the timeout is exceeded, the application will be forcefully terminated.

Jump to

Keyboard shortcuts

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