configs

package
v0.0.0-...-cd938e1 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2020 License: MIT Imports: 9 Imported by: 0

README

conifgs

基于配置的第三方辅助工具

yaml -> go struct -> 具体业务

配置通常和某环境(dev,prod,qa)相关,通过多个配置的merge,比如default + dev,构成某目标环境的具体配置

目前适用范围:

  • mysql
  • thrift
  • redis
  • grpc server | client
  • kafka
  • ldap
  • fluent
  • elastic

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// contains filtered or unexported fields
}

Config raw config

func NewConfig

func NewConfig(options NewConfigOptions) Config

NewConfig 新建

func (*Config) MustScanConfig

func (x *Config) MustScanConfig(i interface{})

MustScanConfig 求出强类型配置

func (*Config) MustToYaml

func (x *Config) MustToYaml() (out []byte)

func (*Config) ScanConfig

func (x *Config) ScanConfig(conf interface{}) error

ScanConfig 求出强类型配置 todo 用 github.com/ghodss/yaml 来避免json/yaml转换问题

Example
package main

import (
	"fmt"
	"github.com/chen56/go-common/configs"
	"github.com/chen56/go-common/must"
)

type Log struct {
	Level string `yaml:"level" json:"level"`
}
type Conf struct {
	Log     Log    `yaml:"log"  json:"log"`
	AppInfo string `yaml:"appInfo"  json:"appInfo"`
}

func main() {
	var config1 = configs.NewMemoryConfigSource([]byte(`
log:
  level: error
appInfo: log=${log_level}
`))
	var config2 = configs.NewMemoryConfigSource([]byte(`
log:
  level: debug
`))

	c := configs.NewConfig(configs.NewConfigOptions{ConfigSources: []configs.ConfigSource{config1, config2}})

	conf := Conf{}
	err := c.ScanConfig(&conf)
	must.NoError(err)
	//spew.Dump(c)
	fmt.Printf("%+v", conf)

}
Output:

{Log:{Level:debug} AppInfo:log=debug}

func (*Config) ToYaml

func (x *Config) ToYaml() (out []byte, err error)

func (*Config) Visit

func (x *Config) Visit(visitor Vistor) error

Visit 遍历

type ConfigSource

type ConfigSource interface {
	fmt.Stringer
	Read() ([]byte, error)
}

ConfigSource 配置源,配置可以从字符串/文件/网络读取

func NewFileConfigSource

func NewFileConfigSource(path string) ConfigSource

NewFileConfigSource 文件配置源

func NewInterfaceConfigSource

func NewInterfaceConfigSource(conf interface{}) ConfigSource

NewMemoryConfigSource 内存配置源

func NewMemoryConfigSource

func NewMemoryConfigSource(data []byte) ConfigSource

NewMemoryConfigSource 内存配置源

type NewConfigOptions

type NewConfigOptions struct {
	ConfigSources []ConfigSource
}

NewConfigOptions 选项

type Vistor

type Vistor func(parentNode map[string]interface{}, nodeKey string, nodePath []string, node interface{}) error

Vistor Visit的访问器

Directories

Path Synopsis
grpc_auth copy from https://github.com/grpc-ecosystem/go-grpc-middleware/tree/master/auth 因为原auth的AuthFunc没有提供fullMethodName参数: type AuthFunc func(ctx context.Context) (context.Context, error) 所以复制来改为: type AuthFunc func(ctx context.Context, fullMethodName string) (context.Context, error) 没有其他修改,使用流程和原auth一致
grpc_auth copy from https://github.com/grpc-ecosystem/go-grpc-middleware/tree/master/auth 因为原auth的AuthFunc没有提供fullMethodName参数: type AuthFunc func(ctx context.Context) (context.Context, error) 所以复制来改为: type AuthFunc func(ctx context.Context, fullMethodName string) (context.Context, error) 没有其他修改,使用流程和原auth一致

Jump to

Keyboard shortcuts

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