Documentation
¶
Index ¶
- Constants
- func DefaultConfigParser[T any](ver string, parser func(string) (*T, error)) *T
- func InitTimezone(zone string) error
- func InitVersionPrinter(printer func(string))
- func Run[T any](conf *T, builder func(*T) (*Application, error), options ...Option) error
- type Application
- type Hook
- type Option
Constants ¶
const DefaultTimezone = "Asia/Shanghai"
DefaultTimezone is the default timezone used for application initialization.
Variables ¶
This section is empty.
Functions ¶
func DefaultConfigParser ¶
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 ¶
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.
type Hook ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
WithShutdownSignals configures which OS signals will trigger application shutdown. Replaces the default signals (SIGTERM, SIGQUIT, SIGINT) with the provided ones.
func WithShutdownTimeout ¶
WithShutdownTimeout configures the maximum duration to wait for graceful shutdown. If the timeout is exceeded, the application will be forcefully terminated.