refactor: restructure project
This commit is contained in:
parent
1bde2041e1
commit
8215e1f13a
21 changed files with 850 additions and 269 deletions
61
cfg/cfg.go
61
cfg/cfg.go
|
@ -1,45 +1,64 @@
|
|||
package cfg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/alecthomas/kong"
|
||||
)
|
||||
|
||||
var ErrInvalidDataURL = errors.New("invalid data url, must not be blank")
|
||||
|
||||
type AppSettings 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"`
|
||||
ServerAddress string
|
||||
DataFile string
|
||||
DataURL string
|
||||
CacheTTL time.Duration
|
||||
ReadHeaderTimeout time.Duration
|
||||
}
|
||||
|
||||
func NewAppSettings() *AppSettings {
|
||||
return &AppSettings{}
|
||||
//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 (s *AppSettings) Parse() error {
|
||||
ctx := kong.Parse(
|
||||
s,
|
||||
func (c *CLI) Parse() (*AppSettings, error) {
|
||||
_ = kong.Parse(
|
||||
c,
|
||||
kong.Vars{
|
||||
"default_address": ":8080",
|
||||
"default_file_path": "./data.csv",
|
||||
"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(),
|
||||
)
|
||||
|
||||
err := validateFlags(*s)
|
||||
cacheTTL, err := time.ParseDuration(c.CacheTTL)
|
||||
if err != nil {
|
||||
ctx.PrintUsage(false)
|
||||
fmt.Println()
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateFlags(settings AppSettings) error {
|
||||
if settings.DataURL == "" {
|
||||
return fmt.Errorf("error: invalid data url, must not be blank")
|
||||
readHeaderTimeout, err := time.ParseDuration(c.ReadHeaderTimeout)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil
|
||||
|
||||
if c.DataURL == "" {
|
||||
return nil, ErrInvalidDataURL
|
||||
}
|
||||
|
||||
return &AppSettings{
|
||||
ServerAddress: c.ServerAddress,
|
||||
DataFile: c.DataFile,
|
||||
DataURL: c.DataURL,
|
||||
CacheTTL: cacheTTL,
|
||||
ReadHeaderTimeout: readHeaderTimeout,
|
||||
}, nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue