/
config.go
56 lines (46 loc) · 1.47 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package server
import (
"encoding/json"
"os"
"time"
)
type ClientConfiguration struct {
Secret string `json:"secret"`
}
// Configuration holds the server configuration
type Configuration struct {
UpdateInterval int `json:"updateInterval"`
Clients map[string]ClientConfiguration `json:"clients"`
Users map[string]string `json:"users"`
MinimumClientUpdateInterval time.Duration `json:"-"`
MaximumClientUpdateInterval time.Duration `json:"-"`
MaximumWidgetAge time.Duration `json:"-"`
}
// Validate a configuration if it is invalid an error is returned.
func (c *Configuration) ValidateConfiguration() error {
return nil
}
// Parse options from a configuration file and validate the configuration file.
//
// Returns an error if reading the configuration file failed or if the resulting
// configuration could not be Validated.
func (c *Configuration) ParseConfiguration(file string) error {
fh, err := os.Open(file)
if err != nil {
return err
}
defer fh.Close()
decoder := json.NewDecoder(fh)
if err := decoder.Decode(&c); err != nil {
return err
}
// Set defaults
c.MinimumClientUpdateInterval = 3 * time.Second
c.MaximumClientUpdateInterval = 5 * time.Second
c.MaximumWidgetAge = 30 * time.Second
if v := c.UpdateInterval; v > 0 {
c.MinimumClientUpdateInterval = time.Duration(v) * time.Second
c.MaximumClientUpdateInterval = time.Duration(3*v/2) * time.Second
}
return c.ValidateConfiguration()
}