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 ¶
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) GetAppName ¶
func (AppConfig) GetAppVersion ¶
func (AppConfig) GetBaseURL ¶
func (AppConfig) IsProduction ¶
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 ¶
func NewDuration ¶
func (Duration) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface. Serializes Duration to a plain byte slice.
func (*Duration) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface. Deserializes a plain byte slice into a Duration.
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
Click to show internal directories.
Click to hide internal directories.