config

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2022 License: Apache-2.0 Imports: 11 Imported by: 7

README

config包

config包主要用于加载和管理项目中配置文件中的内容,配置文件为"application"开头的格式

1. 配置文件路径

默认该文件与main函数所在的类同目录

// 示例
- application.yml
- application-local.yml

2. 配置文件格式

支持yaml、yml、json、properties配置文件 优先级: json > properties > yaml > yml

3. 支持profile加载不同配置文件

格式:application-{profile}.yyy 其中profile对应的变量为:base.profiles.active 变量的设置可以有如下

  • 本地配置
  • 环境变量配置

优先级:环境变量 > 本地配置

img.png

4. 内置的配置文件自动加载

目前内置的自动加载的配置文件有如下这些,后续随着工程越来越大会越来越多

api-module: sample
base:
  api:
    # api前缀
    prefix: /api
  application:
    # 应用名称
    name: sample
  server:
    # 是否启用,默认:true
    enable: true
    # 端口号
    port: 8080
    # web框架gin的配置
    gin:
      # 有三种模式:debug/release/test
      mode: debug
  endpoint:
    # 健康检查处理,默认关闭,true/false
    health:
      enable: true
    # 配置的动态实时变更,默认关闭,true/false
    config:
      enable: true

5. 支持直接获取配置值

config包中提供了各种类型的api,方便实时获取

// 基本类型
config.getValueInt("xxx.xxx")
config.getValueInt32("xxx.xxx")
config.getValueInt64("xxx.xxx")
config.getValueBool("xxx.xxx")
config.getValueString("xxx.xxx")
// ...

// 结构类型
config.getValueObject("xxx.xxx", &xxx)

示例:

var ServerCfg ServerConfig

// base前缀
type BaseConfig struct {
    Application AppApplication
    Data string
}

type AppApplication struct {
    Name string
}
base:
  application:
    name: "xxx-local"
  data: "test"
// 直接读取即可
config.getValueObject("base", &ServerCfg)

6. 支持文件的绝对和相对路径读取

配置路径默认是与main同目录,也支持绝对路径读取对应的配置,该api可以用于与运维同学约定的服务器路径位置

// 相对路径
config.LoadConfigFromRelativePath(xx)

// 绝对路径
config.LoadConfigFromAbsPath(xx)

7. 支持配置的叠加,相对路径和绝对路径

在配置已经加载完毕后,需要对一些配置进行覆盖,比如运维这边有相关的需求时候

// 相对路径
config.AppendConfigFromRelativePath(xx)

// 绝对路径
config.AppendConfigFromAbsPath(xx)

8. 支持自动读取cm文件

应用启动会默认读取/home/{base.application.name}/config/application-default.yml对应的内容并覆盖应用的配置中

9. 支持配置的在线查看以及实时变更

如下配置配置开启后,就可以在线查看应用的所有配置了

base:
  endpoint:
    # 配置的动态实时变更,默认关闭
    config:
      enable: true/false
// 查看应用所有配置
curl http://localhost:xxx/{api-module}system/config/values

// 查看应用的某个配置
curl http://localhost:xxx/{api-module}system/config/value/{key}

// 修改应用的配置
curl -X PUT http://localhost:xxx/{api-module}system/config/update -d '{"key":"xxx", "value":"yyyy"}'
注意

该配置的修改只对config.getValueXXX() 这种实时调用的配置有效,配置使用方式有两种

  • 实体化:在对应文件中直接将配置实体化起来
  • api实时调用:代码中使用时候直接调用config.getValueXXX()

其中动态变更只对api实时调用的方式有效

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ApiModule string

Functions

func AppendConfigFromAbsPath added in v0.3.0

func AppendConfigFromAbsPath(fileName string)

AppendConfigFromAbsPath 追加配置:绝对路径的配置文件

func AppendConfigFromRelativePath added in v0.3.0

func AppendConfigFromRelativePath(fileName string)

AppendConfigFromRelativePath 追加配置:相对路径的配置文件

func AppendJsonFile added in v0.3.0

func AppendJsonFile(filePath string)

func AppendPropertyFile added in v0.3.0

func AppendPropertyFile(filePath string)

func AppendYamlFile added in v0.3.0

func AppendYamlFile(filePath string)

