conf

package
v0.0.0-...-e06e715 Latest Latest
Warning

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

Go to latest
Published: May 14, 2022 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TextFormat 文本格式
	TextFormat = LogFormat("text")
	// JSONFormat json格式
	JSONFormat = LogFormat("json")
)
View Source
const (
	// ToFile 保存到文件
	ToFile = LogTo("file")
	// ToStdout 打印到标准输出
	ToStdout = LogTo("stdout")
)

Variables

This section is empty.

Functions

func LoadConfigFromEnv

func LoadConfigFromEnv() error

从环境变量中加载配置

func LoadConfigFromToml

func LoadConfigFromToml(filepath string) error

从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"`
	Key  string `toml:"key" env:"APP_KEY"`
}

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"`
}

func C

func C() *Config

要想配置,单独提供一个函数 全局Config对象获取函数

func NewDefaulConfig

func NewDefaulConfig() *Config

初始化一个有默认值的config对象

type Log

type Log struct {
	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"`
}

Log todo

func NewDefaulLog

func NewDefaulLog() *Log

type LogFormat

type LogFormat string

LogFormat 日志格式

type LogTo

type LogTo string

LogTo 日志记录到哪儿

type MySQL

type MySQL struct {
	Host     string `toml:"host" env:"D_MYSQL_HOST"`
	Port     string `toml:"port" env:"D_MYSQL_PORT"`
	UserName string `toml:"username" env:"D_MYSQL_USERNAME"`
	Password string `toml:"password" env:"D_MYSQL_PASSWORD"`
	Database string `toml:"database" env:"D_MYSQL_DATABASE"`
	//因为使用mysql链接池,需要池做一些规划配置
	//控制当前程序的mysql打开的连接数
	MaxOpenConn int `toml:"max_open_conn" env:"D_MYSQL_MAX_OPEN_CONN"`
	//控制mysql复用,比如5,最多运行5个来复用
	MaxIdleConn int `toml:"max_idle_conn" env:"D_MYSQL_MAX_IDLE_CONN"`
	//一个链接的生命周期,这个和mysql server 配置有关系,必须小于server配置
	//一个链接12h 换一个conn 保证一定的可用性
	MaxLifeTime int `toml:"max_life_time" env:"D_MYSQL_MAX_LIFE_TIME"`
	// Idle 链接最多允许存活多久
	MaxIdleTime int `toml:"max_idle_time" env:"D_MYSQL_MAX_idle_TIME"`
	// contains filtered or unexported fields
}

MySQL todo

func NewDefaultMysql

func NewDefaultMysql() *MySQL

func (*MySQL) GetDB

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

1.第一种方式使用loadGlobal 在加载时,初始化全局db实例 2.第二种方式,惰性加载,动态判断在初始化

Jump to

Keyboard shortcuts

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