mediamtx/internal/conf/log_destination.go
Alessandro Ros 9c6ba7e2c7
New authentication system (#1341) (#1992) (#2205) (#3081)
This is a new authentication system that covers all the features exposed by the server, including playback, API, metrics and PPROF, improves internal authentication by adding permissions, improves HTTP-based authentication by adding the ability to exclude certain actions from being authenticated, adds an additional method (JWT-based authentication).
2024-03-04 14:20:34 +01:00

89 lines
1.5 KiB
Go

package conf
import (
"encoding/json"
"fmt"
"strings"
"github.com/bluenviron/mediamtx/internal/logger"
)
// LogDestinations is the logDestionations parameter.
type LogDestinations []logger.Destination
// MarshalJSON implements json.Marshaler.
func (d LogDestinations) MarshalJSON() ([]byte, error) {
out := make([]string, len(d))
i := 0
for _, p := range d {
var v string
switch p {
case logger.DestinationStdout:
v = "stdout"
case logger.DestinationFile:
v = "file"
default:
v = "syslog"
}
out[i] = v
i++
}
return json.Marshal(out)
}
func (d *LogDestinations) contains(v logger.Destination) bool {
for _, item := range *d {
if item == v {
return true
}
}
return false
}
// UnmarshalJSON implements json.Unmarshaler.
func (d *LogDestinations) UnmarshalJSON(b []byte) error {
var in []string
if err := json.Unmarshal(b, &in); err != nil {
return err
}
*d = nil
for _, dest := range in {
var v logger.Destination
switch dest {
case "stdout":
v = logger.DestinationStdout
case "file":
v = logger.DestinationFile
case "syslog":
v = logger.DestinationSyslog
default:
return fmt.Errorf("invalid log destination: %s", dest)
}
if d.contains(v) {
return fmt.Errorf("log destination set twice")
}
*d = append(*d, v)
}
return nil
}
// UnmarshalEnv implements env.Unmarshaler.
func (d *LogDestinations) UnmarshalEnv(_ string, v string) error {
byts, _ := json.Marshal(strings.Split(v, ","))
return d.UnmarshalJSON(byts)
}