boot

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package boot provides application bootstrapping: config loading, logger, database connections, Redis, and graceful lifecycle management.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InitLogger

func InitLogger(mode string)

InitLogger sets up a zerolog logger. In debug mode it uses colored console output; otherwise JSON.

func LoadConfig

func LoadConfig(path string, cfg any) error

LoadConfig reads a YAML config file into cfg. Environment variables DATABASE_DSN and REDIS_ADDR override file values.

func Log

func Log() *zerolog.Logger

Log returns the global logger instance.

func LogWriter

func LogWriter() io.Writer

LogWriter returns the logger as an io.Writer (useful for Gin).

func OpenAllDBs

func OpenAllDBs(cfgs map[string]DatabaseConfig, mode string) (map[string]*gorm.DB, error)

OpenAllDBs opens all databases defined in config.

func OpenDB

func OpenDB(cfg DatabaseConfig, mode string) (*gorm.DB, error)

OpenDB creates a GORM database connection with production-ready pool settings.

func OpenRedis

func OpenRedis(cfg RedisConfig) (*redis.Client, error)

OpenRedis creates a Redis client. Returns nil if Addr is empty.

Types

type App

type App struct {
	Config   *Config
	Gin      *gin.Engine
	DBs      map[string]*gorm.DB
	Redis    *redis.Client
	GRPC     *grpc.Server      // nil if gRPC is disabled
	Registry registry.Registry // nil if registry is disabled
	// contains filtered or unexported fields
}

App is the central service instance holding all shared resources.

func New

func New(cfgPath string) (*App, error)

New creates an App from a config file path. It initializes the logger, Gin engine, database connections, and Redis (if configured).

func (*App) DB

func (a *App) DB(name string) *gorm.DB

DB returns a named database connection.

func (*App) OnClose

func (a *App) OnClose(fn func())

OnClose registers a callback invoked during graceful shutdown.

func (*App) OnStart

func (a *App) OnStart(fn func())

OnStart registers a callback invoked after the HTTP server starts listening.

func (*App) Run

func (a *App) Run(setup SetupFunc) error

Run starts the HTTP server (and optional gRPC server), calls setup, then blocks until SIGINT/SIGTERM. Shutdown is graceful.

type Config

type Config struct {
	Server   ServerConfig              `mapstructure:"server"`
	Database map[string]DatabaseConfig `mapstructure:"database"`
	Redis    RedisConfig               `mapstructure:"redis"`
	GRPC     GRPCConfig                `mapstructure:"grpc"`
	Registry RegistryConfig            `mapstructure:"registry"`
}

Config is the base service configuration. Embed it in your own config struct to add service-specific fields.

func LoadBaseConfig

func LoadBaseConfig(path string) (*Config, error)

LoadBaseConfig is a convenience wrapper that loads into a *Config.

type DatabaseConfig

type DatabaseConfig struct {
	DSN           string `mapstructure:"dsn"`
	MaxIdle       int    `mapstructure:"maxIdle"`       // max idle connections (default 10)
	MaxOpen       int    `mapstructure:"maxOpen"`       // max open connections (default 50)
	MaxLifetime   int    `mapstructure:"maxLifetime"`   // max connection lifetime in seconds (default 3600)
	MaxIdleTime   int    `mapstructure:"maxIdleTime"`   // max idle time in seconds (default 300)
	SlowThreshold int    `mapstructure:"slowThreshold"` // slow query threshold in ms (default 200)
	PingOnOpen    bool   `mapstructure:"pingOnOpen"`    // ping DB on open to verify connectivity (default true)
}

DatabaseConfig describes a single database connection.

type GRPCConfig

type GRPCConfig struct {
	Enable bool   `mapstructure:"enable"`
	Addr   string `mapstructure:"addr"` // e.g. ":7889"
}

GRPCConfig describes an optional gRPC listener.

type RedisConfig

type RedisConfig struct {
	Addr     string `mapstructure:"addr"`
	Password string `mapstructure:"password"`
	DB       int    `mapstructure:"db"`
}

RedisConfig describes a Redis connection. Leave Addr empty to disable.

type RegistryConfig

type RegistryConfig struct {
	Enable    bool     `mapstructure:"enable"`
	Endpoints []string `mapstructure:"endpoints"` // e.g. ["127.0.0.1:2379"]
	Prefix    string   `mapstructure:"prefix"`    // default "/mofang/services/"
	TTL       int      `mapstructure:"ttl"`       // lease TTL in seconds, default 30
}

RegistryConfig describes the service registry (etcd).

type ServerConfig

type ServerConfig struct {
	Name string `mapstructure:"name"`
	Addr string `mapstructure:"addr"` // e.g. ":7801"
	Mode string `mapstructure:"mode"` // "debug" or "release"
}

ServerConfig describes the HTTP server.

type SetupFunc

type SetupFunc func(app *App) error

SetupFunc is called during Run to register routes, stores, gRPC services, etc.

Jump to

Keyboard shortcuts

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