2024-11-28 11:52:41 +01:00
package cmd
2023-12-19 13:56:03 +01:00
import (
2024-11-26 13:35:54 +01:00
"errors"
"time"
2023-12-19 13:56:03 +01:00
"github.com/alecthomas/kong"
)
2024-11-26 13:35:54 +01:00
var ErrInvalidDataURL = errors . New ( "invalid data url, must not be blank" )
2023-12-19 13:56:03 +01:00
type AppSettings struct {
2024-11-26 13:35:54 +01:00
ServerAddress string
DataFile string
DataURL string
CacheTTL time . Duration
ReadHeaderTimeout time . Duration
2024-11-28 11:52:41 +01:00
EnableExporter bool
ExporterAddress string
2024-03-02 10:29:59 +01:00
}
2024-11-26 13:35:54 +01:00
//nolint:lll // ignore line length
type CLI struct {
2024-11-28 11:52:41 +01:00
ServerAddress string ` name:"listen-address" env:"GEOIP_LISTEN_ADDRESS" help:"Address to use for the api server" default:"$ { default_address}" `
2024-11-26 13:35:54 +01:00
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}" `
2024-11-28 11:52:41 +01:00
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}" `
2023-12-19 13:56:03 +01:00
}
2024-11-26 13:35:54 +01:00
func ( c * CLI ) Parse ( ) ( * AppSettings , error ) {
_ = kong . Parse (
c ,
2023-12-19 13:56:03 +01:00
kong . Vars {
2024-11-26 13:35:54 +01:00
"default_address" : ":8080" ,
"default_file_path" : "./data.csv" ,
"default_cache_ttl" : "2m" ,
"default_read_header_timeout" : "3s" ,
2024-11-28 11:52:41 +01:00
"default_enable_exporter" : "false" ,
"default_exporter_address" : ":9191" ,
2023-12-19 13:56:03 +01:00
} ,
2024-11-28 11:52:41 +01:00
kong . Name ( "country-geo-locations" ) ,
2023-12-19 13:56:03 +01:00
kong . Description ( "🚀 Start a simple web server for GeoIP data" ) ,
kong . UsageOnError ( ) ,
)
2024-11-26 13:35:54 +01:00
cacheTTL , err := time . ParseDuration ( c . CacheTTL )
2024-03-02 10:29:59 +01:00
if err != nil {
2024-11-26 13:35:54 +01:00
return nil , err
}
readHeaderTimeout , err := time . ParseDuration ( c . ReadHeaderTimeout )
if err != nil {
return nil , err
2023-12-19 13:56:03 +01:00
}
2024-11-26 13:35:54 +01:00
if c . DataURL == "" {
return nil , ErrInvalidDataURL
2023-12-19 13:56:03 +01:00
}
2024-11-26 13:35:54 +01:00
return & AppSettings {
ServerAddress : c . ServerAddress ,
DataFile : c . DataFile ,
DataURL : c . DataURL ,
CacheTTL : cacheTTL ,
ReadHeaderTimeout : readHeaderTimeout ,
2024-11-28 11:52:41 +01:00
EnableExporter : c . EnableExporter ,
ExporterAddress : c . ExporterAddress ,
2024-11-26 13:35:54 +01:00
} , nil
2023-12-19 13:56:03 +01:00
}