uses conf file now

master
Bel LaPointe 2019-02-18 16:25:23 -07:00
parent 1d854cfff5
commit 2c266ffb36
3 changed files with 27 additions and 4 deletions

7
conf.yaml Normal file
View File

@ -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

View File

@ -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 {

View File

@ -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()
}