chore: move cfg
package to internal/cmd
This commit is contained in:
parent
8215e1f13a
commit
95a3a16fdc
1 changed files with 11 additions and 3 deletions
72
internal/cmd/cli.go
Normal file
72
internal/cmd/cli.go
Normal file
|
@ -0,0 +1,72 @@
|
|||
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
|
||||
}
|
||||
|
||||
//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}"`
|
||||
}
|
||||
|
||||
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",
|
||||
},
|
||||
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,
|
||||
EnableExporter: c.EnableExporter,
|
||||
ExporterAddress: c.ExporterAddress,
|
||||
}, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue