config

package
v0.0.0-...-fbee99c Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2016 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package config contains a handful of structs meant for managing common configuration options and credentials. There are currently configs for:

  • MySQL
  • MongoDB
  • Oracle
  • AWS (SNS, SQS, S3, DynamoDB)
  • Kafka
  • Gorilla's `securecookie`
  • Gizmo Servers

The package also has a generic `Config` type that contains all of the above types. It's meant to be a 'catch all' struct that most applications should be able to use.

This package also contains functions to load these config structs from JSON files, JSON blobs in Consul k/v or environment variables.

Index

Constants

View Source
const (
	// AWSRegionUSEast1 is a helper constant for AWS configs.
	AWSRegionUSEast1 = "us-east-1"
	// AWSRegionUSWest is a helper constant for AWS configs.
	AWSRegionUSWest = "us-west-1"
)
View Source
const DefaultConfigLocation = "/opt/nyt/etc/conf.json"

DefaultConfigLocation is the default filepath for JSON config files.

View Source
const DefaultLocation = "America/New_York"

DefaultLocation is used for MySQL connections.

Variables

View Source
var (
	// LogCLI is a pointer to the value of the '-log' command line flag. It is meant to declare
	// an application logging location.
	LogCLI = flag.String("log", "", "Application log location")
	// HTTPAccessLogCLI is a pointer to the value of the '-http-access-log' command line flag. It is meant to
	// declare an access log location for HTTP services.
	HTTPAccessLogCLI = flag.String("http-access-log", "", "HTTP access log location")
	// RPCAccessLogCLI is a pointer to the value of the '-rpc-access-log' command line flag. It is meant to
	// declare an acces log location for RPC services.
	RPCAccessLogCLI = flag.String("rpc-access-log", "", "RPC access log location")
	// ConfigLocationCLI is a pointer to the value of the '-config' command line flag. It is meant to declare
	// the location of a config file. It defaults to `DefaultConfigLocation`.
	ConfigLocationCLI = flag.String("config", DefaultConfigLocation, "Application config file location")
	// HTTPPortCLI is a pointer to the value for the '-http' flag. It is meant to declare the port
	// number to serve HTTP services.
	HTTPPortCLI = flag.Int("http", 0, "Port to run an HTTP server on")
	// RPCPortCLI is a pointer to the value for the '-rpc' flag. It is meant to declare the port
	// number to serve RPC services.
	RPCPortCLI = flag.Int("rpc", 0, "Port to run an RPC server on")
)
View Source
var (
	// MySQLMaxOpenConns will be used to set a MySQL
	// drivers MaxOpenConns value.
	MySQLMaxOpenConns = 1
	// MySQLMaxIdleConns will be used to set a MySQL
	// drivers MaxIdleConns value.
	MySQLMaxIdleConns = 1
)
View Source
var EnvAppName = ""

EnvAppName is used as a prefix for environment variable names when using the LoadXFromEnv funcs. It defaults to empty.

Functions

func LoadAWSFromEnv

func LoadAWSFromEnv() (*AWS, *SNS, *SQS, *S3, *DynamoDB, *ElastiCache)

LoadAWSFromEnv will attempt to load the AWS structs from environment variables. If not populated, nil is returned.

func LoadEnvConfig

func LoadEnvConfig(c interface{})

LoadEnvConfig will use envconfig to load the given config struct from the environment.

func LoadJSONFile

func LoadJSONFile(fileName string, cfg interface{})

LoadJSONFile is a helper function to read a config file into whatever config struct you need. For example, your custom config could be composed of one or more of the given Config, AWS, MySQL, Oracle or MongoDB structs.

func LoadJSONFromConsulKV

func LoadJSONFromConsulKV(configKeyParameter string, cfg interface{}) interface{}

LoadJSONFromConsulKV is a helper function to read a JSON string found in a path defined by configKey inside Consul's Key Value storage then unmarshalled into a config struct, like LoadJSONFile does. It assumes that the Consul agent is running with the default setup, where the HTTP API is found via 127.0.0.1:8500.

