config

package
v0.0.0-...-218d4e6 Latest Latest
Warning

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

Go to latest
Published: May 6, 2023 License: MIT Imports: 16 Imported by: 0

README

How to use

  1. Define a config structure, like below:
type RestfulConf struct {
  ServiceName  string        `json:",env=SERVICE_NAME"`  // read from env automatically
	Host         string        `json:",default=0.0.0.0"`
	Port         int
	LogMode      string        `json:",options=[file,console]"`
	Verbose      bool          `json:",optional"`
	MaxConns     int           `json:",default=10000"`
	MaxBytes     int64         `json:",default=1048576"`
	Timeout      time.Duration `json:",default=3s"`
	CpuThreshold int64         `json:",default=900,range=[0:1000]"`
}
  1. Write the yaml, toml or json config file:
  • yaml example
# most fields are optional or have default values
port: 8080
logMode: console
# you can use env settings
maxBytes: ${MAX_BYTES}
  • toml example
# most fields are optional or have default values
port = 8_080
logMode = "console"
# you can use env settings
maxBytes = "${MAX_BYTES}"
  1. Load the config from a file:
// exit on error
var config RestfulConf
conf.MustLoad(configFile, &config)

// or handle the error on your own
var config RestfulConf
if err := conf.Load(configFile, &config); err != nil {
  log.Fatal(err)
}

// enable reading from environments
var config RestfulConf
conf.MustLoad(configFile, &config, conf.UseEnv())

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load(file string, v any, opts ...Option) error

Load loads config into v from file, .json, .yaml and .yml are acceptable.

func LoadConfig

func LoadConfig(file string, v any, opts ...Option) error

LoadConfig loads config into v from file, .json, .yaml and .yml are acceptable. Deprecated: use Load instead.

func LoadConfigFromJsonBytes

func LoadConfigFromJsonBytes(content []byte, v any) error

LoadConfigFromJsonBytes loads config into v from content json bytes. Deprecated: use LoadFromJsonBytes instead.

func LoadConfigFromYamlBytes

func LoadConfigFromYamlBytes(content []byte, v any) error

LoadConfigFromYamlBytes loads config into v from content yaml bytes. Deprecated: use LoadFromYamlBytes instead.

func LoadFromJsonBytes

func LoadFromJsonBytes(content []byte, v any) error

LoadFromJsonBytes loads config into v from content json bytes.

func LoadFromTomlBytes

func LoadFromTomlBytes(content []byte, v any) error

LoadFromTomlBytes loads config into v from content toml bytes.

func LoadFromYamlBytes

func LoadFromYamlBytes(content []byte, v any) error

LoadFromYamlBytes loads config into v from content yaml bytes.

func MustLoad

func MustLoad(path string, v any, opts ...Option)

MustLoad loads config into v from path, exits on error.

Types

type DatabaseConf

type DatabaseConf struct {
	Host         string
	Port         int
	Username     string `json:",optional"`
	Password     string `json:",optional"`
	DBName       string `json:",optional"`
	SSLMode      string `json:",optional"`
	Type         string `json:",default=mysql,options=[mysql,postgres]"`
	MaxOpenConns *int   `json:",optional,default=100"`
	Debug        bool   `json:",optional,default=false"`
	CacheTime    int    `json:",optional,default=10"`
}

func (DatabaseConf) GetDSN

func (c DatabaseConf) GetDSN() string

func (DatabaseConf) MysqlDSN

func (c DatabaseConf) MysqlDSN() string

func (DatabaseConf) NewDataBaseDriver

func (c DatabaseConf) NewDataBaseDriver() *storage.MySQL

func (DatabaseConf) PostgresDSN

func (c DatabaseConf) PostgresDSN() string

type Option

type Option func(opt *options)

Option defines the method to customize the config options.

func UseEnv

func UseEnv() Option

UseEnv customizes the config to use environment variables.

type Properties

type Properties interface {
	GetString(key string) string
	SetString(key, value string)
	GetInt(key string) int
	SetInt(key string, value int)
	ToString() string
}

Properties interface provides the means to access configuration.

func LoadProperties

func LoadProperties(filename string, opts ...Option) (Properties, error)

LoadProperties loads the properties into a properties configuration instance. Returns an error that indicates if there was a problem loading the configuration.

func NewProperties

func NewProperties() Properties

NewProperties builds a new properties configuration structure.

type PropertyError

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

PropertyError represents a configuration error message.

func (*PropertyError) Error

func (configError *PropertyError) Error() string

Error returns the error message.

type ServerConf

type ServerConf struct {
	Name    string
	Host    string
	Port    int
	Network string `json:",optional,default=tcp"`
	Debug   bool   `json:",optional,default=false"`
}

func (ServerConf) GetServerAddr

func (c ServerConf) GetServerAddr() string

Jump to

Keyboard shortcuts

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