Browse Source

conf: use dedicated interface to unmarshal from environment

pull/666/head
aler9 5 years ago
parent
commit
94e519f08f
  1. 39
      internal/conf/authmethod.go
  2. 4
      internal/conf/credential.go
  3. 4
      internal/conf/encryption.go
  4. 9
      internal/conf/env.go
  5. 14
      internal/conf/ipsornets.go
  6. 12
      internal/conf/logdestination.go
  7. 4
      internal/conf/loglevel.go
  8. 12
      internal/conf/protocol.go
  9. 4
      internal/conf/sourceprotocol.go
  10. 4
      internal/conf/stringduration.go

39
internal/conf/authmethod.go

@ -8,34 +8,6 @@ import (
"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
@ -58,12 +30,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 {
slice, err := unmarshalStringSlice(b) var in []string
if err != nil { if err := json.Unmarshal(b, &in); err != nil {
return err return err
} }
for _, v := range slice { for _, v := range in {
switch v { switch v {
case "basic": case "basic":
*d = append(*d, headers.AuthBasic) *d = append(*d, headers.AuthBasic)
@ -78,3 +50,8 @@ func (d *AuthMethods) UnmarshalJSON(b []byte) error {
return nil return nil
} }
func (d *AuthMethods) unmarshalEnv(s string) error {
byts, _ := json.Marshal(strings.Split(s, ","))
return d.UnmarshalJSON(byts)
}

4
internal/conf/credential.go

@ -35,3 +35,7 @@ func (d *Credential) UnmarshalJSON(b []byte) error {
*d = Credential(in) *d = Credential(in)
return nil return nil
} }
func (d *Credential) unmarshalEnv(s string) error {
return d.UnmarshalJSON([]byte(`"` + s + `"`))
}

4
internal/conf/encryption.go

@ -56,3 +56,7 @@ func (d *Encryption) UnmarshalJSON(b []byte) error {
return nil return nil
} }
func (d *Encryption) unmarshalEnv(s string) error {
return d.UnmarshalJSON([]byte(`"` + s + `"`))
}

9
internal/conf/env.go

@ -1,7 +1,6 @@
package conf package conf
import ( import (
"encoding/json"
"fmt" "fmt"
"os" "os"
"reflect" "reflect"
@ -9,12 +8,16 @@ import (
"strings" "strings"
) )
type envUnmarshaler interface {
unmarshalEnv(string) error
}
func loadEnvInternal(env map[string]string, prefix string, rv reflect.Value) error { func loadEnvInternal(env map[string]string, prefix string, rv reflect.Value) error {
rt := rv.Type() rt := rv.Type()
if i, ok := rv.Addr().Interface().(json.Unmarshaler); ok { if i, ok := rv.Addr().Interface().(envUnmarshaler); ok {
if ev, ok := env[prefix]; ok { if ev, ok := env[prefix]; ok {
err := i.UnmarshalJSON([]byte(`"` + ev + `"`)) err := i.unmarshalEnv(ev)
if err != nil { if err != nil {
return fmt.Errorf("%s: %s", prefix, err) return fmt.Errorf("%s: %s", prefix, err)
} }

14
internal/conf/ipsornets.go

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net" "net"
"strings"
) )
// IPsOrNets is a parameter that acceps IPs or subnets. // IPsOrNets is a parameter that acceps IPs or subnets.
@ -22,16 +23,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 {
slice, err := unmarshalStringSlice(b) var in []string
if err != nil { if err := json.Unmarshal(b, &in); err != nil {
return err return err
} }
if len(slice) == 0 { if len(in) == 0 {
return nil return nil
} }
for _, t := range slice { for _, t := range in {
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 {
@ -43,3 +44,8 @@ func (d *IPsOrNets) UnmarshalJSON(b []byte) error {
return nil return nil
} }
func (d *IPsOrNets) unmarshalEnv(s string) error {
byts, _ := json.Marshal(strings.Split(s, ","))
return d.UnmarshalJSON(byts)
}

12
internal/conf/logdestination.go

@ -3,6 +3,7 @@ package conf
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"strings"
"github.com/aler9/rtsp-simple-server/internal/logger" "github.com/aler9/rtsp-simple-server/internal/logger"
) )
@ -38,14 +39,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 {
slice, err := unmarshalStringSlice(b) var in []string
if err != nil { if err := json.Unmarshal(b, &in); err != nil {
return err return err
} }
*d = make(LogDestinations) *d = make(LogDestinations)
for _, proto := range slice { for _, proto := range in {
switch proto { switch proto {
case "stdout": case "stdout":
(*d)[logger.DestinationStdout] = struct{}{} (*d)[logger.DestinationStdout] = struct{}{}
@ -63,3 +64,8 @@ func (d *LogDestinations) UnmarshalJSON(b []byte) error {
return nil return nil
} }
func (d *LogDestinations) unmarshalEnv(s string) error {
byts, _ := json.Marshal(strings.Split(s, ","))
return d.UnmarshalJSON(byts)
}

4
internal/conf/loglevel.go

@ -51,3 +51,7 @@ func (d *LogLevel) UnmarshalJSON(b []byte) error {
return nil return nil
} }
func (d *LogLevel) unmarshalEnv(s string) error {
return d.UnmarshalJSON([]byte(`"` + s + `"`))
}

12
internal/conf/protocol.go

@ -3,6 +3,7 @@ package conf
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"strings"
) )
// Protocol is a RTSP stream protocol. // Protocol is a RTSP stream protocol.
@ -46,14 +47,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 {
slice, err := unmarshalStringSlice(b) var in []string
if err != nil { if err := json.Unmarshal(b, &in); err != nil {
return err return err
} }
*d = make(Protocols) *d = make(Protocols)
for _, proto := range slice { for _, proto := range in {
switch proto { switch proto {
case "udp": case "udp":
(*d)[ProtocolUDP] = struct{}{} (*d)[ProtocolUDP] = struct{}{}
@ -71,3 +72,8 @@ func (d *Protocols) UnmarshalJSON(b []byte) error {
return nil return nil
} }
func (d *Protocols) unmarshalEnv(s string) error {
byts, _ := json.Marshal(strings.Split(s, ","))
return d.UnmarshalJSON(byts)
}

4
internal/conf/sourceprotocol.go

@ -62,3 +62,7 @@ func (d *SourceProtocol) UnmarshalJSON(b []byte) error {
return nil return nil
} }
func (d *SourceProtocol) unmarshalEnv(s string) error {
return d.UnmarshalJSON([]byte(`"` + s + `"`))
}

4
internal/conf/stringduration.go

@ -29,3 +29,7 @@ func (d *StringDuration) UnmarshalJSON(b []byte) error {
return nil return nil
} }
func (d *StringDuration) unmarshalEnv(s string) error {
return d.UnmarshalJSON([]byte(`"` + s + `"`))
}

Loading…
Cancel
Save