Help us shape the future of Go! Take the 2026 Go Developer Survey

config

package
v0.0.0-...-b1aaf46 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2026 License: AGPL-3.0 Imports: 13 Imported by: 0

Documentation

Overview

Package config provides a global config store that other packages can utilise for fetching/storing configuration. The config store supports both file and environment configuration.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ConfigEnvPrefix defines the environment variable prefix for reading server configuration from the environment.
	// This can be overridden to provide a customised default environment prefix for an app.
	ConfigEnvPrefix = "APP_"
	// ErrLoadConfig is the sentinel error returned when there was an issue loading the config.
	ErrLoadConfig = errors.New("error loading config")
	// ErrInvalidConfig is the sentinel error returned when the config was loaded but contains invalid values.
	ErrInvalidConfig = errors.New("invalid config")
)
View Source
var LoadAppConfig = sync.OnceValues(func() (*AppConfig, error) {
	cfg := &AppConfig{
		Environment: EnvDevelopment,
		Version:     "_UNKNOWN_",
	}
	var vcsRevision string
	// var vcsTime string
	var vcsModified bool
	var vcsSystem string
	if info, ok := debug.ReadBuildInfo(); ok {
		for buildInfo := range slices.Values(info.Settings) {
			switch buildInfo.Key {
			case "vcs":
				vcsSystem = buildInfo.Value
			case "vcs.revision":
				vcsRevision = buildInfo.Value

			case "vcs.modified":
				vcsModified = buildInfo.Value == "true"
			}
		}
		cfg.Version = strings.Join([]string{vcsSystem, vcsRevision}, "-")
		if vcsModified {
			cfg.Version += "-dirty"
		}
	}

	if err := Load(ConfigEnvPrefix, cfg); err != nil {
		return nil, fmt.Errorf("load base config: %w", err)
	}

	if err := validation.Validate.Struct(cfg); err != nil {
		return nil, fmt.Errorf("validate base config: %w", err)
	}

	return cfg, nil
})

Functions

func Load

func Load[T any](envPrefix string, cfg T) error

Load will load a config via environment variables with the given prefix into an object of the given type.

Types

type AppConfig

type AppConfig struct {
	// AppName is the application name.
	AppName string `koanf:"name" validate:"required"`
	// AppID is the application name formatted for use as an ID.
	AppID string `koanf:"id" validate:"required"`
	// Version is the application/stack version.
	Version string `koanf:"version"`
	// CurrentEnvironment is the environment in which the app is running (i.e., production, development). Defaults to
	// "development".
	Environment Environment `koanf:"environment" validate:"required,oneof=production development"`
	// BaseURL is the base url from which the app is being served.
	BaseURL string `koanf:"baseurl" validate:"required,url"`
}

func (AppConfig) GetAppEnvironment

func (c AppConfig) GetAppEnvironment() Environment

func (AppConfig) GetAppID

func (c AppConfig) GetAppID() string

func (AppConfig) GetAppName

func (c AppConfig) GetAppName() string

func (AppConfig) GetAppVersion

func (c AppConfig) GetAppVersion() string

func (AppConfig) GetBaseURL

func (c AppConfig) GetBaseURL() *url.URL

func (AppConfig) IsProduction

func (c AppConfig) IsProduction() bool

type ContainerRuntime

type ContainerRuntime string

ContainerRuntime identifies the detected container environment.

const (
	RuntimeNone     ContainerRuntime = "none"
	RuntimeDocker   ContainerRuntime = "docker"
	RuntimePodman   ContainerRuntime = "podman"
	RuntimeCloudRun ContainerRuntime = "cloudrun"
	RuntimeUnknown  ContainerRuntime = "unknown-container"
)

func DetectContainerRuntime

func DetectContainerRuntime() ContainerRuntime

DetectContainerRuntime tries to identify the container runtime we're executing under, checking the most specific/reliable signals first.

type Duration

type Duration struct {
	time.Duration
}

func NewDuration

func NewDuration(duration time.Duration) Duration

func (Duration) MarshalText

func (t Duration) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface. Serializes Duration to a plain byte slice.

func (*Duration) UnmarshalText

func (t *Duration) UnmarshalText(data []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface. Deserializes a plain byte slice into a Duration.

func (Duration) Validate

func (t Duration) Validate() error

type Environment

type Environment string

Environment is the app running environment.

const (
	// EnvDevelopment represents a development environment.
	EnvDevelopment Environment = "development"
	// EnvProduction represents a production environment.
	EnvProduction Environment = "production"
)

func (Environment) String

func (e Environment) String() string

Jump to

Keyboard shortcuts

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