config

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2025 License: Apache-2.0 Imports: 32 Imported by: 2

README

Config

Standardizing environment configuration for GO applications

Uses godotenv to load .env files from Vault into environment variables

Uses envconfig to process environment variables into config structs

Contains helpful structs for common configurations to force consistency

Usage

go get github.com/jesse0michael/pkg/config
import (
    "github.com/jesse0michael/pkg/config"
)

type Config struct {
    Hostname string `envconfig:"HOSTNAME"`
    MySQL    config.MysqlConfig
}

func main() {
    var cfg Config
    if err := config.Process(os.Getenv("ENV_FILES_DIR"), &cfg); err != nil {
        panic(err)
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadEnv

func LoadEnv(envDir string) error

LoadEnv loads environment variables from .env files in the specified directory

func New

func New[T any]() (T, error)

New creates and processes a generic type T with envconfig environment variables

func NewFirestoreClient

func NewFirestoreClient(cfg FirestoreConfig) (*firestore.Client, error)

func NewMysqlClient

func NewMysqlClient(cfg MysqlConfig) (*sql.DB, error)

func NewPostgresClient

func NewPostgresClient(cfg PostgresConfig) (*sqlx.DB, error)

func NewRedisClient

func NewRedisClient(cfg RedisConfig) *redis.Client

func OtelMeterProvider

func OtelMeterProvider(ctx context.Context, cfg OpenTelemetryConfig, r *resource.Resource,
) (*metric.MeterProvider, error)

OtelMeterProvider creates and sets an otel meter provider from an otel config and resource. The purpose of this function is to standardize on the way open telemetry is configured and not have to repeat the same boilerplate code in every service or end up in a situation where every service configures open telemetry differently.

func OtelResource

func OtelResource(ctx context.Context, cfg AppConfig, attributes ...attribute.KeyValue) (*resource.Resource, error)

OtelResource creates a otel resource from an app config. The purpose of this function standardize on the way open telemetry is configured and not have to repeat the same boilerplate code in every service or end up in a situation where every service configures open telemetry differently.

func OtelTraceProvider

func OtelTraceProvider(ctx context.Context, cfg OpenTelemetryConfig, r *resource.Resource,
) (*trace.TracerProvider, error)

OtelTraceProvider creates and sets an otel trace provider from an otel config and resource. The purpose of this function is to standardize on the way open telemetry is configured and not have to repeat the same boilerplate code in every service or end up in a situation where every service configures open telemetry differently.

func Process

func Process(envDir string, cfg interface{}) error

Process loads environment variables from .env files in the specified directory and then processes the environment variables into the provided configuration struct

Types

type AppConfig

type AppConfig struct {
	Environment string `envconfig:"ENVIRONMENT"`
	Name        string `envconfig:"APP_NAME"`
	Version     string `envconfig:"VERSION"`
	LogLevel    string `envconfig:"LOG_LEVEL"`
}

type File

type File []byte

File is a custom envconfig decoder that reads a file from disk using the assigned environment variable as the path to the file

func (*File) Decode

func (f *File) Decode(value string) error

Decode a file using the environment variable value as the path to the file

type FirestoreConfig

type FirestoreConfig struct {
	APIKey string `envconfig:"FIRESTORE_API_KEY"`
}

type MysqlConfig

type MysqlConfig struct {
	Password string `envconfig:"MYSQL_PASSWORD"`
	User     string `envconfig:"MYSQL_USER"     default:"mysql"`
	Port     int    `envconfig:"MYSQL_PORT"     default:"3306"`
	Database string `envconfig:"MYSQL_DB"`
	Host     string `envconfig:"MYSQL_HOST"     default:"localhost"`
}

func (MysqlConfig) ConnectionString

func (m MysqlConfig) ConnectionString() string

type OpenTelemetryConfig

type OpenTelemetryConfig struct {
	OpenTelemetryEndpoint   string  `envconfig:"OTEL_EXPORTER_OTLP_ENDPOINT" default:"localhost:4317"`
	OpenTelemetryInsecure   bool    `envconfig:"OTEL_EXPORTER_OTLP_INSECURE" default:"true"`
	OpenTelemetrySampleRate float64 `envconfig:"OTEL_TRACES_SAMPLER_ARG" default:"1.0"`
}

OpenTelemetryConfig is the configuration for the OpenTelemetry exporters. It uses the environment variables defined by the OpenTelemetry Collector but with different defaults, to make it easier to integrate into services. https://github.com/open-telemetry/opentelemetry-specification/blob/v1.20.0/specification/protocol/exporter.md

func (OpenTelemetryConfig) MetricOptions

func (cfg OpenTelemetryConfig) MetricOptions() []otlpmetricgrpc.Option

func (OpenTelemetryConfig) TracerOptions

func (cfg OpenTelemetryConfig) TracerOptions() []otlptracegrpc.Option

type PostgresConfig

type PostgresConfig struct {
	Password        string        `envconfig:"POSTGRES_PASSWORD"`
	User            string        `envconfig:"POSTGRES_USER"     default:"postgres"`
	Port            int           `envconfig:"POSTGRES_PORT"     default:"5432"`
	Database        string        `envconfig:"POSTGRES_DB"       default:"postgres"`
	Host            string        `envconfig:"POSTGRES_HOST"     default:"localhost"`
	SSLMode         string        `envconfig:"POSTGRES_SSLMODE"  default:"require"`
	MaxConns        int           `envconfig:"POSTGRES_MAX_CONNS"`
	MaxConnDuration time.Duration `envconfig:"POSTGRES_MAX_CONN_DURATION"`
	MaxIdleConns    int           `envconfig:"POSTGRES_MAX_IDLE_CONNS"`
	MaxIdleDuration time.Duration `envconfig:"POSTGRES_MAX_IDLE_DURATION"`
}

func (PostgresConfig) ConnectionString

func (p PostgresConfig) ConnectionString() string

type RSAPublicKey

type RSAPublicKey rsa.PublicKey

RSAPublicKey is a RSA public key PEM file read from the os

func (*RSAPublicKey) Decode

func (k *RSAPublicKey) Decode(value string) error

Decode a RSA public key using the environment variable value as the path to the file

type RedisConfig

type RedisConfig struct {
	Addr         string        `envconfig:"REDIS_ADDR" default:"localhost:6379"`
	Password     string        `envconfig:"REDIS_PASSWORD"`
	DB           int           `envconfig:"REDIS_DB"`
	TLS          bool          `envconfig:"REDIS_TLS"`
	ReadTimeout  time.Duration `envconfig:"REDIS_READ_TIMEOUT" default:"1s"`
	WriteTimeout time.Duration `envconfig:"REDIS_WRITE_TIMEOUT" default:"5s"`
}

Jump to

Keyboard shortcuts

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