country-geo-locations/cfg/cfg.go
Tom Neuber 8215e1f13a
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
refactor: restructure project
2024-11-26 13:35:54 +01:00

64 lines
1.8 KiB
Go

package cfg
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
}
//nolint:lll // ignore line length
type CLI struct {
ServerAddress string `name:"listen-address" env:"GEOIP_LISTEN_ADDRESS" help:"Address to use for the metrics 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}"`
}
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",
},
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
}
if c.DataURL == "" {
return nil, ErrInvalidDataURL
}
return &AppSettings{
ServerAddress: c.ServerAddress,
DataFile: c.DataFile,
DataURL: c.DataURL,
CacheTTL: cacheTTL,
ReadHeaderTimeout: readHeaderTimeout,
}, nil
}