conf

package
v0.0.0-...-5f20023 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ToFile   = LogTo("file")
	ToStdout = LogTo("stdout")
)

Variables

This section is empty.

Functions

func LoadConfigFromEnv

func LoadConfigFromEnv() error

2. 从环境变量中读取配置

func LoadConfigFromToml

func LoadConfigFromToml(filePath string) error

如何配置映射成config对象 1. 从toml文件中读取配置

func LoadGlobalDB

func LoadGlobalDB() error

Types

type App

type App struct {
	Name string `toml:"name" env:"APP_NAME"`
	Host string `toml:"host" env:"APP_HOST"`
	Port string `toml:"port" env:"APP_PORT"`
}

func NewDefaultApp

func NewDefaultApp() *App

type Config

type Config struct {
	App   *App   `toml:"app"`
	Log   *Log   `toml:"log"`
	MySQL *MySQL `toml:"mysql"`
}

应用配置对象

func C

func C() *Config

GetConfig 获取全局配置对象

func NewDefaultConfig

func NewDefaultConfig() *Config

NewDefaultConfig 创建默认配置

type Log

type Log struct {
	//debug, info, warn, error, dpanic, panic, fatal
	Level   string    `toml:"level" env:"LOG_LEVEL"`
	PathDir string    `toml:"path_dir" env:"LOG_PATH_DIR"`
	Format  LogFormat `toml:"format" env:"LOG_FORMAT"`
	To      LogTo     `toml:"to" env:"LOG_TO"`
}

func NewDefaultLog

func NewDefaultLog() *Log

type LogFormat

type LogFormat string

日志格式

const (
	TextFormat LogFormat = "text"
	JsonFormat LogFormat = "json"
)

type LogTo

type LogTo string

日志输出目的地

type MySQL

type MySQL struct {
	Host     string `toml:"host" env:"MYSQL_HOST"`
	Port     string `toml:"port" env:"MYSQL_PORT"`
	UserName string `toml:"username" env:"MYSQL_USERNAME"`
	Password string `toml:"password" env:"MYSQL_PASSWORD"`
	Database string `toml:"database" env:"MYSQL_DATABASE"`
	// 连接池配置,含义:最大空闲连接数
	MaxIdleConns int `toml:"max_idle_conns" env:"MYSQL_MAX_IDLE_CONNS"`
	// 最大打开连接数,含义:最大连接数
	MaxOpenConns int `toml:"max_open_conns" env:"MYSQL_MAX_OPEN_CONNS"`
	// 连接最大存活时间,含义:连接最大存活时间
	MaxLifetime int `toml:"max_lifetime" env:"MYSQL_MAX_LIFETIME"`
	// 连接最大空闲时间,含义:连接最大空闲时间
	MaxIdleTime int `toml:"max_idle_time" env:"MYSQL_MAX_IDLE_TIME"`
	// contains filtered or unexported fields
}

func NewDefaultMySQL

func NewDefaultMySQL() *MySQL

func (*MySQL) GetDB

func (m *MySQL) GetDB() *sql.DB

1.使用LoadGlobalDB()函数加载全局数据库连接 2.惰性加载,获取的时候动态加载再初始化

func (*MySQL) GetDBConn

func (m *MySQL) GetDBConn() (*sql.DB, error)

Jump to

Keyboard shortcuts

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