config

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 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"`
	Enabled    bool     `mapstructure:"enabled"`
	SkipPaths  []string `mapstructure:"skip_paths"`
	AdminUsers []string `mapstructure:"admin_users"`
}

CasbinConfig contains Casbin settings.

type Config

type Config struct {
	System   SystemConfig   `mapstructure:"system"`
	Database DatabaseConfig `mapstructure:"database"`
	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"`
	Otel     OtelConfig     `mapstructure:"otel"`
}

Config is the main application configuration.

type DatabaseConfig added in v0.0.4

type DatabaseConfig struct {
	Driver   string `mapstructure:"driver"`   // mysql, postgres, sqlite
	Host     string `mapstructure:"host"`     // mysql/postgres: server host
	Port     int    `mapstructure:"port"`     // mysql/postgres: server port
	User     string `mapstructure:"user"`     // mysql/postgres: username
	Password string `mapstructure:"password"` // mysql/postgres: password
	Database string `mapstructure:"database"` // database name or sqlite file path

	// MySQL specific
	Charset string `mapstructure:"charset"` // mysql: character set (default: utf8mb4)

	// PostgreSQL specific
	SSLMode string `mapstructure:"ssl_mode"` // postgres: disable, require, verify-ca, verify-full
	Schema  string `mapstructure:"schema"`   // postgres: search_path schema

	// Common options
	TimeZone        string `mapstructure:"timezone"`           // timezone for parsing time (e.g., "Asia/Shanghai", "Local")
	MaxIdleConns    int    `mapstructure:"max_idle_conns"`     // max idle connections in pool
	MaxOpenConns    int    `mapstructure:"max_open_conns"`     // max open connections in pool
	MaxLifetime     int    `mapstructure:"max_lifetime"`       // max connection lifetime in seconds
	ConnMaxIdleTime int    `mapstructure:"conn_max_idle_time"` // max connection idle time in seconds
}

DatabaseConfig contains database connection settings. Supports mysql, postgres, and sqlite drivers.

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"`
	Enabled   bool          `mapstructure:"enabled"`
}

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 OtelConfig added in v0.0.3

type OtelConfig struct {
	Enabled       bool    `mapstructure:"enabled"`        // 是否启用 OpenTelemetry
	ServiceName   string  `mapstructure:"service_name"`   // 服务名称
	Environment   string  `mapstructure:"environment"`    // 环境标识: dev, test, prod
	OTLPEndpoint  string  `mapstructure:"otlp_endpoint"`  // OTLP gRPC 端点地址
	ExporterType  string  `mapstructure:"exporter_type"`  // 导出器类型: otlp, stdout
	SamplingRatio float64 `mapstructure:"sampling_ratio"` // 采样率: 0.0 - 1.0
}

OtelConfig contains OpenTelemetry 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