func SetLogOverride

func SetLogOverride(log *string)

SetLogOverride will check `*LogCLI` for any values and override the given string pointer if LogCLI is set. If LogCLI is set to "dev", the given log var will be set to "".

func SetServerOverrides

func SetServerOverrides(c *Server)

SetServerOverrides will check the *CLI variables for any values and override the values in the given config if they are set. If LogCLI is set to "dev", the given `Log` pointer will be set to an empty string.

Types

type AWS

type AWS struct {
	SecretKey string `envconfig:"AWS_SECRET_KEY"`
	AccessKey string `envconfig:"AWS_ACCESS_KEY"`

	Region string `envconfig:"AWS_REGION"`
}

AWS holds common AWS credentials and keys.

type Config

type Config struct {
	Server *Server

	AWS         *AWS
	SQS         *SQS
	SNS         *SNS
	S3          *S3
	DynamoDB    *DynamoDB
	ElastiCache *ElastiCache

	Kafka *Kafka

	Oracle *Oracle

	MySQL      *MySQL
	MySQLSlave *MySQL

	MongoDB *MongoDB

	Cookie *Cookie

	GraphiteHost *string `envconfig:"GRAPHITE_HOST"`

	LogLevel *string `envconfig:"APP_LOG_LEVEL"`
	Log      *string `envconfig:"APP_LOG"`
}

Config is a generic struct to hold information for applications that need to connect to databases, handle cookies, log events or emit metrics. If you have a use case that does not fit this struct, you can make a struct containing just the types that suit your needs and use some of the helper functions in this package to load it from the environment.

func LoadConfigFromEnv

func LoadConfigFromEnv() *Config

LoadConfigFromEnv will attempt to inspect the environment of any valid config options and will return a populated Config struct with what it found. If you need a unique config object and want to use envconfig, you will need to run the LoadXXFromEnv for each child struct in your config struct. For an example on how to do this, check out the guts of this function.

func NewConfig

func NewConfig(fileName string) *Config

NewConfig will attempt to unmarshal the contents of the given JSON string source into a Config struct. The value of fileName can be either the path to a JSON file or a path to a JSON string found in Consul's Key Value storage (using the format consul:path/to/JSON/string). If the value of fileName is empty, a blank Config will be returned.

type Cookie struct {
	HashKey  string `envconfig:"COOKIE_HASH_KEY"`
	BlockKey string `envconfig:"COOKIE_BLOCK_KEY"`
	Domain   string `envconfig:"COOKIE_DOMAIN"`
	Name     string `envconfig:"COOKIE_NAME"`
}

Cookie holds information for creating a secure cookie.

func LoadCookieFromEnv

func LoadCookieFromEnv() *Cookie

LoadCookieFromEnv will attempt to load an Cookie object from environment variables. If not populated, nil is returned.

type DynamoDB

type DynamoDB struct {
	AWS
	TableName string `envconfig:"AWS_DYNAMODB_TABLE_NAME"`
}

DynamoDB holds some basic info required to work with Amazon DynamoDB.

type ElastiCache

type ElastiCache struct {
	AWS
	ClusterID string `envconfig:"AWS_ELASTICACHE_CLUSTER_ID"`
}

ElastiCache holds the basic info required to work with Amazon ElastiCache.

func (*ElastiCache) MustClient

func (e *ElastiCache) MustClient() *memcache.Client

MustClient will use the cache cluster ID to describe the cache cluster and instantiate a memcache.Client with the cache nodes returned from AWS.

type Kafka

type Kafka struct {
	BrokerHosts []string
	// BrokerHostsString is used when loading the list from environment variables.
	// If loaded via the LoadEnvConfig() func, BrokerHosts will get updated with these
	// values.
	BrokerHostsString string `envconfig:"KAFKA_BROKER_HOSTS"`

	Partition int32  `envconfig:"KAFKA_PARTITION"`
	Topic     string `envconfig:"KAFKA_TOPIC"`

	MaxRetry int `envconfig:"KAFKA_MAX_RETRY"`
}

