conf

package
v0.0.0-...-14ee9cd Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2022 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TextFormat = LogFormat("text")
	JSONFormat = LogFormat("json")
)
View Source
const (
	ToFile   = LogTo("file")
	ToStdout = LogTo("stdout")
)

Variables

This section is empty.

Functions

func LoadConfigFromTEnv

func LoadConfigFromTEnv() error

LoadConfigFromTEnv 从环境变量中加载配置

func LoadConfigFromToml

func LoadConfigFromToml(filePath string) error

LoadConfigFromToml 从toml文件中加载配置

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

func (*App) HttpAddr

func (a *App) HttpAddr() string

type Config

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

Config config 应用配置,通过封装一个对象,来与外部配置进行对接

func C

func C() *Config

func NewDefaultConfig

func NewDefaultConfig() *Config

type Log

type Log struct {
	Level   string    `toml:"level" env:"LOG_LEVEL"`
	Format  LogFormat `toml:"format" env:"LOG_FORMAT"`
	To      LogTo     `toml:"to" env:"LOG_TO"`
	PathDir string    `toml:"path_dir" env:"LOG_PATH_DIR"`
}

func NewDefaultLog

func NewDefaultLog() *Log

type LogFormat

type LogFormat string

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"`
	// 因为使用的MySQL连接池, 需要池做一些规划配置
	// 控制当前程序的MySQL打开的连接数
	MaxOpenConn int `toml:"max_open_conn" env:"MYSQL_MAX_OPEN_CONN"`
	//控制mysql的复用 比如5 最多运行5个复用
	MaxIdleConn int `toml:"max_idle_conn" env:"MYSQL_MAX_IDLE_CONN"`
	// 一个连接的生命周期 这个跟 mysql 的server 的配置有关系, 例子:一个连接12h,保证一定的可用性
	MaxLifeTime int `toml:"max_life_time" env:"MYSQL_MAX_LIFE_TIME"`
	//idle的最多允许 存活多久
	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. 第一种方式, 使用LoadGlobal 在加载时 初始化全局db实例 2. 第二种方式, 惰性加载, 获取DB是,动态判断再初始化

Jump to

Keyboard shortcuts

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