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:
|
||||
|
||||
```
|
||||
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
|
||||
|
|
|
|||
11
config.go
11
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 {
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
|
|
|
|||
|
|
@ -22,3 +22,4 @@ sensors:
|
|||
coolminutes: 10
|
||||
verbose: true
|
||||
baseurl: https://foo.bar
|
||||
listenaddr: 127.0.0.1:8080
|
||||
|
|
|
|||
|
|
@ -22,3 +22,4 @@ sensors:
|
|||
coolminutes: 10
|
||||
verbose: true
|
||||
baseurl: https://foo.bar
|
||||
listenaddr: 127.0.0.1:8080
|
||||
|
|
|
|||
|
|
@ -11,3 +11,4 @@ sensors:
|
|||
coolminutes: 10
|
||||
verbose: true
|
||||
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
|
||||
r := SetupRouter(config, &states)
|
||||
srv := &http.Server{
|
||||
Addr: ":8080",
|
||||
Addr: config.ListenAddr,
|
||||
Handler: r,
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue