remoteconfig

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

README

go-remote-config

godoc Circle CI

A Go library for configuration management with JSON files in remote storage.

Install

go get github.com/zencoder/go-remote-config

Supported Storage Providers

  • AWS S3 (Signed URLs)
  • HTTP/HTTPS

Features

  • Reflection based config validation
    • Required fields
    • Optional fields
    • Custom Validate interface
    • Empty string checks
    • Struct & Slice, nested support
  • Built in config structs for services
    • AWS Regions
    • AWS DynamoDB (Client + Table)
    • AWS SQS (Client + Queue)
    • AWS S3
    • Generic HTTP Endpoints

Future Features

  • More storage provider support
    • Google Cloud Storage
    • Rackspace CloudFiles
  • Default value support
  • Config download retry support
  • Live config reloading

Example

type SampleConfig struct {
	SQSQueueOptional           *SQSQueueConfig       `json:"sqs_queue_optional,omitempty" remoteconfig:"optional"`
	SQSClientOptional          *SQSClientConfig      `json:"sqs_client_optional,omitempty" remoteconfig:"optional"`
	DynamoDBTableOptional      *DynamoDBTableConfig  `json:"dynamodb_table_optional,omitempty" remoteconfig:"optional"`
	DynamoDBClientOptional     *DynamoDBClientConfig `json:"dynamodb_client_optional,omitempty" remoteconfig:"optional"`
	StrOptional                *string               `json:"str_optional,omitempty" remoteconfig:"optional"`
	StorageConfigOptional      *StorageConfig        `json:"storage_config_optional,omitempty" remoteconfig:"optional"`
	StorageConfigSliceOptional []*StorageConfig      `json:"storage_config_slice_optional,omitempty" remoteconfig:"optional"`
	SQSQueue                   *SQSQueueConfig       `json:"sqs_queue,omitempty"`
	SQSClient                  *SQSClientConfig      `json:"sqs_client,omitempty"`
	DynamoDBTable              *DynamoDBTableConfig  `json:"dynamodb_table,omitempty"`
	DynamoDBClient             *DynamoDBClientConfig `json:"dynamodb_client,omitempty"`
	Str                        *string               `json:"str,omitempty"`
	StorageConfig              *StorageConfig        `json:"storage_config,omitempty"`
	StorageConfigSlice         []*StorageConfig      `json:"storage_config_slice,omitempty"`
}

var s SampleConfig
LoadConfig(s)

import (
	"log"
	"os"

	"github.com/zencoder/go-remote-config"
)

func LoadConfig(config interface{}) {
	// Load the config from S3
	configURL := os.Getenv("S3_CONFIG_URL")
	configRegion := remoteconfig.AWSRegion(os.Getenv("S3_CONFIG_REGION"))

	// Load an endpoint for S3 config (can be used to fake out S3 for testing)
	configEndpoint := os.Getenv("S3_CONFIG_ENDPOINT")

	// We should fail out if config environment variables are not set / valid
	if configURL == "" {
		log.Panic("S3 Configuration URL must be provided.")
	}

	if err := configRegion.Validate(); err != nil {
		log.Panic("Invalid Region for S3 Configuration")
	}

	log.Printf("Loading config file from S3. URL = %s, Region = %s", configURL, configRegion)

	if err := remoteconfig.LoadConfigFromS3(configURL, configRegion, configEndpoint, config); err != nil {
		log.Panicf("Failed to load config file, with error: %s", err.Error())
	}

	log.Printf("Successfully loaded config file from S3. URL = %s, Region = %s", configURL, configRegion)
	log.Printf("%s", config)

}

Development

Build and run unit tests
make test
CI

This library builds on Circle CI, here.

License

Apache License Version 2.0

Documentation

Index

Constants

View Source
const (
	DEFAULT_S3_EXPIRY   uint   = 60
	DEFAULT_S3_ENDPOINT string = ""
)
View Source
const (
	S3_CONFIG_DEFAULT_EXPIRY uint = 60
)
View Source
const (
	S3_ENDPOINT_EXPIRY_CONFIG_DEFAULT_EXPIRY uint = 60
)

Variables

View Source
var (
	ErrAWSRegionEmptyString = errors.New("Region cannot be empty")
)

Functions

func LoadConfigFromURL

func LoadConfigFromURL(configURL string, configStruct interface{}) error

Downloads a configuration JSON file from S3. Parses it to a particular struct type and runs a validation. URL should be of the format s3://bucket/path/file.json

func ReadJSONValidate

func ReadJSONValidate(cfgReader io.Reader, configStruct interface{}) error

Downloads JSON from a URL, decodes it and then validates.

Types

type APIEndpointConfig

type APIEndpointConfig struct {
	BasePath *string `json:"base_path,omitempty"`
	SubPath  *string `json:"sub_path,omitempty"`
}

func (APIEndpointConfig) GetFullPath

func (c APIEndpointConfig) GetFullPath() string

func (APIEndpointConfig) GetFullPathWithID

func (c APIEndpointConfig) GetFullPathWithID(id string) string

type AWSRegion

type AWSRegion string
const (
	AWS_REGION_US_EAST_1      AWSRegion = "us-east-1"
	AWS_REGION_US_WEST_1      AWSRegion = "us-west-1"
	AWS_REGION_US_WEST_2      AWSRegion = "us-west-2"
	AWS_REGION_US_GOV_WEST_1  AWSRegion = "us-gov-west-1"
	AWS_REGION_EU_WEST_1      AWSRegion = "eu-west-1"
	AWS_REGION_EU_CENTRAL_1   AWSRegion = "eu-central-1"
	AWS_REGION_AP_SOUTHEAST_1 AWSRegion = "ap-southeast-1"
	AWS_REGION_AP_SOUTHEAST_2 AWSRegion = "ap-southeast-2"
	AWS_REGION_AP_NORTHEAST_1 AWSRegion = "ap-northeast-1"
	AWS_REGION_SA_EAST_1      AWSRegion = "sa-east-1"
)

func (*AWSRegion) UnmarshalText

func (r *AWSRegion) UnmarshalText(data []byte) error

func (AWSRegion) Validate

func (r AWSRegion) Validate() error

type DynamoDBClientConfig

type DynamoDBClientConfig struct {
	Region     *AWSRegion `json:"region,omitempty"`
	Endpoint   *string    `json:"endpoint,omitempty" remoteconfig:"optional"`
	DisableSSL *bool      `json:"disable_ssl,omit" remoteconfig:"optional"`
}

func (DynamoDBClientConfig) GetDisableSSL

func (d DynamoDBClientConfig) GetDisableSSL() bool

func (DynamoDBClientConfig) GetEndpoint

func (d DynamoDBClientConfig) GetEndpoint() string

func (DynamoDBClientConfig) GetRegion

func (d DynamoDBClientConfig) GetRegion() AWSRegion

type DynamoDBTableConfig

type DynamoDBTableConfig struct {
	TableName *string `mapstructure:"table_name" json:"table_name,omitempty"`
}

func (DynamoDBTableConfig) GetTableName

func (d DynamoDBTableConfig) GetTableName() string

type S3Config

type S3Config struct {
	Endpoint *string    `json:"endpoint,omitempty" yaml:"endpoint,omitempty" remoteconfig:"optional"`
	Bucket   *string    `json:"bucket,omitempty" yaml:"bucket,omitempty"`                         // i.e. bucket
	Region   *AWSRegion `json:"region,omitempty" yaml:"region,omitempty"`                         // i.e. us-west-2
	Expiry   *uint      `json:"expiry,omitempty" yaml:"expiry,omitempty" remoteconfig:"optional"` // i.e. 60
}

func S3URLToConfig

func S3URLToConfig(s3URL string) (*S3Config, string, error)

func (S3Config) GetEndpoint

func (c S3Config) GetEndpoint() string

func (S3Config) GetExpiry

func (c S3Config) GetExpiry() uint

type S3EndpointExpiryConfig

type S3EndpointExpiryConfig struct {
	Endpoint *string `json:"endpoint,omitempty" yaml:"endpoint,omitempty" remoteconfig:"optional"`
	Expiry   *uint   `json:"expiry,omitempty" yaml:"expiry,omitempty" remoteconfig:"optional"`
}

func (S3EndpointExpiryConfig) GetEndpoint

func (c S3EndpointExpiryConfig) GetEndpoint() string

func (S3EndpointExpiryConfig) GetExpiry

func (c S3EndpointExpiryConfig) GetExpiry() uint

type SQSClientConfig

type SQSClientConfig struct {
	Region   *AWSRegion `json:"region,omitempty"`
	Endpoint *string    `json:"endpoint,omitempty" remoteconfig:"optional"`
}

func (SQSClientConfig) GetEndpoint

func (s SQSClientConfig) GetEndpoint() string

func (SQSClientConfig) GetRegion

func (s SQSClientConfig) GetRegion() AWSRegion

type SQSQueueConfig

type SQSQueueConfig struct {
	Region       *AWSRegion `json:"region,omitempty"`
	AWSAccountID *string    `json:"aws_account_id,omitempty"`
	QueueName    *string    `json:"queue_name,omitempty"`
}

func (SQSQueueConfig) GetURL

func (s SQSQueueConfig) GetURL(endpoint string) string

Returns a full SQS queue URL.

type StorageConfig

type StorageConfig struct {
	Provider *StorageProvider `json:"provider,omitempty"`
	Location *StorageLocation `json:"location,omitempty"`
}

func (*StorageConfig) GetLocation

func (s *StorageConfig) GetLocation() StorageLocation

func (*StorageConfig) GetProvider

func (s *StorageConfig) GetProvider() StorageProvider

func (StorageConfig) Validate

func (s StorageConfig) Validate() error

type StorageLocation

type StorageLocation string

func (*StorageLocation) UnmarshalText

func (s *StorageLocation) UnmarshalText(data []byte) error

type StorageProvider

type StorageProvider string
const (
	STORAGE_PROVIDER_AWS StorageProvider = "aws"
)

func (*StorageProvider) UnmarshalText

func (s *StorageProvider) UnmarshalText(data []byte) error

func (StorageProvider) Validate

func (s StorageProvider) Validate() error

type Validater

type Validater interface {
	Validate() error
}

Jump to

Keyboard shortcuts

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