Browse Source

fix regression that prevented setting config slices with env variables (#612)

pull/666/head v0.17.5
aler9 5 years ago
parent
commit
b70a4bfe5b
  1. 37
      internal/conf/authmethod.go
  2. 5
      internal/conf/conf_test.go
  3. 8
      internal/conf/ipsornets.go
  4. 6
      internal/conf/logdestination.go
  5. 6
      internal/conf/protocol.go
  6. 2
      internal/core/api_test.go

37
internal/conf/authmethod.go

@ -3,10 +3,39 @@ package conf
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"strings"
"github.com/aler9/gortsplib/pkg/headers" "github.com/aler9/gortsplib/pkg/headers"
) )
func unmarshalStringSlice(b []byte) ([]string, error) {
var in interface{}
if err := json.Unmarshal(b, &in); err != nil {
return nil, err
}
var slice []string
switch it := in.(type) {
case string: // from environment variables
slice = strings.Split(it, ",")
case []interface{}: // from yaml
for _, e := range it {
et, ok := e.(string)
if !ok {
return nil, fmt.Errorf("cannot unmarshal from %T", e)
}
slice = append(slice, et)
}
default:
return nil, fmt.Errorf("cannot unmarshal from %T", in)
}
return slice, nil
}
// AuthMethods is the authMethods parameter. // AuthMethods is the authMethods parameter.
type AuthMethods []headers.AuthMethod type AuthMethods []headers.AuthMethod
@ -29,12 +58,12 @@ func (d AuthMethods) MarshalJSON() ([]byte, error) {
// UnmarshalJSON unmarshals a AuthMethods from JSON. // UnmarshalJSON unmarshals a AuthMethods from JSON.
func (d *AuthMethods) UnmarshalJSON(b []byte) error { func (d *AuthMethods) UnmarshalJSON(b []byte) error {
var in []string slice, err := unmarshalStringSlice(b)
if err := json.Unmarshal(b, &in); err != nil { if err != nil {
return err return err
} }
for _, v := range in { for _, v := range slice {
switch v { switch v {
case "basic": case "basic":
*d = append(*d, headers.AuthBasic) *d = append(*d, headers.AuthBasic)
@ -43,7 +72,7 @@ func (d *AuthMethods) UnmarshalJSON(b []byte) error {
*d = append(*d, headers.AuthDigest) *d = append(*d, headers.AuthDigest)
default: default:
return fmt.Errorf("invalid authentication method: %s", in) return fmt.Errorf("invalid authentication method: %s", v)
} }
} }

5
internal/conf/conf_test.go

@ -86,6 +86,9 @@ func TestConfFromFileAndEnv(t *testing.T) {
os.Setenv("RTSP_PATHS_CAM1_SOURCE", "rtsp://testing") os.Setenv("RTSP_PATHS_CAM1_SOURCE", "rtsp://testing")
defer os.Unsetenv("RTSP_PATHS_CAM1_SOURCE") defer os.Unsetenv("RTSP_PATHS_CAM1_SOURCE")
os.Setenv("RTSP_PROTOCOLS", "tcp")
defer os.Unsetenv("RTSP_PROTOCOLS")
tmpf, err := writeTempFile([]byte("{}")) tmpf, err := writeTempFile([]byte("{}"))
require.NoError(t, err) require.NoError(t, err)
defer os.Remove(tmpf) defer os.Remove(tmpf)
@ -94,6 +97,8 @@ func TestConfFromFileAndEnv(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, true, hasFile) require.Equal(t, true, hasFile)
require.Equal(t, Protocols{ProtocolTCP: {}}, conf.Protocols)
pa, ok := conf.Paths["cam1"] pa, ok := conf.Paths["cam1"]
require.Equal(t, true, ok) require.Equal(t, true, ok)
require.Equal(t, &PathConf{ require.Equal(t, &PathConf{

8
internal/conf/ipsornets.go

@ -22,16 +22,16 @@ func (d IPsOrNets) MarshalJSON() ([]byte, error) {
// UnmarshalJSON unmarshals a IPsOrNets from JSON. // UnmarshalJSON unmarshals a IPsOrNets from JSON.
func (d *IPsOrNets) UnmarshalJSON(b []byte) error { func (d *IPsOrNets) UnmarshalJSON(b []byte) error {
var in []string slice, err := unmarshalStringSlice(b)
if err := json.Unmarshal(b, &in); err != nil { if err != nil {
return err return err
} }
if len(in) == 0 { if len(slice) == 0 {
return nil return nil
} }
for _, t := range in { for _, t := range slice {
if _, ipnet, err := net.ParseCIDR(t); err == nil { if _, ipnet, err := net.ParseCIDR(t); err == nil {
*d = append(*d, ipnet) *d = append(*d, ipnet)
} else if ip := net.ParseIP(t); ip != nil { } else if ip := net.ParseIP(t); ip != nil {

6
internal/conf/logdestination.go

@ -38,14 +38,14 @@ func (d LogDestinations) MarshalJSON() ([]byte, error) {
// UnmarshalJSON unmarshals a LogDestinations from JSON. // UnmarshalJSON unmarshals a LogDestinations from JSON.
func (d *LogDestinations) UnmarshalJSON(b []byte) error { func (d *LogDestinations) UnmarshalJSON(b []byte) error {
var in []string slice, err := unmarshalStringSlice(b)
if err := json.Unmarshal(b, &in); err != nil { if err != nil {
return err return err
} }
*d = make(LogDestinations) *d = make(LogDestinations)
for _, proto := range in { for _, proto := range slice {
switch proto { switch proto {
case "stdout": case "stdout":
(*d)[logger.DestinationStdout] = struct{}{} (*d)[logger.DestinationStdout] = struct{}{}

6
internal/conf/protocol.go

@ -46,14 +46,14 @@ func (d Protocols) MarshalJSON() ([]byte, error) {
// UnmarshalJSON unmarshals a Protocols from JSON. // UnmarshalJSON unmarshals a Protocols from JSON.
func (d *Protocols) UnmarshalJSON(b []byte) error { func (d *Protocols) UnmarshalJSON(b []byte) error {
var in []string slice, err := unmarshalStringSlice(b)
if err := json.Unmarshal(b, &in); err != nil { if err != nil {
return err return err
} }
*d = make(Protocols) *d = make(Protocols)
for _, proto := range in { for _, proto := range slice {
switch proto { switch proto {
case "udp": case "udp":
(*d)[ProtocolUDP] = struct{}{} (*d)[ProtocolUDP] = struct{}{}

2
internal/core/api_test.go

@ -72,6 +72,7 @@ func TestAPIConfigSet(t *testing.T) {
err := httpRequest(http.MethodPost, "http://localhost:9997/v1/config/set", map[string]interface{}{ err := httpRequest(http.MethodPost, "http://localhost:9997/v1/config/set", map[string]interface{}{
"rtmpDisable": true, "rtmpDisable": true,
"readTimeout": "7s", "readTimeout": "7s",
"protocols": []string{"tcp"},
}, nil) }, nil)
require.NoError(t, err) require.NoError(t, err)
@ -82,6 +83,7 @@ func TestAPIConfigSet(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, true, out["rtmpDisable"]) require.Equal(t, true, out["rtmpDisable"])
require.Equal(t, "7s", out["readTimeout"]) require.Equal(t, "7s", out["readTimeout"])
require.Equal(t, []interface{}{"tcp"}, out["protocols"])
} }
func TestAPIConfigPathsAdd(t *testing.T) { func TestAPIConfigPathsAdd(t *testing.T) {

Loading…
Cancel
Save