Documentation
¶
Overview ¶
config package is a package for managing application configuration. It provides a BaseConfig struct that holds the configuration for the application. The package also provides functions to load the configuration from a file or environment variables, set default values, and validate the configuration.
This file is used to configure the log system ¶
This file is used to configure the server ¶
This file is used to configure the SQL-database
Index ¶
- Constants
- func WithCustomConfig(custom interface{}) func(*BaseConfig)
- type BaseConfig
- type Email
- type LogConfig
- type LogConfigConfigOption
- func WithCompress(compress bool) LogConfigConfigOption
- func WithFilename(filename string) LogConfigConfigOption
- func WithFormat(format string) LogConfigConfigOption
- func WithLevel(level string) LogConfigConfigOption
- func WithMaxAge(maxAge int) LogConfigConfigOption
- func WithMaxBackups(maxBackups int) LogConfigConfigOption
- func WithMaxSize(maxSize int) LogConfigConfigOption
- func WithOutput(output string) LogConfigConfigOption
- type Metrics
- type SQLConfig
- type SQLConfigOption
- func WithDB(db string) SQLConfigOption
- func WithDebug(debug bool) SQLConfigOption
- func WithHost(host string) SQLConfigOption
- func WithMaxIdleConns(n int) SQLConfigOption
- func WithMaxOpenConns(n int) SQLConfigOption
- func WithPassword(password string) SQLConfigOption
- func WithType(dbType string) SQLConfigOption
- func WithUser(user string) SQLConfigOption
- type ServerConfig
- type ServerConfigOption
- func WithBindAddr(addr string) ServerConfigOption
- func WithMetrics(metrics *Metrics) ServerConfigOption
- func WithMetricsEnabled(enabled bool) ServerConfigOption
- func WithMetricsExcludeItem(items []string) ServerConfigOption
- func WithMetricsPath(path string) ServerConfigOption
- func WithMetricsPort(port int) ServerConfigOption
- func WithMetricsServiceName(name string) ServerConfigOption
- func WithMetricsTimeSensitive(enabled bool) ServerConfigOption
- func WithTrace(trace *Trace) ServerConfigOption
- func WithTraceEnabled(enabled bool) ServerConfigOption
- func WithTraceEndpoint(endpoint string) ServerConfigOption
- func WithTraceExcludeItem(items []string) ServerConfigOption
- func WithTraceServiceName(name string) ServerConfigOption
- func WithTraceStdout(enabled bool) ServerConfigOption
- type Trace
Constants ¶
const ( DebugLevel = "debug" InfoLevel = "info" WarnLevel = "warn" ErrorLevel = "error" FatalLevel = "fatal" )
Log levels
const ( DebugLevelNum = 4 InfoLevelNum = 3 WarnLevelNum = 2 ErrorLevelNum = 1 FatalLevelNum = 0 )
Log level numbers
const ( MySQL = "mysql" Sqlite3 = "sqlite3" )
support mysql and sqlite
Variables ¶
This section is empty.
Functions ¶
func WithCustomConfig ¶ added in v0.1.2
func WithCustomConfig(custom interface{}) func(*BaseConfig)
WithCustomConfig sets the custom configuration
Types ¶
type BaseConfig ¶ added in v0.1.2
type BaseConfig struct {
// server configuration
Server *ServerConfig `mapstructure:"server"`
// SQL database configuration
SQL *SQLConfig `mapstructure:"sql"`
// log configuration
Log *LogConfig `mapstructure:"log"`
// custom configuration, can be any type
Custom interface{} `mapstructure:"custom"`
}
BaseConfig is the base configuration
func NewConfig ¶ added in v0.1.2
func NewConfig() *BaseConfig
NewConfig creates a new configuration with default values
func (*BaseConfig) Load ¶ added in v0.1.2
func (bc *BaseConfig) Load(name string, env bool)
Load loads configuration from the specified file and optionally parses custom config name: configuration file path env: whether to load from environment variables
func (*BaseConfig) ParseCustomConfig ¶ added in v0.1.2
func (bc *BaseConfig) ParseCustomConfig(out interface{}) error
ParseCustomConfig parses the custom configuration into the provided interface
type Email ¶
type Email struct {
Account string `fig:"account"`
SMTP string `fig:"smtp"`
Port int `fig:"port"`
Password string `fig:"password"`
}
type LogConfig ¶ added in v0.1.2
type LogConfig struct {
// log file path
Filename string `mapstructure:"filename"`
// log file max size, unit MB
MaxSize int `mapstructure:"maxSize"`
// log file max backups
MaxBackups int `mapstructure:"maxBackups"`
// log file max age, unit day
MaxAge int `mapstructure:"maxAge"`
// log file compress
Compress bool `mapstructure:"compress"`
// log level, debug, info, warn, error, fatal
Level string `mapstructure:"level"`
// log format, only support string and json
Format string `mapstructure:"format"`
// log output, only support stdout and file
Output string `mapstructure:"output"`
}
LogConfig
func NewLogConfig ¶ added in v0.1.2
func NewLogConfig(opts ...LogConfigConfigOption) (*LogConfig, error)
NewLogConfig creates a new LogConfig config with the given options
type LogConfigConfigOption ¶ added in v0.1.2
type LogConfigConfigOption func(*LogConfig)
LogConfigConfigOption is used to configure the log system
func WithCompress ¶ added in v0.1.2
func WithCompress(compress bool) LogConfigConfigOption
WithCompress sets the log file compress option
func WithFilename ¶ added in v0.1.2
func WithFilename(filename string) LogConfigConfigOption
WithFilename sets the log filename
func WithFormat ¶ added in v0.1.2
func WithFormat(format string) LogConfigConfigOption
WithFormat sets the log format
func WithLevel ¶ added in v0.1.2
func WithLevel(level string) LogConfigConfigOption
WithLevel sets the log level
func WithMaxAge ¶ added in v0.1.2
func WithMaxAge(maxAge int) LogConfigConfigOption
WithMaxAge sets the log file max age
func WithMaxBackups ¶ added in v0.1.2
func WithMaxBackups(maxBackups int) LogConfigConfigOption
WithMaxBackups sets the log file max backups
func WithMaxSize ¶ added in v0.1.2
func WithMaxSize(maxSize int) LogConfigConfigOption
WithMaxSize sets the log file max size
func WithOutput ¶ added in v0.1.2
func WithOutput(output string) LogConfigConfigOption
WithOutput sets the log output
type Metrics ¶ added in v0.1.2
type Metrics struct {
Enabled bool `mapstructure:"enabled"`
ServiceName string `mapstructure:"serviceName"`
Path string `mapstructure:"path"`
Port int `mapstructure:"port"`
// TimeSensitive is a flag to enable time sensitive metrics buckets
TimeSensitive bool `mapstructure:"timeSensitive"`
// ExcludeItem is a list of items to exclude from metrics, e.g. /health
ExcludeItem []string `mapstructure:"excludeItem"`
}
Metrics defines the metrics configuration options
type SQLConfig ¶ added in v0.1.2
type SQLConfig struct {
// Database type only support mysql and sqlite, default sqlite
Type string `mapstructure:"type"`
// Database host, include port such as 127.0.0.1:3306
Host string `mapstructure:"host"`
// Database user
User string `mapstructure:"user"`
// Database password
Password string `mapstructure:"password"`
// Database name
DB string `mapstructure:"db"`
// Maximum number of idle connections
MaxIdleConns int `mapstructure:"maxIdleConns"`
// Maximum number of open connections
MaxOpenConns int `mapstructure:"maxOpenConns"`
// Print raw sql for debugging
Debug bool `mapstructure:"debug"`
}
SQLConfig is used to configure the SQL-database
func NewSQLConfig ¶ added in v0.1.2
func NewSQLConfig(opts ...SQLConfigOption) (*SQLConfig, error)
NewSQLConfig creates a new SQLConfig with the given options
type SQLConfigOption ¶ added in v0.1.2
type SQLConfigOption func(*SQLConfig)
SQLConfigOption is used to configure the SQL-database
func WithDebug ¶ added in v0.1.2
func WithDebug(debug bool) SQLConfigOption
WithDebug sets the SQL debug mode
func WithHost ¶ added in v0.1.2
func WithHost(host string) SQLConfigOption
WithHost sets the database host
func WithMaxIdleConns ¶ added in v0.1.2
func WithMaxIdleConns(n int) SQLConfigOption
WithMaxIdleConns sets the maximum number of idle connections
func WithMaxOpenConns ¶ added in v0.1.2
func WithMaxOpenConns(n int) SQLConfigOption
WithMaxOpenConns sets the maximum number of open connections
func WithPassword ¶ added in v0.1.2
func WithPassword(password string) SQLConfigOption
WithPassword sets the database password
func WithType ¶ added in v0.1.2
func WithType(dbType string) SQLConfigOption
WithType sets the database type
func WithUser ¶ added in v0.1.2
func WithUser(user string) SQLConfigOption
WithUser sets the database user
type ServerConfig ¶ added in v0.1.2
type ServerConfig struct {
// Server bind address, e.g. 0.0.0.0:8080
BindAddr string `mapstructure:"bindAddr"`
// Metrics
Metrics *Metrics `mapstructure:"metrics"`
// Trace
Trace *Trace `mapstructure:"trace"`
}
ServerConfig defines the server configuration options
func NewServerConfig ¶ added in v0.1.2
func NewServerConfig(opts ...ServerConfigOption) (*ServerConfig, error)
NewServerConfig creates a new ServerConfig with the given options
type ServerConfigOption ¶ added in v0.1.2
type ServerConfigOption func(*ServerConfig)
ServerConfigOption is used to configure the server
func WithBindAddr ¶ added in v0.1.2
func WithBindAddr(addr string) ServerConfigOption
WithBindAddr sets the server bind address
func WithMetrics ¶ added in v0.1.2
func WithMetrics(metrics *Metrics) ServerConfigOption
WithMetrics sets the metrics configuration
func WithMetricsEnabled ¶ added in v0.1.2
func WithMetricsEnabled(enabled bool) ServerConfigOption
WithMetricsEnabled enables/disables metrics
func WithMetricsExcludeItem ¶ added in v0.1.2
func WithMetricsExcludeItem(items []string) ServerConfigOption
func WithMetricsPath ¶ added in v0.1.2
func WithMetricsPath(path string) ServerConfigOption
WithMetricsPath sets the metrics path
func WithMetricsPort ¶ added in v0.1.2
func WithMetricsPort(port int) ServerConfigOption
WithMetricsPort sets the metrics port
func WithMetricsServiceName ¶ added in v0.1.2
func WithMetricsServiceName(name string) ServerConfigOption
WithMetricsServiceName sets the metrics service name
func WithMetricsTimeSensitive ¶ added in v0.1.2
func WithMetricsTimeSensitive(enabled bool) ServerConfigOption
func WithTrace ¶ added in v0.1.2
func WithTrace(trace *Trace) ServerConfigOption
func WithTraceEnabled ¶ added in v0.1.2
func WithTraceEnabled(enabled bool) ServerConfigOption
func WithTraceEndpoint ¶ added in v0.1.2
func WithTraceEndpoint(endpoint string) ServerConfigOption
func WithTraceExcludeItem ¶ added in v0.1.2
func WithTraceExcludeItem(items []string) ServerConfigOption
func WithTraceServiceName ¶ added in v0.1.2
func WithTraceServiceName(name string) ServerConfigOption
func WithTraceStdout ¶ added in v0.1.2
func WithTraceStdout(enabled bool) ServerConfigOption
type Trace ¶ added in v0.1.2
type Trace struct {
Enabled bool `mapstructure:"enabled"`
// ServiceName is the application service name
ServiceName string `mapstructure:"serviceName"`
// Stdout is a flag to enable/disable stdout
Stdout bool `mapstructure:"stdout"`
// Endpoint is the OTEL collector endpoint to send traces to
Endpoint string `mapstructure:"endpoint"`
// ExcludeItem is a list of items to exclude from tracing, e.g. /health
ExcludeItem []string `mapstructure:"excludeItem"`
}
Trace defines the trace configuration options