country-geo-locations/internal/cmd/cli.go
Tom Neuber 8b7f45563a
All checks were successful
ci/woodpecker/push/lint Pipeline was successful
ci/woodpecker/push/test Pipeline was successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/deploy Pipeline was successful
chore(exporter): rework exporter to fix wrong histogram usage and cache metric data
2025-01-10 14:35:08 +01:00

81 lines
2.7 KiB
Go

package cmd
import (
"errors"
"time"
"github.com/alecthomas/kong"
)
var ErrInvalidDataURL = errors.New("invalid data url, must not be blank")
type AppSettings struct {
ServerAddress string
DataFile string
DataURL string
CacheTTL time.Duration
ReadHeaderTimeout time.Duration
EnableExporter bool
ExporterAddress string
ExporterInterval time.Duration
}
//nolint:lll // ignore line length
type CLI struct {
ServerAddress string `name:"listen-address" env:"GEOIP_LISTEN_ADDRESS" help:"Address to use for the api server" default:"${default_address}"`
DataFile string `name:"data-file" env:"GEOIP_DATA_FILE" help:"path to data file" default:"${default_file_path}"`
DataURL string `name:"data-url" env:"GEOIP_DATA_URL" help:"url to data file"`
CacheTTL string `name:"cache-ttl" env:"GEOIP_CACHE_TTL" help:"ttl for response cache" default:"${default_cache_ttl}"`
ReadHeaderTimeout string `name:"read-header-timeout" env:"GEOIP_READ_HEADER_TIMEOUT" help:"timeout for reading http header" default:"${default_read_header_timeout}"`
EnableExporter bool `name:"enable-exporter" env:"GEOIP_ENABLE_EXPORTER" help:"enable prometheus exporter" default:"${default_enable_exporter}"`
ExporterAddress string `name:"exporter-address" env:"GEOIP_EXPORTER_ADDRESS" help:"Address to use for the prometheus metrics server" default:"${default_exporter_address}"`
ExporterInterval string `name:"exporter-interval" env:"GEOIP_EXPORTER_INTERVAL" help:"Interval to scrape the new metrics data" default:"${default_exporter_interval}"`
}
func (c *CLI) Parse() (*AppSettings, error) {
_ = kong.Parse(
c,
kong.Vars{
"default_address": ":8080",
"default_file_path": "./data.csv",
"default_cache_ttl": "2m",
"default_read_header_timeout": "3s",
"default_enable_exporter": "false",
"default_exporter_address": ":9191",
"default_exporter_interval": "5s",
},
kong.Name("country-geo-locations"),
kong.Description("🚀 Start a simple web server for GeoIP data"),
kong.UsageOnError(),
)
cacheTTL, err := time.ParseDuration(c.CacheTTL)
if err != nil {
return nil, err
}
readHeaderTimeout, err := time.ParseDuration(c.ReadHeaderTimeout)
if err != nil {
return nil, err
}
exporterInterval, err := time.ParseDuration(c.ExporterInterval)
if err != nil {
return nil, err
}
if c.DataURL == "" {
return nil, ErrInvalidDataURL
}
return &AppSettings{
ServerAddress: c.ServerAddress,
DataFile: c.DataFile,
DataURL: c.DataURL,
CacheTTL: cacheTTL,
ReadHeaderTimeout: readHeaderTimeout,
EnableExporter: c.EnableExporter,
ExporterAddress: c.ExporterAddress,
ExporterInterval: exporterInterval,
}, nil
}