config

package
v0.0.0-...-37e343e Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2023 License: Apache-2.0 Imports: 6 Imported by: 5

README

配置管理

介紹

用來管理系統配置,目前支援 環境變數, 文件檔案 等模組, 執行順序依照加入模組的先後

  • 環境變數: 讀取環境變數
  • 文件檔案: 讀取檔案內容,支援 YAML, JSON 等格式

使用方式

範例配置文件 (app.yml)

app_id: blackbear
web:
  port: 10080
  ping: true
  1. 設定 文件檔案 模組
    fileProvder := file.New()
    fileProvder.SetConfigName("config.yml") // 手動修改為讀取 config.yml, 預設讀取 app.yml 檔名
    fileProvder.SetConfigType("yaml") // 如果檔案沒有附檔名需要設定,支援 yaml
    err := fileProvder.Load() // 如果目錄下都找不到配置檔,ErrFileNotFound 會被回傳
    if err != nil {
        return err
    }

    config.AddProvider(fileProvider)
  1. 讀取配置內容
    fileProvder := file.New()
    err := fileProvder.Load()
    if err != nil {
        return err
    }
    config.AddProvider(fileProvider)
    appID, err := config.String("app.id") // case casesentive
    fmt.Print(appID) // print: blackbear

    port, err := config.Int32("web.port", 10080) // 設定預設值,如果 "web.port" 這個 key 找不到, 就會回傳 "10080"
    fmt.Print(port) // print: 10080

更新檢查

目前是採用緩存機制,如果已經有內容被讀入就會被緩存已提升後續的效能

RoadMap

  1. File Watch
  2. 配置檔的繼承模式
  3. integrate remote config system

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrFileNotFound         = errors.New("config: config file was not found")
	ErrKeyNotFound          = errors.New("config: key was not found")
	ErrProviderNotFound     = errors.New("config: no provider is added to config.  Provider need to be added first")
	ErrConfigTypeNotSupport = errors.New("config: type is not support")
)

Functions

func AddProvider

func AddProvider(provider Provider)

func Bool

func Bool(key string, defaultValue ...bool) (bool, error)

Bool returns a boolean type value which has the key.

func Duration

func Duration(key string, defaultValue ...time.Duration) (time.Duration, error)

Bool returns a boolean type value which has the key.

func Float32

func Float32(key string, defaultValue ...float32) (float32, error)

Float32 returns a float32 type value which has the key.

func Float64

func Float64(key string, defaultValue ...float64) (float64, error)

Float64 returns a float64 type value which has the key.

func Float64Slice

func Float64Slice(key string, defaultValue ...[]float64) ([]float64, error)

Float64Slice returns the value associated with the key as a slice of float.

func Int

func Int(key string, defaultValue ...int) (int, error)

Int returns a int type value which has the key.

func Int32

func Int32(key string, defaultValue ...int32) (int32, error)

Int32 returns a int32 type value which has the key.

func Int64

func Int64(key string, defaultValue ...int64) (int64, error)

Int64 returns a int64 type value which has the key.

func IntSlice

func IntSlice(key string, defaultValue ...[]int) ([]int, error)

IntSlice returns the value associated with the key as a slice of int.

func OnChangedEvent

func OnChangedEvent(event ChangedEvent)

func RemoveAllPrividers

func RemoveAllPrividers()

func Scan

func Scan(key string, value interface{}) error

Scan binds a value which has the key.

func String

func String(key string, defaultValue ...string) (string, error)

String returns a string type value which has the key.

func StringMap

func StringMap(key string, defaultValue ...map[string]interface{}) (map[string]interface{}, error)

StringMap returns a map[string]interface{} type value which has the key.

func StringMapString

func StringMapString(key string, defaultValue ...map[string]string) (map[string]string, error)

StringMapString returns a map[string]string type value which has the key.

func StringSlice

func StringSlice(key string, defaultValue ...[]string) ([]string, error)

StringSlice returns the value associated with the key as a slice of strings.

Types

type ChangedEvent

type ChangedEvent func() error

type Configuration

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

type Provider

type Provider interface {
	NotifyChange() chan bool
	Get(key string) (interface{}, error)
}

Directories

Path Synopsis
provider
env

Jump to

Keyboard shortcuts

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