config

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package config provides configuration management with hot-reload support.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load[T any](path string) T

Load loads configuration from the given path.

func LoadFrom

func LoadFrom[T any](src Source[T]) T

LoadFrom loads configuration from a custom source.

func NewCfg

func NewCfg[T any](path string) T

NewCfg loads configuration from file (alias for Load).

Types

type BaseConfig

type BaseConfig = Config

BaseConfig is an alias for Config for backward compatibility.

type CORSConfig

type CORSConfig struct {
	AllowOrigins     []string `mapstructure:"allow_origins"`
	AllowMethods     []string `mapstructure:"allow_methods"`
	AllowHeaders     []string `mapstructure:"allow_headers"`
	AllowCredentials bool     `mapstructure:"allow_credentials"`
}

CORSConfig contains CORS settings.

type CasbinConfig

type CasbinConfig struct {
	Model     string `mapstructure:"model"`
	ModelFile string `mapstructure:"model_file"`
}

CasbinConfig contains Casbin settings.

type Config

type Config struct {
	System  SystemConfig `mapstructure:"system"`
	Mysql   MysqlConfig  `mapstructure:"mysql"`
	Redis   RedisConfig  `mapstructure:"redis"`
	Mongodb MongoConfig  `mapstructure:"mongodb"`
	Kafka   KafkaConfig  `mapstructure:"kafka"`
	Log     LogConfig    `mapstructure:"log"`
	JWT     JWTConfig    `mapstructure:"jwt"`
	CORS    CORSConfig   `mapstructure:"cors"`
	Casbin  CasbinConfig `mapstructure:"casbin"`
}

Config is the main application configuration.

type FileSource

type FileSource[T any] struct {
	Path string
}

FileSource loads configuration from a local file.

func (FileSource[T]) Load

func (f FileSource[T]) Load(ctx context.Context) (T, error)

Load loads configuration from file.

func (FileSource[T]) Watch

func (f FileSource[T]) Watch(ctx context.Context, onChange func(T)) error

Watch watches for file changes and calls onChange.

type JWTConfig

type JWTConfig struct {
	Secret    string        `mapstructure:"secret"`
	Expire    time.Duration `mapstructure:"expire"`
	SkipPaths []string      `mapstructure:"skip_paths"`
}

JWTConfig contains JWT settings.

type KafkaConfig

type KafkaConfig struct {
	Brokers  []string `mapstructure:"brokers"`
	ClientID string   `mapstructure:"client_id"`
	Topic    string   `mapstructure:"topic"`
}

KafkaConfig contains Kafka connection settings.

type LogConfig

type LogConfig struct {
	Level  string        `mapstructure:"level"`
	Format string        `mapstructure:"format"` // json, text, color
	Output string        `mapstructure:"output"` // stdout, stderr
	File   LogFileConfig `mapstructure:"file"`
}

LogConfig contains logging settings.

type LogFileConfig

type LogFileConfig struct {
	Filename   string `mapstructure:"filename"`
	MaxSize    int    `mapstructure:"max_size"`
	MaxBackups int    `mapstructure:"max_backups"`
	MaxAge     int    `mapstructure:"max_age"`
	Compress   bool   `mapstructure:"compress"`
	Format     string `mapstructure:"format"`
}

LogFileConfig contains file logging settings.

type MongoConfig

type MongoConfig struct {
	Host     string `mapstructure:"host"`
	Port     int    `mapstructure:"port"`
	User     string `mapstructure:"user"`
	Password string `mapstructure:"password"`
	Database string `mapstructure:"database"`
}

MongoConfig contains MongoDB connection settings.

type MysqlConfig

type MysqlConfig struct {
	Host         string `mapstructure:"host"`
	Port         int    `mapstructure:"port"`
	User         string `mapstructure:"user"`
	Password     string `mapstructure:"password"`
	Dbname       string `mapstructure:"database"`
	Database     string `mapstructure:"database"` // Alias for Dbname
	DockerHost   string `mapstructure:"docker_host"`
	MaxIdleConns int    `mapstructure:"max_idle_conns"`
	MaxOpenConns int    `mapstructure:"max_open_conns"`
	MaxLifetime  int    `mapstructure:"max_lifetime"`
}

MysqlConfig contains MySQL connection settings.

type RedisConfig

type RedisConfig struct {
	Host     string `mapstructure:"host"`
	Port     int    `mapstructure:"port"`
	Password string `mapstructure:"password"`
	DB       int    `mapstructure:"database"`
	Database int    `mapstructure:"database"` // Alias for DB
	PoolSize int    `mapstructure:"pool_size"`
}

RedisConfig contains Redis connection settings.

type Source

type Source[T any] interface {
	Load(ctx context.Context) (T, error)
}

Source abstracts configuration loading from various sources.

type Store

type Store[T any] struct {
	// contains filtered or unexported fields
}

Store provides atomic read/update for configuration with hot-reload.

func Bootstrap

func Bootstrap[T any](path string) (T, *Store[T])

Bootstrap loads configuration from file and sets up hot-reload.

func NewStore

func NewStore[T any](initial T) *Store[T]

NewStore creates a new configuration store.

func (*Store[T]) Current

func (s *Store[T]) Current() T

Current returns the current configuration.

func (*Store[T]) Subscribe

func (s *Store[T]) Subscribe(key string) <-chan T

Subscribe subscribes to configuration updates.

func (*Store[T]) Update

func (s *Store[T]) Update(c T)

Update updates the configuration and notifies subscribers.

type SystemConfig

type SystemConfig struct {
	Port    string `mapstructure:"port"`
	Env     string `mapstructure:"env"` // dev, test, prod
	Name    string `mapstructure:"name"`
	Version string `mapstructure:"version"`
	Level   string `mapstructure:"level"`
}

SystemConfig contains system-level settings.

type WatchableSource

type WatchableSource[T any] interface {
	Source[T]
	Watch(ctx context.Context, onChange func(T)) error
}

WatchableSource supports hot-reload configuration.

Jump to

Keyboard shortcuts

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