config_provider

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2023 License: MIT Imports: 6 Imported by: 0

README

config-provider-go

Provide multiple configuration sources and format support for applications. Based on https://github.com/spf13/viper

Features

Support Source
  • Local config file
  • Nacos
Support Config Format
  • JSON
  • YAML
  • Properties
  • TOML
  • INI
Watch Config Changed

Support watch configuration changes

Dependents

Example

From Local File
package main

import (
	"fmt"
	config_provider "github.com/zhangsq-ax/config-provider-go"
	options "github.com/zhangsq-ax/config-provider-go/options"
	constants "github.com/zhangsq-ax/config-provider-go/constants"
)

func main() {
	provider, err := config_provider.NewConfigProvider(&options.ConfigProviderOptions{
		ConfigFormat: constants.YAML,
		ConfigSource: constants.FILE,
		ConfigFile:   "./config.yml",
		WatchConfig: true,
		OnConfigChange: func() {
			fmt.Println("config changed")
			// to do something
        },
    })
	if err != nil {
        panic(err)
	}
	
	fmt.Println(provider.Instance().AllKeys())
}
From Nacos
package main

import (
	"fmt"
	config_provider "github.com/zhangsq-ax/config-provider-go"
	options "github.com/zhangsq-ax/config-provider-go/options"
	constants "github.com/zhangsq-ax/config-provider-go/constants"
)

func main() {
	provider, err := config_provider.NewConfigProvider(&options.ConfigProviderOptions{
		ConfigFormat: constants.YAML,
		ConfigSource: constants.NACOS,
		WatchConfig: true,
		OnConfigChange: func() {
			fmt.Println("config changed")
			// to do something
        },
		NacosOptions: &options.NacosOptions{
			Host:        "localhost",
			Port:        8848,
			NamespaceId: "public",
			Group:       "DEFAULT_GROUP",
			DataId:      "...",
			Username:    "...",
			Password:    "...",
        },
    })
	if err != nil {
        panic(err)
	}
	
	fmt.Println(provider.Instance().AllKeys())
}

Methods

NewConfigProvider(opts *options.ConfigProviderOptions)

Instantiate a config provider

provider.Instance()

Get instance of the config provider. The instance is a Viper object, configuration content reading methods reference https://github.com/spf13/viper

Todo

  • Support etcd as remote config source
  • Support consul as remote config source
  • Support firestore as remote config source

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConfigProvider

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

ConfigProvider Config provider

func NewConfigProvider

func NewConfigProvider(opts *options.ConfigProviderOptions) (*ConfigProvider, error)

NewConfigProvider Instantiate the config provider

func (*ConfigProvider) Instance

func (provider *ConfigProvider) Instance() *viper.Viper

Instance Get instance of the config provider

func (*ConfigProvider) OnConfigChanged

func (provider *ConfigProvider) OnConfigChanged(configChangedCallback func())

OnConfigChanged Set config changed callback

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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