forked from linksmart/thing-directory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
125 lines (108 loc) · 3.14 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2014-2016 Fraunhofer Institute for Applied Information Technology FIT
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
"github.com/kelseyhightower/envconfig"
"github.com/linksmart/go-sec/auth/obtainer"
"github.com/linksmart/go-sec/auth/validator"
"github.com/linksmart/thing-directory/catalog"
)
type Config struct {
ServiceID string `json:"serviceID"`
Description string `json:"description"`
HTTP HTTPConfig `json:"http"`
DNSSD DNSSDConfig `json:"dnssd"`
Storage StorageConfig `json:"storage"`
ServiceCatalog ServiceCatalog `json:"serviceCatalog"`
}
type HTTPConfig struct {
PublicEndpoint string `json:"publicEndpoint"`
BindAddr string `json:"bindAddr"`
BindPort int `json:"bindPort"`
Auth validator.Conf `json:"auth"`
}
type ServiceCatalog struct {
Enabled bool `json:"enabled"`
Discover bool `json:"discover"`
Endpoint string `json:"endpoint"`
Ttl int `json:"ttl"`
Auth obtainer.Conf `json:"auth"`
}
type DNSSDConfig struct {
Publish struct {
Enabled bool `json:"enabled"`
Instance string `json:"instance"`
Domain string `json:"domain"`
Interfaces []string `json:"interfaces"`
}
}
type StorageConfig struct {
Type string `json:"type"`
DSN string `json:"dsn"`
}
var supportedBackends = map[string]bool{
catalog.BackendMemory: false,
catalog.BackendLevelDB: true,
}
func (c *Config) Validate() error {
if c.HTTP.BindAddr == "" || c.HTTP.BindPort == 0 || c.HTTP.PublicEndpoint == "" {
return fmt.Errorf("BindAddr, BindPort, and PublicEndpoint have to be defined")
}
_, err := url.Parse(c.HTTP.PublicEndpoint)
if err != nil {
return fmt.Errorf("PublicEndpoint should be a valid URL")
}
if c.HTTP.Auth.Enabled {
// Validate ticket validator config
err = c.HTTP.Auth.Validate()
if err != nil {
return fmt.Errorf("invalid auth: %s", err)
}
}
_, err = url.Parse(c.Storage.DSN)
if err != nil {
return fmt.Errorf("storage DSN should be a valid URL")
}
if !supportedBackends[c.Storage.Type] {
return fmt.Errorf("unsupported storage backend")
}
if c.ServiceCatalog.Enabled {
if c.ServiceCatalog.Endpoint == "" && c.ServiceCatalog.Discover == false {
return fmt.Errorf("Service Catalog must have either endpoint or set discovery flag")
}
if c.ServiceCatalog.Ttl <= 0 {
return fmt.Errorf("Service Catalog must have TTL >= 0")
}
if c.ServiceCatalog.Auth.Enabled {
// Validate ticket obtainer config
err = c.ServiceCatalog.Auth.Validate()
if err != nil {
return fmt.Errorf("invalid Service Catalog auth: %s", err)
}
}
}
return err
}
func loadConfig(path string) (*Config, error) {
file, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
var config Config
err = json.Unmarshal(file, &config)
if err != nil {
return nil, err
}
// Override loaded values with environment variables
err = envconfig.Process("td", &config)
if err != nil {
return nil, err
}
if err = config.Validate(); err != nil {
return nil, fmt.Errorf("invalid configuration: %s", err)
}
return &config, nil
}