Documentation ¶
Overview ¶
Package baseapp implements a lightweight abstract base framework for a microservice application.
Index ¶
- type BaseApp
- func (b *BaseApp) GetConfig() Config
- func (b *BaseApp) GetLogger() log.Log
- func (b *BaseApp) GetNotifier() notifier.Notifier
- func (b *BaseApp) PanicRecovery(ctx context.Context, rec any) (string, error)
- func (b *BaseApp) ProcessError(ctx context.Context, stackTrace string, err error) (int, []byte)
- func (b *BaseApp) RegisterHealthCheckHook(handler HealthCheckHook)
- func (b *BaseApp) RegisterHooks(hook any)
- func (b *BaseApp) RegisterOnShutdownHook(handler ShutdownHook)
- func (b *BaseApp) RegisterStatusCheckHook(handler StatusCheckHook)
- func (b *BaseApp) RunHealthCheck(ctx context.Context) error
- func (b *BaseApp) RunStatusCheck(ctx context.Context) map[string]any
- func (b *BaseApp) SetNotifier(notifier notifier.Notifier)
- func (b *BaseApp) Shutdown(ctx context.Context)
- func (b *BaseApp) StartSignalMonitor(ctx context.Context) error
- func (b *BaseApp) WaitForCompleteShutDown()
- type Config
- type HealthCheckHook
- type Name
- type Option
- type ShutdownHook
- type StatusCheckHook
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BaseApp ¶
type BaseApp struct {
// contains filtered or unexported fields
}
BaseApp represents a basic application structure with configuration, logging, status check, health check, and shutdown functionality.
func New ¶
New creates a new instance of BaseApp with the provided configuration.
This function initializes a BaseApp instance with default or provided configurations and sets up the logging context. It also adds an initial wait group for the shutdown process.
func NewWithConfig ¶
func (*BaseApp) GetConfig ¶
GetConfig returns the server configuration associated with the BaseApp.
This function provides access to the configuration used by the BaseApp.
func (*BaseApp) GetLogger ¶
GetLogger returns the logger associated with the BaseApp.
This function provides access to the logging instance used by the BaseApp.
func (*BaseApp) GetNotifier ¶
GetNotifier returns the notifier associated with the BaseApp.
This function provides access to the notifier instance used by the BaseApp.
func (*BaseApp) PanicRecovery ¶
PanicRecovery recovers from panics, logs the panic details, and returns the stack trace and error.
This function should be used within a defer statement to capture any panics that occur during execution. It logs the panic details, captures the stack trace, and converts the panic to an error if it is not already one.
func (*BaseApp) ProcessError ¶
ProcessError processes an error, logs it, and returns the HTTP status code and error response body.
This function determines the type of the provided error, logs the appropriate details, and constructs an HTTP response. It handles custom errors, HTTP errors, and general errors, providing the necessary error response and logging stack traces if available. Notifications are sent if configured, based on the error type and status code.
func (*BaseApp) RegisterHealthCheckHook ¶
func (b *BaseApp) RegisterHealthCheckHook(handler HealthCheckHook)
RegisterHealthCheckHook registers a health check hook to be executed during the health check.
This function appends the provided health check handler to the list of health check hooks in the BaseApp.
func (*BaseApp) RegisterHooks ¶
RegisterHooks registers the provided hooks to the BaseApp.
This function checks the type of the provided hook and registers it as a HealthCheckHook, ShutdownHook, or StatusCheckHook if it implements the respective interface.
func (*BaseApp) RegisterOnShutdownHook ¶
func (b *BaseApp) RegisterOnShutdownHook(handler ShutdownHook)
RegisterOnShutdownHook registers a shutdown hook to be executed during server shutdown.
This function appends the provided shutdown handler to the list of shutdown hooks in the BaseApp.
func (*BaseApp) RegisterStatusCheckHook ¶
func (b *BaseApp) RegisterStatusCheckHook(handler StatusCheckHook)
RegisterStatusCheckHook registers a status check hook to be executed during the status check.
This function appends the provided status check handler to the list of status check hooks in the BaseApp.
func (*BaseApp) RunHealthCheck ¶
RunHealthCheck runs the registered health check hooks and returns an error if any health check fails.
This function iterates through all registered health check hooks, executing each one within a specified timeout context. If any health check fails, it logs the failure and returns an error indicating which hook failed.
func (*BaseApp) RunStatusCheck ¶
RunStatusCheck runs the registered status check hooks and returns a map of their statuses.
This function iterates through all registered status check hooks, executing each one within a specified timeout context. It collects the statuses of all hooks in a map, logging each status check's outcome and including it in the result map.
func (*BaseApp) SetNotifier ¶
SetNotifier sets the notifier for the BaseApp.
This function updates the notifier instance used by the BaseApp.
func (*BaseApp) Shutdown ¶
Shutdown gracefully shuts down the server by executing registered shutdown hooks.
This function iterates through all registered shutdown hooks, executing each one within a specified timeout context. It logs the progress of the shutdown process and ensures all hooks are processed before completing the shutdown.
func (*BaseApp) StartSignalMonitor ¶
StartSignalMonitor starts monitoring for OS signals such as SIGTERM and SIGINT and initiates shutdown on receiving them.
This function sets up a channel to receive OS signals and starts a goroutine to monitor those signals. When a signal is received, it triggers the server shutdown process.
func (*BaseApp) WaitForCompleteShutDown ¶
func (b *BaseApp) WaitForCompleteShutDown()
WaitForCompleteShutDown waits until the BaseApp completes shutdown.
This function blocks until the shutdown process, including all registered shutdown hooks, is complete.
type Config ¶
Config holds the configuration for the base app.
func GetDefaultConfig ¶
func GetDefaultConfig() *Config
GetDefaultConfig creates a new default Config with values from environment variables or default values.
Environment Variables - SERVICE_NAME: Sets [ServiceName] - KAFKACS__HEALTH_CHECK_RESULT_PATH: Sets [HealthCheckResultPath]
type HealthCheckHook ¶
HealthCheckHook defines an interface for health checks of different resources used by the app.
This interface extends the Name interface and requires the following method:
- HealthCheck(ctx context.Context) error
type Name ¶
Name defines an interface to retrieve the module's identity.
Implementing this interface requires the following method:
- Name(ctx context.Context) string
type Option ¶
type Option func(*Config)
Option represents a function that applies a configuration option to Config.
func WithNotifier ¶
WithNotifier sets the Notifier field of Config.
func WithServiceName ¶
WithServiceName sets the ServiceName field of Config.
type ShutdownHook ¶
ShutdownHook defines an interface for the graceful shutdown of different resources used by the app.
This interface extends the Name interface and requires the following method:
- Shutdown(ctx context.Context) error
type StatusCheckHook ¶
StatusCheckHook defines an interface to get the current status of different resources used by the app.
This interface extends the Name interface and requires the following method:
- StatusCheck(ctx context.Context) (any, error)
Directories ¶
Path | Synopsis |
---|---|
server
|
|
httpserver
Package httpserver provides configuration options for an HTTP server.
|
Package httpserver provides configuration options for an HTTP server. |
kafkaclient
Package kafkaconsumer extends the BaseApp with a kafka consumer server
|
Package kafkaconsumer extends the BaseApp with a kafka consumer server |