From 2c266ffb36a2142fad08f6198c450d384e292544 Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Mon, 18 Feb 2019 16:25:23 -0700 Subject: [PATCH] uses conf file now --- conf.yaml | 7 +++++++ config/config.go | 2 +- config/new.go | 22 +++++++++++++++++++--- 3 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 conf.yaml diff --git a/conf.yaml b/conf.yaml new file mode 100644 index 0000000..835891b --- /dev/null +++ b/conf.yaml @@ -0,0 +1,7 @@ +p: 54243 +r: +- echo:http://localhost:49982,echo2:http://localhost:49983 +crt: ./testdata/rproxy3server.crt +key: ./testdata/rproxy3server.key +user: bel +pass: bel diff --git a/config/config.go b/config/config.go index e5d6f21..7b2b73c 100644 --- a/config/config.go +++ b/config/config.go @@ -8,7 +8,7 @@ import ( func GetPort() string { v := packable.NewString() conf.Get(nsConf, flagPort, v) - return v.String() + return ":" + strings.TrimPrefix(v.String(), ":") } func GetRoutes() map[string]string { diff --git a/config/new.go b/config/new.go index 2513f7c..21e1469 100644 --- a/config/new.go +++ b/config/new.go @@ -6,6 +6,7 @@ import ( "local/rproxy3/storage" "local/rproxy3/storage/packable" "log" + "os" "strings" yaml "gopkg.in/yaml.v2" @@ -28,9 +29,9 @@ type toBind struct { } type fileConf struct { - Port string `yaml:"port"` - Routes []string `yaml:"routes"` - CertPath string `yaml:"cert"` + Port string `yaml:"p"` + Routes []string `yaml:"r"` + CertPath string `yaml:"crt"` KeyPath string `yaml:"key"` Username string `yaml:"user"` Password string `yaml:"pass"` @@ -48,7 +49,12 @@ func Init() error { } func fromFile() error { + flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError) + defer func() { + flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError) + }() flag.String(flagConf, "/dev/null", "yaml config file path") + flag.Parse() confFlag := flag.Lookup(flagConf) if confFlag == nil || confFlag.Value.String() == "" { return nil @@ -85,6 +91,7 @@ func fromFile() error { func fromFlags() error { binds := make([]toBind, 0) binds = append(binds, addFlag(flagPort, "51555", "port to bind to")) + binds = append(binds, addFlag(flagConf, "", "configuration file path")) binds = append(binds, addFlag(flagRoutes, "", "comma-separated routes to map, each as from:scheme://to.tld:port")) binds = append(binds, addFlag(flagCert, "", "path to .crt")) binds = append(binds, addFlag(flagKey, "", "path to .key")) @@ -106,9 +113,18 @@ func fromFlags() error { } func addFlag(key, def, help string) toBind { + def = getFlagOrDefault(key, def) v := flag.String(key, def, help) return toBind{ flag: key, value: v, } } + +func getFlagOrDefault(key, def string) string { + v := packable.NewString() + if err := conf.Get(nsConf, key, v); err != nil { + return def + } + return v.String() +}