config

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2021 License: MIT Imports: 16 Imported by: 0

README

Config

This will configures configurator via environment variables and load config.*.yml files into a package level struct variable. Once loaded, you can use packet level helper methods to retrieve config data.

Usage

Customize Configurator at runtime with Environment Variables

Environment Variables
export CONFIG_FILES=/config/config.yml
# (or) export CONFIG_FILES=/config/config.yml,/config/config.pg.yml
export CONFIG_DEBUG_MODE=true
export CONFIG_VERBOSE_MODE=true
export CONFIG_SILENT_MODE=true
export CONFIG_USE_PKGER=true
export CONFIG_ENV=prod

export CONFIG_ENV_PREFIX=APP
export APP_FEATURES_TLS_ENABLED=true

# for example
CONFIG_SERVICES_GREETER_ENDPOINT=dns:///localhost:8088 ./build/greeter-service
CONFIG_ENV_PREFIX=APP APP_SERVICES_GREETER_ENDPOINT=dns:///localhost:8088 ./build/greeter-service
CONFIG_ENV_PREFIX=APP APP_FEATURES_TLS_ENABLED=true ./build/greeter-service
CONFIG_ENV=prod ./build/greeter-service
Examples

Import shared/config package. It will be self-initialized.

import  "github.com/xmlking/grpc-starter-kit/internal/config"

Once config is initialized, then you can use github.com/xmlking/grpc-starter-kit/internal/config package's helper methods retrieve config items.

import (
    _ "github.com/xmlking/grpc-starter-kit/internal/config"
)

func ExampleGetConfig_check_defaults() {
	fmt.Println(config.GetConfig().Services.Account.Endpoint)
	fmt.Println(config.GetConfig().Services.Account.Version)
	fmt.Println(config.GetConfig().Services.Account.Deadline)

	// Output:
	// dns:///account.test:8080
	// v0.1.0
	// 8888
}

You can also use Configor to load any yaml files into your Struct.

import (
	"github.com/xmlking/grpc-starter-kit/internal/config"
)

func TestOverwriteConfigurationWithEnvironmentWithDefaultPrefix(t *testing.T) {
	os.Setenv("CONFIG_SERVICES_ACCOUNT_ENDPOINT", "dns:///localhost:8088")
	defer os.Setenv("CONFIG_SERVICES_ACCOUNT_ENDPOINT", "")

	var cfg config.Configuration
	config.Configor.Load(&cfg, "/config/config.yml")

	t.Logf("Environment: %s", config.Configor.GetEnvironment())
	t.Log(cfg.Services.Account)
	if cfg.Services.Account.Endpoint != "dns:///localhost:8088" {
		t.Errorf("Account Endpoint is %s, want %s", cfg.Services.Account.Endpoint, "dns:///localhost:8088")
	}
}

Test

CONFIG_DEBUG_MODE=true go test -v ./internal/config/... -count=1

Documentation

Overview

config package from micro-strter-kit import myConfig "github.com/xmlking/grpc-starter-kit/internal/config" to `main.go` and all test files. then use Helper Methods `config.GetServiceConfig()` anywhere you need to retrieve `Configuration`

Index

Examples

Constants

This section is empty.

Variables

View Source
var (

	// Version is populated by govvv in compile-time.
	Version = "untouched"
	// BuildDate is populated by govvv.
	BuildDate string
	// GitCommit is populated by govvv.
	GitCommit string
	// GitBranch is populated by govvv.
	GitBranch string
	// GitState is populated by govvv.
	GitState string
	// GitSummary is populated by govvv.
	GitSummary string
)

Functions

func GetBuildInfo

func GetBuildInfo() string

func GetClientConn

func GetClientConn(service *Service, ucInterceptors []grpc.UnaryClientInterceptor) (clientConn *grpc.ClientConn, err error)

func IsProduction

func IsProduction() bool

func IsSecure

func IsSecure() bool

Types

type Configuration

type Configuration struct {
	Database *DatabaseConfiguration `yaml:"database,omitempty"`
	Email    *EmailConfiguration    `yaml:"email,omitempty"`
	Features *Features              `yaml:"features,omitempty"`
	Services *Services              `yaml:"services,omitempty"`
}

func GetConfig

func GetConfig() Configuration
Example
package main

import (
	"fmt"

	"github.com/xmlking/grpc-starter-kit/internal/config"
)

func main() {
	fmt.Println(config.GetConfig().Email)
	// fmt.Println(config.GetConfig().Services.Account.Authority)

}
Output:

&{yourGmailUsername yourGmailAppPassword smtp.gmail.com 587 from-test@gmail.com}
Example (Check_defaults)
package main

import (
	"fmt"

	"github.com/xmlking/grpc-starter-kit/internal/config"
)

