diff --git a/README.md b/README.md index 9dfda12..de1bf31 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ scp temp-gopher :~/somepath Create a `config.yml` file like this: ``` +listenaddr: ":8080" # Address:port to listen on. If not specified, defaults to ":8080" baseurl: http://:8080 # Base URL to find the app at. Usually your Pi's IP address or hostname, unless using a reverse proxy sensors: - id: 28-000008083108 # Id of the DS18b120 sensor diff --git a/config.go b/config.go index 070b40f..9b6a217 100644 --- a/config.go +++ b/config.go @@ -24,8 +24,9 @@ type Sensor struct { // Config contains the applications configuration type Config struct { - Sensors []Sensor `yaml:"sensors"` - BaseURL string `yaml:"baseurl"` + Sensors []Sensor `yaml:"sensors"` + BaseURL string `yaml:"baseurl"` + ListenAddr string `yaml:"listenaddr"` } // LoadConfig will loads a file and parses it into a Config struct @@ -38,6 +39,12 @@ func LoadConfig(path string) (*Config, error) { var config Config yaml.Unmarshal(data, &config) + // Set a default listen address if not define. + if config.ListenAddr == "" { + config.ListenAddr = ":8080" + } + + // Check for Duplicates ids := make(map[string]bool) aliases := make(map[string]bool) for _, v := range config.Sensors { diff --git a/config_test.go b/config_test.go index 02bf022..e3d479c 100644 --- a/config_test.go +++ b/config_test.go @@ -22,8 +22,9 @@ func Test_LoadConfig(t *testing.T) { } testConfig := Config{ - Sensors: []Sensor{testSensor}, - BaseURL: "https://foo.bar", + Sensors: []Sensor{testSensor}, + BaseURL: "https://foo.bar", + ListenAddr: "127.0.0.1:8080", } loadedConfig, err := LoadConfig("tests/test_config.yml") diff --git a/tests/duplicate_alias.yml b/tests/duplicate_alias.yml index 91b1489..4f2f74e 100644 --- a/tests/duplicate_alias.yml +++ b/tests/duplicate_alias.yml @@ -22,3 +22,4 @@ sensors: coolminutes: 10 verbose: true baseurl: https://foo.bar +listenaddr: 127.0.0.1:8080 diff --git a/tests/duplicate_id.yml b/tests/duplicate_id.yml index 8eb2dd9..9758ae9 100644 --- a/tests/duplicate_id.yml +++ b/tests/duplicate_id.yml @@ -22,3 +22,4 @@ sensors: coolminutes: 10 verbose: true baseurl: https://foo.bar +listenaddr: 127.0.0.1:8080 diff --git a/tests/test_config.yml b/tests/test_config.yml index fbe1ada..5f9eb6b 100644 --- a/tests/test_config.yml +++ b/tests/test_config.yml @@ -11,3 +11,4 @@ sensors: coolminutes: 10 verbose: true baseurl: https://foo.bar +listenaddr: 127.0.0.1:8080 diff --git a/web.go b/web.go index 49bee31..254a784 100644 --- a/web.go +++ b/web.go @@ -118,7 +118,7 @@ func RunWeb(configpath string, sc <-chan State, wg *sync.WaitGroup) { // Launch the web server r := SetupRouter(config, &states) srv := &http.Server{ - Addr: ":8080", + Addr: config.ListenAddr, Handler: r, }