setting

package
v1.64.58 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

README

配置文件

package main

import (
	`log`

	`github.com/aid297/aid/setting`
	`github.com/aid297/aid/str`
	`github.com/aid297/aid/web-site/backend/aid-web-backend/src/global`
	`github.com/fsnotify/fsnotify`
	`github.com/spf13/viper`
)

func main() {
	var config any

	_, err := setting.NewSetting(
		setting.Filename("local.yaml"),   // 文件名
		setting.EnvName("CONFIG"),        // 环境变量
		setting.DefaultName("test.yaml"), // 默认名
		setting.Content(&config),         // 获取配置文件
		setting.OnChange(func(v *viper.Viper, e fsnotify.Event) {
			log.Println("配置文件发生变化")
			if err := v.Unmarshal(&global.CONFIG); err != nil {
				global.LOG.Error(str.APP.Buffer.JoinString("更新配置文件失败:", err.Error()))
			}
		}),
	)
	if err != nil {
		panic(err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrConfigNotSet       = errors.New("配置文件路径未设置")
	ErrConfigFileNotFound = errors.New("配置文件未找到")
	ErrConfigReadFailed   = errors.New("读取配置文件失败")
)

Functions

func NewSetting added in v1.63.4

func NewSetting(attrs ...SettingAttributes) (v *viper.Viper, err error)

Types

type AttrConfig

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

******************** 配置文件属性 ********************

func Content

func Content(content any) AttrConfig

func (AttrConfig) Register

func (my AttrConfig) Register(setting *Setting)

type AttrDefaultName added in v1.63.6

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

******************** 配置文件属性 ********************

func DefaultName added in v1.63.6

func DefaultName(name string) AttrDefaultName

func (AttrDefaultName) Register added in v1.63.6

func (my AttrDefaultName) Register(setting *Setting)

type AttrEnvName

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

******************** 配置文件属性 ********************

func EnvName

func EnvName(envName string) AttrEnvName

func (AttrEnvName) Register

func (my AttrEnvName) Register(setting *Setting)

type AttrFilename added in v1.63.6

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

******************** 配置文件属性 ********************

func Filename added in v1.63.6

func Filename(filename string) AttrFilename

func (AttrFilename) Register added in v1.63.6

func (my AttrFilename) Register(setting *Setting)

type AttrOnChange

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

******************** 配置文件属性 ********************

func OnChange

func OnChange(onChange func(v *viper.Viper, e fsnotify.Event)) AttrOnChange

func (AttrOnChange) Register

func (my AttrOnChange) Register(setting *Setting)

type Setting

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

******************** 配置文件 ******************** Setting

func (Setting) SetAttrs

func (my Setting) SetAttrs(attrs ...SettingAttributes) Setting

type SettingAttributes

type SettingAttributes interface{ Register(setting *Setting) }

******************** 配置文件属性 ********************

Jump to

Keyboard shortcuts

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