Add config #3
							
								
								
									
										1
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								go.mod
									
									
									
									
									
								
							| @@ -12,6 +12,7 @@ require ( | ||||
| 	github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect | ||||
| 	github.com/davecgh/go-spew v1.1.1 // indirect | ||||
| 	github.com/google/uuid v1.4.0 // indirect | ||||
| 	github.com/pelletier/go-toml/v2 v2.1.0 // indirect | ||||
| 	github.com/pion/datachannel v1.5.5 // indirect | ||||
| 	github.com/pion/dtls/v2 v2.2.8 // indirect | ||||
| 	github.com/pion/ice/v3 v3.0.2 // indirect | ||||
|   | ||||
							
								
								
									
										2
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								go.sum
									
									
									
									
									
								
							| @@ -37,6 +37,8 @@ github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042 | ||||
| github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= | ||||
| github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= | ||||
| github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY= | ||||
| github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4= | ||||
| github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= | ||||
| github.com/pion/datachannel v1.5.5 h1:10ef4kwdjije+M9d7Xm9im2Y3O6A6ccQb0zcqZcJew8= | ||||
| github.com/pion/datachannel v1.5.5/go.mod h1:iMz+lECmfdCMqFRhXhcA/219B0SQlbpoR2V118yimL0= | ||||
| github.com/pion/dtls/v2 v2.2.7/go.mod h1:8WiMkebSHFD0T+dIU+UeBaoV7kDhOW5oDCzZ7WZ/F9s= | ||||
|   | ||||
							
								
								
									
										9
									
								
								ministream.toml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								ministream.toml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | ||||
| # SiteName is used for displaying title of site on frontend. | ||||
| # Default: "ministream" | ||||
| # Env: MINISTREAM_SITENAME | ||||
| SiteName = "stream.example.org" | ||||
|  | ||||
| # HTTPListenAddr is which port the HTTP server will listen on. | ||||
| # Default: ":8080" | ||||
| # Env: MINISTREAM_HTTPLISTENADDR | ||||
| HTTPListenAddr = ":8080" | ||||
							
								
								
									
										73
									
								
								server/config.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										73
									
								
								server/config.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,73 @@ | ||||
| package server | ||||
|  | ||||
| import ( | ||||
| 	"io" | ||||
| 	"os" | ||||
|  | ||||
| 	"github.com/pelletier/go-toml/v2" | ||||
| ) | ||||
|  | ||||
| type Config struct { | ||||
| 	SiteName       string `json:"siteName" toml:"siteName"` | ||||
| 	HTTPListenAddr string `json:"httpListenAddr" toml:"HTTPListenAddr"` | ||||
| } | ||||
|  | ||||
| func DefaultConfig() *Config { | ||||
| 	return &Config{ | ||||
| 		SiteName:       "ministream", | ||||
| 		HTTPListenAddr: ":8080", | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func (c *Config) OverrideFromEnv() { | ||||
| 	if siteName, ok := os.LookupEnv("MINISTREAM_SITENAME"); ok { | ||||
| 		c.SiteName = siteName | ||||
| 	} | ||||
|  | ||||
| 	if httpAddr, ok := os.LookupEnv("MINISTREAM_HTTPLISTENADDR"); ok { | ||||
| 		c.HTTPListenAddr = httpAddr | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func ConfigFromReader(r io.Reader) (*Config, error) { | ||||
| 	var c Config | ||||
|  | ||||
| 	err := toml.NewDecoder(r).Decode(&c) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	return &c, nil | ||||
| } | ||||
|  | ||||
| func ConfigFromFile(path string) (*Config, error) { | ||||
| 	f, err := os.Open(path) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	defer f.Close() | ||||
|  | ||||
| 	return ConfigFromReader(f) | ||||
| } | ||||
|  | ||||
| func ConfigFromDefault() (*Config, error) { | ||||
| 	var config *Config | ||||
| 	defaultPaths := []string{ | ||||
| 		"ministream.toml", | ||||
| 	} | ||||
|  | ||||
| 	for _, p := range defaultPaths { | ||||
| 		c, err := ConfigFromFile(p) | ||||
| 		if err != nil { | ||||
| 			continue | ||||
| 		} | ||||
| 		config = c | ||||
| 		break | ||||
| 	} | ||||
|  | ||||
| 	if config == nil { | ||||
| 		config = DefaultConfig() | ||||
| 	} | ||||
|  | ||||
| 	return config, nil | ||||
| } | ||||
							
								
								
									
										37
									
								
								server/config_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								server/config_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,37 @@ | ||||
| package server_test | ||||
|  | ||||
| import ( | ||||
| 	"strings" | ||||
| 	"testing" | ||||
|  | ||||
| 	"git.t-juice.club/torjus/ministream/server" | ||||
| ) | ||||
|  | ||||
| func TestConfig(t *testing.T) { | ||||
| 	t.Run("FromReader", func(t *testing.T) { | ||||
| 		configString := `SiteName = "ministream.example.org"` | ||||
| 		expectedSiteName := "ministream.example.org" | ||||
| 		r := strings.NewReader(configString) | ||||
|  | ||||
| 		c, err := server.ConfigFromReader(r) | ||||
| 		if err != nil { | ||||
| 			t.Fatalf("Error reading config: %s", err) | ||||
| 		} | ||||
|  | ||||
| 		if c.SiteName != expectedSiteName { | ||||
| 			t.Errorf("SiteName incorrect. Got %s want %s", c.SiteName, expectedSiteName) | ||||
| 		} | ||||
| 	}) | ||||
| 	t.Run("OverrideFromEnv", func(t *testing.T) { | ||||
| 		c := server.DefaultConfig() | ||||
| 		expectedSiteName := "ms.example.org" | ||||
|  | ||||
| 		t.Setenv("MINISTREAM_SITENAME", expectedSiteName) | ||||
|  | ||||
| 		c.OverrideFromEnv() | ||||
|  | ||||
| 		if c.SiteName != expectedSiteName { | ||||
| 			t.Errorf("SiteName incorrect. Got %s want %s", c.SiteName, expectedSiteName) | ||||
| 		} | ||||
| 	}) | ||||
| } | ||||
		Reference in New Issue
	
	Block a user