func ExistConfigFile added in v1.0.0

func ExistConfigFile() bool

func GetConfigValue added in v0.3.0

func GetConfigValue(c *gin.Context)

func GetConfigValues added in v0.3.0

func GetConfigValues(c *gin.Context)

func GetValue added in v0.3.0

func GetValue(key string) any

func GetValueArray added in v1.0.0

func GetValueArray(key string) []any

func GetValueArrayInt added in v1.0.0

func GetValueArrayInt(key string) []int

func GetValueBool added in v0.3.0

func GetValueBool(key string) bool

func GetValueBoolDefault added in v0.3.0

func GetValueBoolDefault(key string, defaultValue bool) bool

func GetValueFloat32 added in v0.3.0

func GetValueFloat32(key string) float32

func GetValueFloat32Default added in v0.3.0

func GetValueFloat32Default(key string, defaultValue float32) float32

func GetValueFloat64 added in v0.3.0

func GetValueFloat64(key string) float64

func GetValueFloat64Default added in v0.3.0

func GetValueFloat64Default(key string, defaultValue float64) float64

func GetValueInt added in v0.3.0

func GetValueInt(key string) int

func GetValueInt16 added in v0.3.0

func GetValueInt16(key string) int16

func GetValueInt16Default added in v0.3.0

func GetValueInt16Default(key string, defaultValue int16) int16

func GetValueInt32 added in v0.3.0

func GetValueInt32(key string) int32

func GetValueInt32Default added in v0.3.0

func GetValueInt32Default(key string, defaultValue int32) int32

func GetValueInt64 added in v0.3.0

func GetValueInt64(key string) int64

func GetValueInt64Default added in v0.3.0

func GetValueInt64Default(key string, defaultValue int64) int64

func GetValueInt8 added in v0.3.0

func GetValueInt8(key string) int8

func GetValueInt8Default added in v0.3.0

func GetValueInt8Default(key string, defaultValue int8) int8

func GetValueIntDefault added in v0.3.0

func GetValueIntDefault(key string, defaultValue int) int

func GetValueObject added in v0.3.0

func GetValueObject(key string, targetPtrObj any) error

func GetValueString added in v0.3.0

func GetValueString(key string) string

func GetValueStringDefault added in v0.3.0

func GetValueStringDefault(key, defaultValue string) string

func GetValueUInt added in v0.3.0

func GetValueUInt(key string) uint

func GetValueUInt16 added in v0.3.0

func GetValueUInt16(key string) uint16

func GetValueUInt16Default added in v0.3.0

func GetValueUInt16Default(key string, defaultValue uint16) uint16

func GetValueUInt32 added in v0.3.0

func GetValueUInt32(key string) uint32

func GetValueUInt32Default added in v0.3.0

func GetValueUInt32Default(key string, defaultValue uint32) uint32

func GetValueUInt64 added in v0.3.0

func GetValueUInt64(key string) uint64

func GetValueUInt64Default added in v0.3.0

func GetValueUInt64Default(key string, defaultValue uint64) uint64

func GetValueUInt8 added in v0.3.0

func GetValueUInt8(key string) uint8

func GetValueUInt8Default added in v0.3.0

func GetValueUInt8Default(key string, defaultValue uint8) uint8

func GetValueUIntDefault added in v0.3.0

func GetValueUIntDefault(key string, defaultValue uint) uint

func LoadConfig

func LoadConfig()

func LoadConfigFromAbsPath added in v0.3.0

func LoadConfigFromAbsPath(resourceAbsPath string)

LoadConfigFromAbsPath 加载绝对文件路径

func LoadConfigFromRelativePath added in v0.3.0

func LoadConfigFromRelativePath(resourceAbsPath string)

LoadConfigFromRelativePath 加载相对文件路径

func LoadJsonFile added in v0.3.0

func LoadJsonFile(filePath string)

func LoadPropertyFile added in v0.3.0

func LoadPropertyFile(filePath string)

func LoadSpringConfig added in v0.3.0

func LoadSpringConfig(AConfig any)

func LoadYamlConfig added in v0.3.0

func LoadYamlConfig(fileName string, AConfig any, handler func(data []byte, AConfig any) error) error

LoadYamlConfig read fileName from private path fileName,eg:application.yml, and transform it to AConfig note: AConfig must be a pointer

