uses conf file now
parent
1d854cfff5
commit
2c266ffb36
|
|
@ -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
|
||||||
|
|
@ -8,7 +8,7 @@ import (
|
||||||
func GetPort() string {
|
func GetPort() string {
|
||||||
v := packable.NewString()
|
v := packable.NewString()
|
||||||
conf.Get(nsConf, flagPort, v)
|
conf.Get(nsConf, flagPort, v)
|
||||||
return v.String()
|
return ":" + strings.TrimPrefix(v.String(), ":")
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetRoutes() map[string]string {
|
func GetRoutes() map[string]string {
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"local/rproxy3/storage"
|
"local/rproxy3/storage"
|
||||||
"local/rproxy3/storage/packable"
|
"local/rproxy3/storage/packable"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
yaml "gopkg.in/yaml.v2"
|
yaml "gopkg.in/yaml.v2"
|
||||||
|
|
@ -28,9 +29,9 @@ type toBind struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type fileConf struct {
|
type fileConf struct {
|
||||||
Port string `yaml:"port"`
|
Port string `yaml:"p"`
|
||||||
Routes []string `yaml:"routes"`
|
Routes []string `yaml:"r"`
|
||||||
CertPath string `yaml:"cert"`
|
CertPath string `yaml:"crt"`
|
||||||
KeyPath string `yaml:"key"`
|
KeyPath string `yaml:"key"`
|
||||||
Username string `yaml:"user"`
|
Username string `yaml:"user"`
|
||||||
Password string `yaml:"pass"`
|
Password string `yaml:"pass"`
|
||||||
|
|
@ -48,7 +49,12 @@ func Init() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func fromFile() 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.String(flagConf, "/dev/null", "yaml config file path")
|
||||||
|
flag.Parse()
|
||||||
confFlag := flag.Lookup(flagConf)
|
confFlag := flag.Lookup(flagConf)
|
||||||
if confFlag == nil || confFlag.Value.String() == "" {
|
if confFlag == nil || confFlag.Value.String() == "" {
|
||||||
return nil
|
return nil
|
||||||
|
|
@ -85,6 +91,7 @@ func fromFile() error {
|
||||||
func fromFlags() error {
|
func fromFlags() error {
|
||||||
binds := make([]toBind, 0)
|
binds := make([]toBind, 0)
|
||||||
binds = append(binds, addFlag(flagPort, "51555", "port to bind to"))
|
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(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(flagCert, "", "path to .crt"))
|
||||||
binds = append(binds, addFlag(flagKey, "", "path to .key"))
|
binds = append(binds, addFlag(flagKey, "", "path to .key"))
|
||||||
|
|
@ -106,9 +113,18 @@ func fromFlags() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func addFlag(key, def, help string) toBind {
|
func addFlag(key, def, help string) toBind {
|
||||||
|
def = getFlagOrDefault(key, def)
|
||||||
v := flag.String(key, def, help)
|
v := flag.String(key, def, help)
|
||||||
return toBind{
|
return toBind{
|
||||||
flag: key,
|
flag: key,
|
||||||
value: v,
|
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()
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue