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:
parent
8b3093d0f1
commit
446fb72189
7 changed files with 17 additions and 5 deletions
|
|
@ -27,6 +27,7 @@ scp temp-gopher <raspberrypi>:~/somepath
|
||||||
Create a `config.yml` file like this:
|
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
|
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:
|
sensors:
|
||||||
- id: 28-000008083108 # Id of the DS18b120 sensor
|
- id: 28-000008083108 # Id of the DS18b120 sensor
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,7 @@ type Sensor struct {
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Sensors []Sensor `yaml:"sensors"`
|
Sensors []Sensor `yaml:"sensors"`
|
||||||
BaseURL string `yaml:"baseurl"`
|
BaseURL string `yaml:"baseurl"`
|
||||||
|
ListenAddr string `yaml:"listenaddr"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadConfig will loads a file and parses it into a Config struct
|
// 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
|
var config Config
|
||||||
yaml.Unmarshal(data, &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)
|
ids := make(map[string]bool)
|
||||||
aliases := make(map[string]bool)
|
aliases := make(map[string]bool)
|
||||||
for _, v := range config.Sensors {
|
for _, v := range config.Sensors {
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,7 @@ func Test_LoadConfig(t *testing.T) {
|
||||||
testConfig := Config{
|
testConfig := Config{
|
||||||
Sensors: []Sensor{testSensor},
|
Sensors: []Sensor{testSensor},
|
||||||
BaseURL: "https://foo.bar",
|
BaseURL: "https://foo.bar",
|
||||||
|
ListenAddr: "127.0.0.1:8080",
|
||||||
}
|
}
|
||||||
|
|
||||||
loadedConfig, err := LoadConfig("tests/test_config.yml")
|
loadedConfig, err := LoadConfig("tests/test_config.yml")
|
||||||
|
|
|
||||||
|
|
@ -22,3 +22,4 @@ sensors:
|
||||||
coolminutes: 10
|
coolminutes: 10
|
||||||
verbose: true
|
verbose: true
|
||||||
baseurl: https://foo.bar
|
baseurl: https://foo.bar
|
||||||
|
listenaddr: 127.0.0.1:8080
|
||||||
|
|
|
||||||
|
|
@ -22,3 +22,4 @@ sensors:
|
||||||
coolminutes: 10
|
coolminutes: 10
|
||||||
verbose: true
|
verbose: true
|
||||||
baseurl: https://foo.bar
|
baseurl: https://foo.bar
|
||||||
|
listenaddr: 127.0.0.1:8080
|
||||||
|
|
|
||||||
|
|
@ -11,3 +11,4 @@ sensors:
|
||||||
coolminutes: 10
|
coolminutes: 10
|
||||||
verbose: true
|
verbose: true
|
||||||
baseurl: https://foo.bar
|
baseurl: https://foo.bar
|
||||||
|
listenaddr: 127.0.0.1:8080
|
||||||
|
|
|
||||||
2
web.go
2
web.go
|
|
@ -118,7 +118,7 @@ func RunWeb(configpath string, sc <-chan State, wg *sync.WaitGroup) {
|
||||||
// Launch the web server
|
// Launch the web server
|
||||||
r := SetupRouter(config, &states)
|
r := SetupRouter(config, &states)
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
Addr: ":8080",
|
Addr: config.ListenAddr,
|
||||||
Handler: r,
|
Handler: r,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue