This commit is contained in:
Joachim Bauch 2024-04-30 12:02:01 +02:00 committed by GitHub
commit 6bb259a4c3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 81 additions and 10 deletions

View file

@ -23,10 +23,40 @@ package signaling
import (
"errors"
"os"
"regexp"
"github.com/dlintw/goconf"
)
var (
searchVarsRegexp = regexp.MustCompile(`\$\([A-Za-z][A-Za-z0-9]*\)`)
)
func replaceEnvVars(s string) string {
return searchVarsRegexp.ReplaceAllStringFunc(s, func(name string) string {
name = name[2 : len(name)-1]
value, found := os.LookupEnv(name)
if !found {
return name
}
return value
})
}
// GetStringOptionWithEnv will get the string option and resolve any environment
// variable references in the form "$(VAR)".
func GetStringOptionWithEnv(config *goconf.ConfigFile, section string, option string) (string, error) {
value, err := config.GetString(section, option)
if err != nil {
return "", err
}
value = replaceEnvVars(value)
return value, nil
}
func GetStringOptions(config *goconf.ConfigFile, section string, ignoreErrors bool) (map[string]string, error) {
options, _ := config.GetOptions(section)
if len(options) == 0 {
@ -35,7 +65,7 @@ func GetStringOptions(config *goconf.ConfigFile, section string, ignoreErrors bo
result := make(map[string]string)
for _, option := range options {
value, err := config.GetString(section, option)
value, err := GetStringOptionWithEnv(config, section, option)
if err != nil {
if ignoreErrors {
continue

View file

@ -29,13 +29,19 @@ import (
)
func TestStringOptions(t *testing.T) {
t.Setenv("FOO", "foo")
expected := map[string]string{
"one": "1",
"two": "2",
"foo": "http://foo/1",
}
config := goconf.NewConfigFile()
for k, v := range expected {
config.AddOption("foo", k, v)
if k == "foo" {
config.AddOption("foo", k, "http://$(FOO)/1")
} else {
config.AddOption("foo", k, v)
}
}
config.AddOption("default", "three", "3")
@ -48,3 +54,36 @@ func TestStringOptions(t *testing.T) {
t.Errorf("expected %+v, got %+v", expected, options)
}
}
func TestStringOptionWithEnv(t *testing.T) {
t.Setenv("FOO", "foo")
t.Setenv("BAR", "")
config := goconf.NewConfigFile()
config.AddOption("test", "foo", "http://$(FOO)/1")
config.AddOption("test", "bar", "http://$(BAR)/2")
config.AddOption("test", "baz", "http://$(BAZ)/3")
config.AddOption("test", "inv1", "http://$(FOO")
config.AddOption("test", "inv2", "http://$FOO)")
config.AddOption("test", "inv3", "http://$((FOO)")
config.AddOption("test", "inv4", "http://$(F.OO)")
expected := map[string]string{
"foo": "http://foo/1",
"bar": "http:///2",
"baz": "http://BAZ/3",
"inv1": "http://$(FOO",
"inv2": "http://$FOO)",
"inv3": "http://$((FOO)",
"inv4": "http://$(F.OO)",
}
for k, v := range expected {
value, err := GetStringOptionWithEnv(config, "test", k)
if err != nil {
t.Errorf("expected value for %s, got %s", k, err)
} else if value != v {
t.Errorf("expected value %s for %s, got %s", v, k, value)
}
}
}

View file

@ -36,6 +36,8 @@ import (
"github.com/dlintw/goconf"
"github.com/gorilla/mux"
signaling "github.com/strukturag/nextcloud-spreed-signaling"
)
var (
@ -90,7 +92,7 @@ func main() {
}
defer proxy.Stop()
if addr, _ := config.GetString("http", "listen"); addr != "" {
if addr, _ := signaling.GetStringOptionWithEnv(config, "http", "listen"); addr != "" {
readTimeout, _ := config.GetInt("http", "readtimeout")
if readTimeout <= 0 {
readTimeout = defaultReadTimeout

View file

@ -223,7 +223,7 @@ func (s *ProxyServer) checkOrigin(r *http.Request) bool {
}
func (s *ProxyServer) Start(config *goconf.ConfigFile) error {
s.url, _ = config.GetString("mcu", "url")
s.url, _ = signaling.GetStringOptionWithEnv(config, "mcu", "url")
if s.url == "" {
return fmt.Errorf("No MCU server url configured")
}

View file

@ -86,7 +86,7 @@ func (p *proxyConfigStatic) configure(config *goconf.ConfigFile, fromReload bool
remove[u] = ips
}
mcuUrl, _ := config.GetString("mcu", "url")
mcuUrl, _ := GetStringOptionWithEnv(config, "mcu", "url")
for _, u := range strings.Split(mcuUrl, " ") {
u = strings.TrimSpace(u)
if u == "" {

View file

@ -171,7 +171,7 @@ func main() {
signaling.RegisterStats()
natsUrl, _ := config.GetString("nats", "url")
natsUrl, _ := signaling.GetStringOptionWithEnv(config, "nats", "url")
if natsUrl == "" {
natsUrl = nats.DefaultURL
}
@ -224,7 +224,7 @@ func main() {
log.Fatal("Could not create hub: ", err)
}
mcuUrl, _ := config.GetString("mcu", "url")
mcuUrl, _ := signaling.GetStringOptionWithEnv(config, "mcu", "url")
mcuType, _ := config.GetString("mcu", "type")
if mcuType == "" && mcuUrl != "" {
log.Printf("WARNING: Old-style MCU configuration detected with url but no type, defaulting to type %s", signaling.McuTypeJanus)
@ -274,7 +274,7 @@ func main() {
if config, err = goconf.ReadConfigFile(*configFlag); err != nil {
log.Printf("Could not read configuration from %s: %s", *configFlag, err)
} else {
mcuUrl, _ = config.GetString("mcu", "url")
mcuUrl, _ = signaling.GetStringOptionWithEnv(config, "mcu", "url")
mcuType, _ = config.GetString("mcu", "type")
if mcuType == "" && mcuUrl != "" {
log.Printf("WARNING: Old-style MCU configuration detected with url but no type, defaulting to type %s", signaling.McuTypeJanus)
@ -328,7 +328,7 @@ func main() {
var listeners Listeners
if saddr, _ := config.GetString("https", "listen"); saddr != "" {
if saddr, _ := signaling.GetStringOptionWithEnv(config, "https", "listen"); saddr != "" {
cert, _ := config.GetString("https", "certificate")
key, _ := config.GetString("https", "key")
if cert == "" || key == "" {
@ -366,7 +366,7 @@ func main() {
}
}
if addr, _ := config.GetString("http", "listen"); addr != "" {
if addr, _ := signaling.GetStringOptionWithEnv(config, "http", "listen"); addr != "" {
readTimeout, _ := config.GetInt("http", "readtimeout")
if readTimeout <= 0 {
readTimeout = defaultReadTimeout