1
0
Fork 0
mirror of https://github.com/shouptech/tempgopher.git synced 2026-02-03 16:49:42 +00:00

Add configuration parameter for listen address

You can now specify :8080, 127.0.0.1:5555 or whatever
This commit is contained in:
Emma 2018-10-03 20:05:00 -06:00
parent 8b3093d0f1
commit 446fb72189
7 changed files with 17 additions and 5 deletions

View file

@ -27,6 +27,7 @@ scp temp-gopher <raspberrypi>:~/somepath
Create a `config.yml` file like this:
```
listenaddr: ":8080" # Address:port to listen on. If not specified, defaults to ":8080"
baseurl: http://<pihostname>: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

View file

@ -26,6 +26,7 @@ type Sensor struct {
type Config struct {
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 {

View file

@ -24,6 +24,7 @@ func Test_LoadConfig(t *testing.T) {
testConfig := Config{
Sensors: []Sensor{testSensor},
BaseURL: "https://foo.bar",
ListenAddr: "127.0.0.1:8080",
}
loadedConfig, err := LoadConfig("tests/test_config.yml")

View file

@ -22,3 +22,4 @@ sensors:
coolminutes: 10
verbose: true
baseurl: https://foo.bar
listenaddr: 127.0.0.1:8080

View file

@ -22,3 +22,4 @@ sensors:
coolminutes: 10
verbose: true
baseurl: https://foo.bar
listenaddr: 127.0.0.1:8080

View file

@ -11,3 +11,4 @@ sensors:
coolminutes: 10
verbose: true
baseurl: https://foo.bar
listenaddr: 127.0.0.1:8080

2
web.go
View file

@ -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,
}