baseapp

package
v6.0.2 Latest Latest
Warning

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

Go to latest
Published: May 29, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package baseapp implements a lightweight abstract base framework for a microservice application.

Index

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

func New(option ...Option) *BaseApp

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 NewWithConfig(config *Config) *BaseApp

func (*BaseApp) GetConfig

func (b *BaseApp) GetConfig() Config

GetConfig returns the server configuration associated with the BaseApp.

This function provides access to the configuration used by the BaseApp.

func (*BaseApp) GetLogger

func (b *BaseApp) GetLogger() log.Log

GetLogger returns the logger associated with the BaseApp.

This function provides access to the logging instance used by the BaseApp.

func (*BaseApp) GetNotifier

func (b *BaseApp) GetNotifier() notifier.Notifier

GetNotifier returns the notifier associated with the BaseApp.

This function provides access to the notifier instance used by the BaseApp.

func (*BaseApp) PanicRecovery

func (b *BaseApp) PanicRecovery(ctx context.Context, rec any) (string, error)

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

func (b *BaseApp) ProcessError(ctx context.Context, stackTrace string, err error) (int, []byte)

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

func (b *BaseApp) RegisterHooks(hook any)

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

func (b *BaseApp) RunHealthCheck(ctx context.Context) error

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

func (b *BaseApp) RunStatusCheck(ctx context.Context) map[string]any

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

func (b *BaseApp) SetNotifier(notifier notifier.Notifier)

SetNotifier sets the notifier for the BaseApp.

This function updates the notifier instance used by the BaseApp.

func (*BaseApp) Shutdown

func (b *BaseApp) Shutdown(ctx context.Context)

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

func (b *BaseApp) StartSignalMonitor(ctx context.Context) error

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

type Config struct {
	ServiceName string
	Log         log.Log
	Notifier    notifier.Notifier
}

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

type HealthCheckHook interface {
	Name
	HealthCheck(ctx context.Context) error
}

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

type Name interface {
	Name(ctx context.Context) string
}

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 WithLog

func WithLog(log log.Log) Option

WithLog sets the Log field of Config.

func WithNotifier

func WithNotifier(notifier notifier.Notifier) Option

WithNotifier sets the Notifier field of Config.

func WithServiceName

func WithServiceName(serviceName string) Option

WithServiceName sets the ServiceName field of Config.

type ShutdownHook

type ShutdownHook interface {
	Name
	Shutdown(ctx context.Context) error
}

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

type StatusCheckHook interface {
	Name
	StatusCheck(ctx context.Context) (any, error)
}

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

Jump to

Keyboard shortcuts

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