Kafka holds the basic information for working with Kafka

func LoadKafkaFromEnv

func LoadKafkaFromEnv() *Kafka

LoadKafkaFromEnv will attempt to load an Kafka object from environment variables. If not populated, nil is returned.

type MongoDB

type MongoDB struct {
	User       string `envconfig:"MONGODB_USER"`
	Pw         string `envconfig:"MONGODB_PW"`
	Hosts      string `envconfig:"MONGODB_HOSTS"`
	MasterHost string `envconfig:"MONGODB_MASTER_HOST_NAME"`
	AuthDB     string `envconfig:"MONGODB_AUTH_DB_NAME"`
	DB         string `envconfig:"MONGODB_DB_NAME"`
}

MongoDB holds the information required for connecting to a MongoDB replicaset.

func LoadMongoDBFromEnv

func LoadMongoDBFromEnv() *MongoDB

LoadMongoDBFromEnv will attempt to load a MongoCreds object from environment variables. If not populated, nil is returned.

func (*MongoDB) Must

func (m *MongoDB) Must() *mgo.Session

Must will attempt to initiate a new mgo.Session with the replicaset and will panic if it encounters any issues.

func (*MongoDB) MustMaster

func (m *MongoDB) MustMaster() *mgo.Session

MustMaster will attempt to initiate a new mgo.Session with the Master host and will panic if it encounters any issues.

type MySQL

type MySQL struct {
	User     string `envconfig:"MYSQL_USER"`
	Pw       string `envconfig:"MYSQL_PW"`
	Host     string `envconfig:"MYSQL_HOST_NAME"`
	DBName   string `envconfig:"MYSQL_DB_NAME"`
	Location string `envconfig:"MYSQL_LOCATION"`
}

MySQL holds everything you need to connect and interact with a MySQL DB.

func LoadMySQLFromEnv

func LoadMySQLFromEnv() *MySQL

LoadMySQLFromEnv will attempt to load a MySQL object from environment variables. If not populated, nil is returned.

func (*MySQL) DB

func (m *MySQL) DB() (*sql.DB, error)

DB will attempt to open a sql connection with the credentials and the current MySQLMaxOpenConns and MySQLMaxIdleConns values. Users must import a mysql driver in their main to use this.

func (*MySQL) String

func (m *MySQL) String() string

String will return the MySQL connection string.

type Oracle

type Oracle struct {
	User          string `envconfig:"ORACLE_USER"`
	Pw            string `envconfig:"ORACLE_PW"`
	Host          string `envconfig:"ORACLE_HOST_NAME"`
	Port          int    `envconfig:"ORACLE_PORT"`
	DBName        string `envconfig:"ORACLE_DB_NAME"`
	ConnectString string `envconfig:"ORACLE_CONNECT_STRING"`
}

Oracle holds everything you need to connect and interact with an Oracle DB.

func LoadOracleFromEnv

func LoadOracleFromEnv() *Oracle

LoadOracleFromEnv will attempt to load an OracleCreds object from environment variables. If not populated, nil is returned.

func (*Oracle) DB

func (o *Oracle) DB() (*sql.DB, error)

DB will attempt to open a sql connection. Users must import an oci8 driver in their main to use this.

func (*Oracle) String

func (o *Oracle) String() string

String will return the Oracle connection string.

type S3

type S3 struct {
	AWS
	Bucket string `envconfig:"AWS_S3_BUCKET_NAME"`
}

S3 holds the info required to work with Amazon S3.

type SNS

type SNS struct {
	AWS
	Topic string `envconfig:"AWS_SNS_TOPIC"`
}

SNS holds the info required to work with Amazon SNS.

type SQS