func main() {
	fmt.Println(config.GetConfig().Services.Account.Endpoint)
	fmt.Println(config.GetConfig().Services.Account.Version)
	fmt.Println(config.GetConfig().Services.Account.Authority)

}
Output:

dns:///account.test:8080
v0.1.0
aaa.bbb.ccc

type DatabaseConfiguration

type DatabaseConfiguration struct {
	Dialect         string         `yaml:",omitempty" valid:"in(mysql|sqlite3|postgres|gremlin)" default:"sqlite3"`
	Host            string         `yaml:",omitempty" valid:"host"`
	Port            uint32         `yaml:",omitempty" default:"5432" valid:"port"`
	Username        string         `yaml:"username,omitempty" valid:"alphanum,required"`
	Password        string         `yaml:"password,omitempty" valid:"alphanum,required"`
	Database        string         `yaml:"database,omitempty" valid:"type(string),required"`
	Charset         string         `yaml:",omitempty" default:"utf8"`
	Utc             bool           `yaml:",omitempty" default:"true"`
	Logging         bool           `yaml:",omitempty" default:"false"`
	Singularize     bool           `yaml:",omitempty" default:"false"`
	MaxOpenConns    int            `yaml:"max_open_conns,omitempty" default:"100"`
	MaxIdleConns    int            `yaml:"max_idle_conns,omitempty" default:"10"`
	ConnMaxLifetime *time.Duration `yaml:"conn_max_lifetime,omitempty" default:"1h"`
}

func (*DatabaseConfiguration) URL

func (d *DatabaseConfiguration) URL() (url string, err error)

URL returns a connection string for the database.

type EmailConfiguration

type EmailConfiguration struct {
	Username    string `yaml:"username"`
	Password    string `yaml:",omitempty"`
	EmailServer string `yaml:"email_server,omitempty"`
	Port        uint32 `yaml:",omitempty" default:"587" valid:"port"`
	From        string `yaml:",omitempty" valid:"email,optional"`
}

type Features

type Features struct {
	Metrics   *Features_Metrics   `yaml:"metrics,omitempty"`
	Tracing   *Features_Tracing   `yaml:"tracing,omitempty"`
	Tls       *Features_TLS       `yaml:"tls,omitempty"`
	Validator *Features_Validator `yaml:"validator,omitempty"`
	Rpclog    *Features_Rpclog    `yaml:"rpclog,omitempty"`
	Translog  *Features_Translog  `yaml:"translog,omitempty"`
}

type Features_Metrics

type Features_Metrics struct {
	Enabled       bool   `yaml:",omitempty" default:"false"`
	Address       string `yaml:"address,omitempty"`
	FlushInterval uint64 `yaml:"flush_interval,omitempty" default:"10000000"`
}

type Features_Rpclog

type Features_Rpclog struct {
	Enabled bool `yaml:",omitempty" default:"false"`
}

type Features_TLS

type Features_TLS struct {
	Enabled    bool   `yaml:",omitempty" default:"false"`
	CertFile   string `yaml:"cert_file" valid:"type(string),required"`
	KeyFile    string `yaml:"key_file" valid:"type(string),required"`
	CaFile     string `yaml:"ca_file" valid:"type(string),required"`
	Password   string `yaml:"password,omitempty"`
	ServerName string `yaml:"server_name,omitempty" default:"'*'"`
}

type Features_Tracing

type Features_Tracing struct {
	Enabled       bool    `yaml:",omitempty" default:"false"`
	Address       string  `yaml:"address,omitempty"`
	Sampling      float64 `yaml:"sampling,omitempty" default:"0.5"`
	FlushInterval uint64  `yaml:"flush_interval,omitempty" default:"10000000"`
}

type Features_Translog

type Features_Translog struct {
	Enabled bool   `yaml:",omitempty" default:"false"`
	Topic   string `yaml:",omitempty"`
}

type Features_Validator

type Features_Validator struct {
	Enabled bool `yaml:",omitempty" default:"false"`
}

type Service

type Service struct {
	Endpoint      string            `yaml:"endpoint" required:"true"`
	Version       string            `yaml:",omitempty" default:"v0.1.0"`
	Metadata      map[string]string `yaml:",omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`
	ServiceConfig string            `yaml:"service_config,omitempty"`
	Authority     string            `yaml:",omitempty"`
}

type Services

type Services struct {
	Account  *Service `yaml:"account,omitempty"`
	Greeter  *Service `yaml:"greeter,omitempty"`
	Emailer  *Service `yaml:"emailer,omitempty"`
	Recorder *Service `yaml:"recorder,omitempty"`
	Play     *Service `yaml:"play,omitempty"`
}

Jump to

Keyboard shortcuts

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