func LoadYamlConfigByAbsolutPath added in v0.3.0

func LoadYamlConfigByAbsolutPath(path string, AConfig any, handler func(data []byte, AConfig any) error) error

LoadYamlConfigByAbsolutPath read fileName from absolute path fileName,eg:/home/isc-gobase/application.yml, and transform it to AConfig note: AConfig must be a pointer

func LoadYamlFile added in v0.3.0

func LoadYamlFile(filePath string)

func SetValue added in v0.3.0

func SetValue(key string, value any)

func UpdateConfig added in v0.3.0

func UpdateConfig(c *gin.Context)

Types

type ApplicationProperty added in v0.3.0

type ApplicationProperty struct {
	ValueMap     map[string]any
	ValueDeepMap map[string]any
}

type BaseApi added in v1.0.0

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

type BaseApplication added in v1.0.0

type BaseApplication struct {
	Name string `yaml:"name"` // 应用名字
}

type BaseConfig added in v0.3.0

type BaseConfig struct {
	Api         BaseApi         `yaml:"api"`
	Application BaseApplication `yaml:"application"`
	Server      BaseServer      `yaml:"server"`
	EndPoint    BaseEndPoint    `yaml:"endpoint"`
	Logger      BaseLogger      `yaml:"logger"`
	Profiles    BaseProfile     `yaml:"profiles"`
}

BaseConfig base前缀

var BaseCfg BaseConfig

type BaseEndPoint added in v1.0.0

type BaseEndPoint struct {
	Health EndPointHealth `yaml:"health"` // 健康检查[端点]
	Config EndPointConfig `yaml:"config"` // 配置管理[端点]
}

type BaseException added in v1.0.0

type BaseException struct {
	Print ExceptionPrint `yaml:"print"` // 异常返回打印
}

type BaseGin added in v1.0.0

type BaseGin struct {
	Mode string `yaml:"mode"` // 有三种模式:debug/release/test
}

type BaseLogger added in v1.0.0

type BaseLogger struct {
	Level string      `yaml:"level"` // 日志root级别:trace/debug/info/warn/error/fatal/panic,默认:info
	Time  LoggerTime  `yaml:"time"`  // 时间配置
	Color LoggerColor `yaml:"color"` // 日志颜色
	Split LoggerSplit `yaml:"split"` // 日志切分
}

type BaseProfile added in v1.0.0

type BaseProfile struct {
	Active string `yaml:"active"`
}

type BaseServer added in v1.0.0

type BaseServer struct {
	Enable    bool          `yaml:"enable"`    // 是否启用
	Port      int           `yaml:"port"`      // 端口号
	Gin       BaseGin       `yaml:"gin"`       // web框架gin的配置
	Exception BaseException `yaml:"exception"` // 异常处理
}

type EndPointConfig added in v1.0.0

type EndPointConfig struct {
	Enable bool `yaml:"enable"` // 是否启用
}

type EndPointHealth added in v1.0.0

type EndPointHealth struct {
	Enable bool `yaml:"enable"` // 是否启用
}

type EnvProperty added in v0.3.0

type EnvProperty struct {
	Key   string
	Value string
}

type ExceptionPrint added in v1.0.0

type ExceptionPrint struct {
	Enable bool  `yaml:"enable"` // 是否启用
	Except []int `yaml:"except"` // 排除的httpStatus;默认可不填
}

type LoggerColor added in v1.0.0

type LoggerColor struct {
	Enable bool `yaml:"enable"` // 是否启用
}

type LoggerSplit added in v1.0.0

type LoggerSplit struct {
	Enable bool `yaml:"enable"` // 日志是否启用切分:true/false,默认false
	Size   int  `yaml:"size"`   // 日志拆分的单位:MB
}

type LoggerTime added in v1.0.0

type LoggerTime struct {
	Format string `yaml:"format"` // 时间格式,time包中的内容,比如:time.RFC3339
}

type StorageConnectionConfig added in v0.3.0

type StorageConnectionConfig struct {
	Host       string `yaml:"host"`
	Port       int    `yaml:"port"`
	User       string `yaml:"user"`
	Password   string `yaml:"password"`
	Parameters string `yaml:"parameters"`
}

Jump to

Keyboard shortcuts

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