config

package module
v3.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2019 License: MIT Imports: 10 Imported by: 1

README

config

一款简单的配置服务,使用.ini 文件来作为配置源

快速使用

go get github.com/scofieldpeng/config-go
1. 初始化
if err := config.Init(true);err != nil {
    log.Info(err)
    os.Exit(1)
}

在应用同级目录下创建 config 目录,专门用于管理配置文件,例如:

/example-app
 |- /config
 |    |- app.ini # release 环境会读取到该文件
 |    |- app_debug.ini # debug 环境会读取到该文件 
 |- app.go    
2. 使用

假设现在是 debug 模式,app_debug.ini 有如下配置

# app_debug.ini
[info]
version=1.0
// 第一个参数返回 version 值,如果不存在,第二个参数返回 false
// 其中的Data("app")中的 app 值为 app.ini(debug 模式下会读取 app_debug.ini 下的值)
version,ok := config.Data("app").Get("info","version")

// 另一种快捷方法
version := config.String(config.Data("app").Get("info","version"))

重载配置

err := config.Reload()

快速获取对应类型的值

字符:

string := config.String(config.Config["app"].Get("mysql", "default"))

整数:

int := config.Int(config.Config["app"].Get("mysql", "default"))

浮点数:

float64 := config.Float64(config.Config["app"].Get("mysql", "default"))

布尔值:

bool := config.bool(config.Config["app"].Get("mysql", "default"))

自定义配置文件目录

// 设置debug环境
debug := false
// 路径必须为绝对路径,并且以/结尾
absolutePath := `/home/scofield/app/config/`
config.Init( debug, NewFileParser(debug,absolutePath))

或者将上面代码中的absolutePaht定义成APP_CONFIG_DIR的环境变量,就可以直接使用:

config.Init(debug)

文件解析器的debug模式和非debug模式

在使用文件作为config时,默认情况下debug模式下会读取配置目录下的xxx_debug.ini文件,非debug读取xxx.ini文件

有时候我们不想进行_debug.ini和非_debug.ini的区分,如果有此需求,采用v2的默认解析器(文件解析)即可,用法

方法1: 添加环境变量CONFIG_VERSION=v2的值即可

方法2: 初始化时用v2进行初始化:

config.Init(debug,config.NewFileParserV2(debug))

环境变量配置文件

某些时候,我们的某些配置是是需要写成环境变量实现动态生成配置,默认的文件解析器支持环境变量,语法为${ENV_NAME},例如有下列配置文件

# app.ini
[redis]
default=${REDIS_DEFAULT_ADDR}

上面的${REDIS_DEFAULT_ADDR}代表该值需要用环境变量来替换,如果在运行时指代了环境变量,那么config在初始化后会默认将该值替换成对应的值

有时候我们需要当环境变量不存在时设置默认值,那么可以设置为这样

# app.ini
[redis]
default=${REDIS_DEFAULT_ADDR:=127.0.0.1:6379}

当找不到REDIS_DEFAULT_ADDR这个环境变量时,default值会用127.0.0.1:6379去填充

快捷设置环境变量 在配置文件的同目录下建立.env文件,一行一个,KEY=VALUE格式即可,程序启动时会自动将该文件的值设置到环境变量

具体 API

详见godoc.org/github.com/scofieldpeng/config-go

添加手动解析

只要实现了Parser结构体即可:

package config

// Parser 接口结构体
type Parser interface {
    // Parse 解析方法
    Parse() (map[string]ini.File, error)
}

系统已经内置了FileParser和ManualParser结构体,前者为默认的解析器,用于解析文件格式的配置,后者用户配置成手动注入的解析器

ManualParser用法
// 如果有如下配置想手动注入
// app.ini
// [system]
// version = beta0.1

p := config.NewManualParser()

// 方法1
p.SetConfig("app","system","version","beta0.1")
// 也可以直接快捷注入ini.Section
p.SetConfig("app","system",ini.Section{"version":"beta0.1"})

// 设置直接注入ini.File
p.SetConfig("app",ini.File{"system":ini.Section{"version":"beta0.1"}})

// 初始化config,Init的第一个Debug参数随便设置
if err := config.Init(false,p);err != nil {
	log.Error(err)
}

// 正常使用
config.String(config.Data("app").Get("sytem","version"))

Documentation

Index

Constants

View Source
const (
	// v1版本
	V1 = "v1"
	// v2版本
	V2 = "v2"
)

Variables

View Source
var (

	// 兼容旧的调用方法,不建议使用
	Config = c.data
	// 版本号
	Version = "v2.0"
)
View Source
var (
	// 文件没有找到的错误
	ErrFileNotExist = errors.New("file not exist")
)

Functions

func Bool

func Bool(val string, b bool) bool

Bool 返回布尔值数据

func Data

func Data(fileName string) ini.File

Data 获取数据

func Debug

func Debug() bool

Debug 返回debug状态

func Float64

func Float64(val string, b bool) float64

Float64 返回Float64值

func Init

func Init(debug bool, parser ...Parser) (err error)

Init 初始化 默认会读取fileParse(v1)来进行操作

func Int

func Int(val string, b bool) int

Int 返回整数型数据

func Reload

func Reload() error

Reload 重新载入

func SetDebug

func SetDebug(b bool)

SetDebug 设置测试环境

func String

func String(val string, b bool) string

String 返回String类型的数据

Types

type FileParser

type FileParser struct {
	Path    string
	Debug   bool
	Version string
}

FileParser 文件解析

func NewFileParser

func NewFileParser(debug bool, configPath ...string) FileParser

NewFileParser 新建文件 Parser

func NewFileParserV1

func NewFileParserV1(debug bool, configPath ...string) FileParser

func NewFileParserV2

func NewFileParserV2(debug bool, configPath ...string) FileParser

新建文件Parser(v2)版本

func (FileParser) Parse

func (f FileParser) Parse() (data map[string]ini.File, err error)

Parse 解析

type ManualParser

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

ManualParser 手动解析器

func NewManualParser

func NewManualParser() ManualParser

NewManualParser 新建手动解析器

func (ManualParser) Parse

func (mp ManualParser) Parse() (data map[string]ini.File, err error)

Parse 解析

func (*ManualParser) SetConfig

func (mp *ManualParser) SetConfig(appName, section, key, value string)

SetConfig 设置value

func (*ManualParser) SetFile

func (mp *ManualParser) SetFile(fileName string, data ini.File)

SetFile 设置file的值

func (*ManualParser) SetSection

func (mp *ManualParser) SetSection(fileName, section string, data ini.Section)

SetSection 设置section的值

type Parser

type Parser interface {
	// Parse 解析方法
	Parse() (map[string]ini.File, error)
}

Parser 接口结构体

Jump to

Keyboard shortcuts

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