Add various features
* HTTP Server * Config files
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/pelletier/go-toml"
|
||||
)
|
||||
@@ -13,8 +14,11 @@ import (
|
||||
var ErrNotFound = errors.New("no config file found")
|
||||
|
||||
type Config struct {
|
||||
ListenAddr string `toml:"ListenAddr"`
|
||||
LogLevel string `toml:"LogLevel"`
|
||||
RTMPListenAddr string `toml:"RTMPListenAddr"`
|
||||
HTTPServerEnable bool `toml:"HTTPServerEnable"`
|
||||
HTTPListenAddr string `toml:"HTTPListenAddr"`
|
||||
HTTPAccessLogEnable bool `toml:"HTTPAccessLogEnable"`
|
||||
LogLevel string `toml:"LogLevel"`
|
||||
}
|
||||
|
||||
type InvalidValueError struct {
|
||||
@@ -28,7 +32,9 @@ func (ive *InvalidValueError) Error() string {
|
||||
func FromReader(r io.Reader) (*Config, error) {
|
||||
var c Config
|
||||
// Set some defaults
|
||||
c.ListenAddr = ":5566"
|
||||
c.RTMPListenAddr = ":5566"
|
||||
c.HTTPServerEnable = false
|
||||
c.HTTPListenAddr = ":8077"
|
||||
c.LogLevel = "INFO"
|
||||
|
||||
decoder := toml.NewDecoder(r)
|
||||
@@ -53,8 +59,27 @@ func (c *Config) UpdateFromEnv() error {
|
||||
if loglevel, found := os.LookupEnv("DOGTAMER_LOGLEVEL"); found {
|
||||
c.LogLevel = loglevel
|
||||
}
|
||||
if listenAddr, found := os.LookupEnv("DOGTAMER_LISTENADDR"); found {
|
||||
c.ListenAddr = listenAddr
|
||||
|
||||
if listenAddr, found := os.LookupEnv("DOGTAMER_RTMPLISTENADDR"); found {
|
||||
c.RTMPListenAddr = listenAddr
|
||||
}
|
||||
|
||||
if httpEnable, found := os.LookupEnv("DOGTAMER_HTTPSERVERENABLE"); found {
|
||||
switch strings.ToUpper(httpEnable) {
|
||||
case "TRUE", "YES", "ENABLE":
|
||||
c.HTTPServerEnable = true
|
||||
}
|
||||
}
|
||||
|
||||
if httpListenAddr, found := os.LookupEnv("DOGTAMER_HTTPLISTENADDR"); found {
|
||||
c.HTTPListenAddr = httpListenAddr
|
||||
}
|
||||
|
||||
if httpAccessLogEnable, found := os.LookupEnv("DOGTAMER_HTTPACCESSLOGENABLE"); found {
|
||||
switch strings.ToUpper(httpAccessLogEnable) {
|
||||
case "TRUE", "YES", "ENABLE":
|
||||
c.HTTPAccessLogEnable = true
|
||||
}
|
||||
}
|
||||
|
||||
return c.Verify()
|
||||
@@ -69,7 +94,7 @@ func FromFile(path string) (*Config, error) {
|
||||
return FromReader(f)
|
||||
}
|
||||
|
||||
func FromDefaultLocations(path string) (*Config, error) {
|
||||
func FromDefaultLocations() (*Config, error) {
|
||||
defaultLocations := []string{
|
||||
"dogtamer.toml",
|
||||
"/etc/dogtamer.toml",
|
||||
@@ -94,5 +119,9 @@ func FromDefaultLocations(path string) (*Config, error) {
|
||||
return cfg, cfg.UpdateFromEnv()
|
||||
}
|
||||
|
||||
cfg, err := FromReader(strings.NewReader(""))
|
||||
if err == nil {
|
||||
return cfg, ErrNotFound
|
||||
}
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
|
@@ -19,8 +19,8 @@ func TestConfig(t *testing.T) {
|
||||
}
|
||||
|
||||
// Ensure proper defaults are set
|
||||
if c.ListenAddr != ":5566" {
|
||||
t.Errorf("Unexpected ListenAddr: %s", c.ListenAddr)
|
||||
if c.RTMPListenAddr != ":5566" {
|
||||
t.Errorf("Unexpected RTMPListenAddr: %s", c.RTMPListenAddr)
|
||||
}
|
||||
if c.LogLevel != "INFO" {
|
||||
t.Errorf("Unexpected LogLevel: %s", c.LogLevel)
|
||||
@@ -32,7 +32,7 @@ func TestConfig(t *testing.T) {
|
||||
# Random comment
|
||||
LogLevel = "DEBUG"
|
||||
|
||||
ListenAddr = ":5555"
|
||||
RTMPListenAddr = ":5555"
|
||||
`
|
||||
sr := strings.NewReader(configString)
|
||||
|
||||
@@ -41,8 +41,8 @@ func TestConfig(t *testing.T) {
|
||||
t.Fatalf("Error reading config: %s", err)
|
||||
}
|
||||
|
||||
if c.ListenAddr != ":5555" {
|
||||
t.Errorf("Unexpected ListenAddr: %s", c.ListenAddr)
|
||||
if c.RTMPListenAddr != ":5555" {
|
||||
t.Errorf("Unexpected RTMPListenAddr: %s", c.RTMPListenAddr)
|
||||
}
|
||||
|
||||
if c.LogLevel != "DEBUG" {
|
||||
@@ -54,7 +54,7 @@ func TestConfig(t *testing.T) {
|
||||
# Random comment
|
||||
LogLevel = "INVALID"
|
||||
|
||||
ListenAddr = ":5555"
|
||||
RTMPListenAddr = ":5555"
|
||||
`
|
||||
sr := strings.NewReader(configString)
|
||||
|
||||
@@ -71,8 +71,8 @@ func TestConfig(t *testing.T) {
|
||||
os.Clearenv()
|
||||
|
||||
envValues := map[string]string{
|
||||
"DOGTAMER_LOGLEVEL": "DEBUG",
|
||||
"DOGTAMER_LISTENADDR": ":3333",
|
||||
"DOGTAMER_LOGLEVEL": "DEBUG",
|
||||
"DOGTAMER_RTMPLISTENADDR": ":3333",
|
||||
}
|
||||
|
||||
for k, v := range envValues {
|
||||
@@ -92,8 +92,8 @@ func TestConfig(t *testing.T) {
|
||||
t.Errorf("Error updating config with environment")
|
||||
}
|
||||
|
||||
if c.ListenAddr != envValues["DOGTAMER_LISTENADDR"] {
|
||||
t.Errorf("ListenAddr has wrong value: %s", c.ListenAddr)
|
||||
if c.RTMPListenAddr != envValues["DOGTAMER_RTMPLISTENADDR"] {
|
||||
t.Errorf("RTMPListenAddr has wrong value: %s", c.RTMPListenAddr)
|
||||
}
|
||||
if c.LogLevel != envValues["DOGTAMER_LOGLEVEL"] {
|
||||
t.Errorf("LogLevel has wrong value: %s", c.LogLevel)
|
||||
@@ -103,8 +103,8 @@ func TestConfig(t *testing.T) {
|
||||
os.Clearenv()
|
||||
|
||||
envValues := map[string]string{
|
||||
"DOGTAMER_LOGLEVEL": "TEST",
|
||||
"DOGTAMER_LISTENADDR": ":3333",
|
||||
"DOGTAMER_LOGLEVEL": "TEST",
|
||||
"DOGTAMER_RTMPLISTENADDR": ":3333",
|
||||
}
|
||||
|
||||
for k, v := range envValues {
|
||||
|
Reference in New Issue
Block a user