conf

package
v0.0.0-...-8e0b234 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2024 License: OSL-3.0 Imports: 14 Imported by: 0

README

程序配置对象

配置定义

type Config struct {
	MySQL *MySQL
}

// db对象也是一个单列模式
type MySQL struct {
	Host     string `json:"host" yaml:"host" toml:"host" env:"DATASOURCE_HOST"`
	Port     int    `json:"port" yaml:"port" toml:"port" env:"DATASOURCE_PORT"`
	DB       string `json:"database" yaml:"database" toml:"database" env:"DATASOURCE_DB"`
	Username string `json:"username" yaml:"username" toml:"username" env:"DATASOURCE_USERNAME"`
	Password string `json:"password" yaml:"password" toml:"password" env:"DATASOURCE_PASSWORD"`
	Debug    bool   `json:"debug" yaml:"debug" toml:"debug" env:"DATASOURCE_DEBUG"`

	// 判断这个私有属性, 来判断是否返回已有的对象
	db *gorm.DB
	l  sync.Mutex
}

配置加载

文件: TOML 为例
  • JSON/YAML/TOML: 都有对应的格式的解析库
# {"mysql": {"host":...}}
[mysql]
  host = "127.0.0.1"
  port = 3306
  database = ""
  username = ""
  password = ""
  debug = false

找到一个toml格式的解析库:

// github.com/BurntSushi/toml go里使用比较广泛的toml格式解析库
// https://github.com/BurntSushi/toml 查看该库的基本用法
// object <---> toml 配置文件
func LoadFromFile(filepath string) error {
	c := &Config{}
	if _, err := toml.DecodeFile(filepath, c); err != nil {
		return err
	}
	config = c
	return nil
}
环境变量
DATASOURCE_HOST="127.0.0.1"
DATASOURCE_PORT=3306
DATASOURCE_DB=""
DATASOURCE_USERNAME=""
DATASOURCE_PASSWORD=""
os.Getenv("DATASOURCE_HOST")
// "github.com/caarlos0/env/v6" 读取环境变量
// env ---> object
func LoadFromEnv() error {
	c := &Config{}
	// env Tag
	if err := env.Parse(c); err != nil {
		return err
	}
	config = c
	// c.MySQL.Host = os.Getenv("DATASOURCE_HOST")
	return nil
}
程序的默认值

提供默认值,让程序基本能跑(开发环境能跑)

func DefaultConfig() *Config {
	return &Config{
		MySQL: &MySQL{
			Host:     "127.0.0.1",
			Port:     3306,
			DB:       "vblog",
			Username: "root",
			Password: "123456",
			Debug:    true,
		},
	}
}

Documentation

Index

Constants

View Source
const (
	// 模块名称
	AppName = "conf"
)

Variables

This section is empty.

Functions

func LoadFromEnv

func LoadFromEnv() error

"github.com/caarlos0/env/v6" 读取环境变量 env ---> object

func LoadFromFile

func LoadFromFile(filepath string) error

该文件定义 配置对象的加载方法 github.com/BurntSushi/toml go里使用比较广泛的toml格式解析库 https://github.com/BurntSushi/toml 查看该库的基本用法 object <---> toml 配置文件

Types

type Application

type Application struct {
	Domain string `json:"domain" yaml:"domain" toml:"domain" env:"APP_DOMAIN"`
}

type Config

type Config struct {
	Application *Application `json:"app" yaml:"app" toml:"app"`
	MySQL       *MySQL       `json:"mysql" yaml:"mysql" toml:"mysql"`
	GrpcServer  *GrpcServer  `json:"grpc" yaml:"grpc" toml:"grpc"`
}

程序配置对象, 启动时 会读取配置, 并且为程序提供需要全局变量 把配置对象做出全局变量(单列模式)

toml

mysql host="127.0.0.1" port=3306 ...

func C

func C() *Config

这里就可以补充逻辑

func DefaultConfig

func DefaultConfig() *Config

func (*Config) DB

func (c *Config) DB() *gorm.DB

配置对象提供全局单列配置

func (*Config) Destory

func (i *Config) Destory() error

ioc 对象方法约束

func (*Config) Init

func (i *Config) Init() error

ioc 对象方法约束

func (*Config) String

func (c *Config) String() string
fmt.Stringer

如果你想要自定义 对象fmt.PrintXXX() 打印的值 String() string &{0x1400007c600} ---> JSON {}

type GrpcServer

type GrpcServer struct {
	Host string `json:"host" yaml:"host" toml:"host"`
	Port int    `json:"port" yaml:"port" toml:"port"`
	// contains filtered or unexported fields
}

func (*GrpcServer) Address

func (s *GrpcServer) Address() string

func (*GrpcServer) GetServer

func (s *GrpcServer) GetServer() *grpc.Server

func (*GrpcServer) Start

func (s *GrpcServer) Start()

type MySQL

type MySQL struct {
	Host     string `json:"host" yaml:"host" toml:"host" env:"DATASOURCE_HOST"`
	Port     int    `json:"port" yaml:"port" toml:"port" env:"DATASOURCE_PORT"`
	DB       string `json:"database" yaml:"database" toml:"database" env:"DATASOURCE_DB"`
	Username string `json:"username" yaml:"username" toml:"username" env:"DATASOURCE_USERNAME"`
	Password string `json:"password" yaml:"password" toml:"password" env:"DATASOURCE_PASSWORD"`
	Debug    bool   `json:"debug" yaml:"debug" toml:"debug" env:"DATASOURCE_DEBUG"`
	// contains filtered or unexported fields
}

db对象也是一个单列模式

func (*MySQL) DSN

func (m *MySQL) DSN() string

dsn := "user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local"

func (*MySQL) GetDB

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

通过配置就能通过一个DB 实例

Jump to

Keyboard shortcuts

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