generated from rmcguire/go-server-with-otel
57 lines
2.2 KiB
Go
57 lines
2.2 KiB
Go
// Package config contains ServiceConfig, which is intended
|
|
// to extend go-app/pkg/config.AppConfig. Add any custom fields
|
|
// here, and it will automatically be handled by go-app, including
|
|
// overrides by environment, and json schema generation
|
|
package config
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/caarlos0/env/v11"
|
|
|
|
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/config"
|
|
)
|
|
|
|
// DefaultUsageInterval is used when UsageInterval is unset. Rheem's
|
|
// energy/water history only updates hourly, so polling faster is wasteful.
|
|
const DefaultUsageInterval = 5 * time.Minute
|
|
|
|
type ServiceConfig struct {
|
|
// Rheem EcoNet cloud credentials. Prefer supplying the password via
|
|
// the ECONET_PASSWORD environment variable rather than config.yaml.
|
|
EconetEmail string `yaml:"econetEmail" json:"econetEmail,omitempty" env:"ECONET_EMAIL"`
|
|
EconetPassword string `yaml:"econetPassword" json:"econetPassword,omitempty" env:"ECONET_PASSWORD"`
|
|
|
|
// CostPerKWH is the electricity price in US dollars per kWh (e.g. 0.18
|
|
// means 18 cents/kWh, NOT 18). It derives the econet_energy_cost_dollars
|
|
// metric from kWh energy usage.
|
|
CostPerKWH float64 `yaml:"costPerKWH" json:"costPerKWH,omitempty" env:"ECONET_COST_PER_KWH"`
|
|
|
|
// UsageInterval controls how often energy/water usage history is polled.
|
|
UsageInterval time.Duration `yaml:"usageInterval" json:"usageInterval,omitempty" env:"ECONET_USAGE_INTERVAL"`
|
|
|
|
// Embeds go-app config, used by go-app to
|
|
// merge custom config into go-app config, and to produce
|
|
// a complete configuration json schema
|
|
*config.AppConfig
|
|
}
|
|
|
|
// LoadEnv applies environment-variable overrides to the custom ServiceConfig
|
|
// fields. go-app's MustLoadConfigInto only handles env for the embedded
|
|
// AppConfig, so the caller must apply env for custom fields. The embedded
|
|
// AppConfig is skipped here to preserve go-app's own env/yaml precedence.
|
|
func (c *ServiceConfig) LoadEnv() error {
|
|
appConfig := c.AppConfig
|
|
c.AppConfig = nil
|
|
defer func() { c.AppConfig = appConfig }()
|
|
return env.Parse(c)
|
|
}
|
|
|
|
// GetUsageInterval returns the configured usage poll interval or the default.
|
|
func (c *ServiceConfig) GetUsageInterval() time.Duration {
|
|
if c.UsageInterval <= 0 {
|
|
return DefaultUsageInterval
|
|
}
|
|
return c.UsageInterval
|
|
}
|