type SQS struct {
	AWS
	QueueName string `envconfig:"AWS_SQS_NAME"`
	// MaxMessages will override the DefaultSQSMaxMessages.
	MaxMessages *int64 `envconfig:"AWS_SQS_MAX_MESSAGES"`
	// TimeoutSeconds will override the DefaultSQSTimeoutSeconds.
	TimeoutSeconds *int64 `envconfig:"AWS_SQS_TIMEOUT_SECONDS"`
	// SleepInterval will override the DefaultSQSSleepInterval.
	SleepInterval *time.Duration `envconfig:"AWS_SQS_SLEEP_INTERVAL"`
	// DeleteBufferSize will override the DefaultSQSDeleteBufferSize.
	DeleteBufferSize *int `envconfig:"AWS_SQS_DELETE_BUFFER_SIZE"`
	// ConsumeBase64 is a flag to signal the subscriber to base64 decode the payload
	// before returning it. If it is not set in the config, the flag will default
	// to 'true'.
	ConsumeBase64 *bool `envconfig:"AWS_SQS_CONSUME_BASE64"`
}

SQS holds the info required to work with Amazon SQS

type Server

type Server struct {
	// Server will tell the server package which type of server to init. If
	// empty, this will default to 'simple'.
	ServerType string `envconfig:"GIZMO_SERVER_TYPE"`
	// HealthCheckType is used by server to init the proper HealthCheckHandler.
	// If empty, this will default to 'simple'.
	HealthCheckType string `envconfig:"GIZMO_HEALTH_CHECK_TYPE"`
	// HealthCheckPath is used by server to init the proper HealthCheckHandler.
	// If empty, this will default to '/status.txt'.
	HealthCheckPath string `envconfig:"GIZMO_HEALTH_CHECK_PATH"`
	// JSONContentType can be used to override the default JSONContentType.
	JSONContentType *string `envconfig:"GIZMO_JSON_CONTENT_TYPE"`
	//	MaxHeaderBytes can be used to override the default MaxHeaderBytes (1<<20).
	MaxHeaderBytes *int `envconfig:"GIZMO_JSON_CONTENT_TYPE"`
	// GOMAXPROCS can be used to override the default GOMAXPROCS (runtime.NumCPU).
	GOMAXPROCS *int `envconfig:"GIZMO_SERVER_GOMAXPROCS"`
	// HTTPAccessLog is the location of the http access log. If it is empty,
	// no access logging will be done.
	HTTPAccessLog string `envconfig:"HTTP_ACCESS_LOG"`
	// RPCAccessLog is the location of the RPC access log. If it is empty,
	// no access logging will be done.
	RPCAccessLog string `envconfig:"RPC_ACCESS_LOG"`
	// HTTPPort is the port the server implementation will serve HTTP over.
	HTTPPort int `envconfig:"HTTP_PORT"`
	// RPCPort is the port the server implementation will serve RPC over.
	RPCPort int `envconfig:"RPC_PORT"`
	// Log is the path to the application log.
	Log string `envconfig:"APP_LOG"`
	// LogLevel will override the default log level of 'info'.
	LogLevel string `envconfig:"APP_LOG_LEVEL"`
	// Enable pprof Profiling. Off by default.
	EnablePProf bool `envconfig:"ENABLE_PPROF"`
	// GraphiteHost should be the host and port of an available graphite cluster.
	// If not set, the server will not emit metrics.
	GraphiteHost string `envconfig:"GRAPHITE_HOST"`
	// TLSCertFile is an optional string for enabling TLS in simple servers.
	TLSCertFile *string `envconfig:"TLS_CERT"`
	// TLSKeyFile is an optional string for enabling TLS in simple servers.
	TLSKeyFile *string `envconfig:"TLS_KEY"`
	// NotFoundHandler will override the default server NotfoundHandler if set.
	NotFoundHandler http.Handler
}

Server holds info required to configure a gizmo server.Server.

func LoadServerFromEnv

func LoadServerFromEnv() *Server

LoadServerFromEnv will attempt to load a Server object from environment variables. If not populated, nil is returned.

Jump to

Keyboard shortcuts

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