Rework complete project
This commit is contained in:
parent
868965a072
commit
aebf7447c6
18 changed files with 2237 additions and 1980 deletions
59
cfg/cfg.go
Normal file
59
cfg/cfg.go
Normal file
|
@ -0,0 +1,59 @@
|
|||
package cfg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/alecthomas/kong"
|
||||
)
|
||||
|
||||
type AppSettings struct {
|
||||
ServerAddress string
|
||||
DataFile string
|
||||
DataURL string
|
||||
}
|
||||
|
||||
var cliStruct 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" default:"${default_file_url}"`
|
||||
}
|
||||
|
||||
func Parse() *AppSettings {
|
||||
ctx := kong.Parse(
|
||||
&cliStruct,
|
||||
kong.Vars{
|
||||
"default_address": ":8080",
|
||||
"default_file_path": "./data.csv",
|
||||
"default_file_url": "https://data.neuber.io/data.csv",
|
||||
},
|
||||
kong.Name("country_geo_locations"),
|
||||
kong.Description("🚀 Start a simple web server for GeoIP data"),
|
||||
kong.UsageOnError(),
|
||||
)
|
||||
|
||||
validateFlags(ctx)
|
||||
settings := &AppSettings{
|
||||
ServerAddress: cliStruct.ServerAddress,
|
||||
DataFile: cliStruct.DataFile,
|
||||
DataURL: cliStruct.DataURL,
|
||||
}
|
||||
return settings
|
||||
}
|
||||
|
||||
func validateFlags(cliCtx *kong.Context) {
|
||||
var flagsValid = true
|
||||
var messages = []string{}
|
||||
if cliStruct.ServerAddress == "" {
|
||||
messages = append(messages, "error: invalid server address, must not be blank")
|
||||
flagsValid = false
|
||||
}
|
||||
if !flagsValid {
|
||||
cliCtx.PrintUsage(false)
|
||||
fmt.Println()
|
||||
for i := 0; i < len(messages); i++ {
|
||||
fmt.Println(messages[i])
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue