config.go 10.6 KB
Newer Older
1
2
3
package config

import (
4
	"time"
5
6
7
8
9
)

// Config defines the configuration structure.
type Config struct {
	General struct {
10
11
12
13
		LogLevel                  int    `mapstructure:"log_level"`
		LogToSyslog               bool   `mapstructure:"log_to_syslog"`
		PasswordHashIterations    int    `mapstructure:"password_hash_iterations"`
		GRPCDefaultResolverScheme string `mapstructure:"grpc_default_resolver_scheme"`
14
	} `mapstructure:"general"`
15
16

	PostgreSQL struct {
17
18
19
20
		DSN                string `mapstructure:"dsn"`
		Automigrate        bool
		MaxOpenConnections int `mapstructure:"max_open_connections"`
		MaxIdleConnections int `mapstructure:"max_idle_connections"`
21
22
23
	} `mapstructure:"postgresql"`

	Redis struct {
24
25
26
27
28
29
30
		URL        string   `mapstructure:"url"` // deprecated
		Servers    []string `mapstructure:"servers"`
		Cluster    bool     `mapstructure:"cluster"`
		MasterName string   `mapstructure:"master_name"`
		PoolSize   int      `mapstructure:"pool_size"`
		Password   string   `mapstructure:"password"`
		Database   int      `mapstructure:"database"`
31
		TLSEnabled bool     `mapstructure:"tls_enabled"`
32
		KeyPrefix  string   `mapstructure:"key_prefix"`
33
	} `mapstructure:"redis"`
34
35
36
37

	ApplicationServer struct {
		ID string `mapstructure:"id"`

38
39
40
41
42
43
44
45
46
		UserAuthentication struct {
			OpenIDConnect struct {
				Enabled                 bool   `mapstructure:"enabled"`
				RegistrationEnabled     bool   `mapstructure:"registration_enabled"`
				RegistrationCallbackURL string `mapstructure:"registration_callback_url"`
				ProviderURL             string `mapstructure:"provider_url"`
				ClientID                string `mapstructure:"client_id"`
				ClientSecret            string `mapstructure:"client_secret"`
				RedirectURL             string `mapstructure:"redirect_url"`
47
				LogoutURL               string `mapstructure:"logout_url"`
48
49
50
51
				LoginLabel              string `mapstructure:"login_label"`
			} `mapstructure:"openid_connect"`
		} `mapstructure:"user_authentication"`

52
53
54
55
56
57
		Codec struct {
			JS struct {
				MaxExecutionTime time.Duration `mapstructure:"max_execution_time"`
			} `mapstructure:"js"`
		} `mapstructure:"codec"`

58
		Integration struct {
59
60
61
62
63
64
65
			Marshaler       string                      `mapstructure:"marshaler"`
			Backend         string                      `mapstructure:"backend"` // deprecated
			Enabled         []string                    `mapstructure:"enabled"`
			AWSSNS          IntegrationAWSSNSConfig     `mapstructure:"aws_sns"`
			AzureServiceBus IntegrationAzureConfig      `mapstructure:"azure_service_bus"`
			MQTT            IntegrationMQTTConfig       `mapstructure:"mqtt"`
			GCPPubSub       IntegrationGCPConfig        `mapstructure:"gcp_pub_sub"`
66
			Kafka           IntegrationKafkaConfig      `mapstructure:"kafka"`
67
			PostgreSQL      IntegrationPostgreSQLConfig `mapstructure:"postgresql"`
68
			AMQP            IntegrationAMQPConfig       `mapstructure:"amqp"`
69
		} `mapstructure:"integration"`
70
71
72
73
74
75
76
77
78
79

		API struct {
			Bind       string
			CACert     string `mapstructure:"ca_cert"`
			TLSCert    string `mapstructure:"tls_cert"`
			TLSKey     string `mapstructure:"tls_key"`
			PublicHost string `mapstructure:"public_host"`
		} `mapstructure:"api"`

		ExternalAPI struct {
80
81
82
83
84
			Bind            string
			TLSCert         string `mapstructure:"tls_cert"`
			TLSKey          string `mapstructure:"tls_key"`
			JWTSecret       string `mapstructure:"jwt_secret"`
			CORSAllowOrigin string `mapstructure:"cors_allow_origin"`
85
86
87
88
89
		} `mapstructure:"external_api"`

		Branding struct {
			Footer       string
			Registration string
90
		} `mapstructure:"branding"`
91
92
93
94
95
96
97
	} `mapstructure:"application_server"`

	JoinServer struct {
		Bind    string
		CACert  string `mapstructure:"ca_cert"`
		TLSCert string `mapstructure:"tls_cert"`
		TLSKey  string `mapstructure:"tls_key"`
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
98
		EnableDane bool `mapstructure:"enable_dane"`
99
		AllowedDomains []string `mapstructure:"allowed_client_domains"`
Gaël Berthaud-Müller's avatar
Gaël Berthaud-Müller committed
100
		ResolvConf string `mapstructure:"resolv_conf"`
101
102
103
104
105
106
107
108
109

		KEK struct {
			ASKEKLabel string `mapstructure:"as_kek_label"`

			Set []struct {
				Label string `mapstructure:"label"`
				KEK   string `mapstructure:"kek"`
			}
		} `mapstructure:"kek"`
110
	} `mapstructure:"join_server"`
111
112

	Metrics struct {
113
114
115
116
117
118
119
120
		Timezone string `mapstructure:"timezone"`
		Redis    struct {
			AggregationIntervals []string      `mapstructure:"aggregation_intervals"`
			MinuteAggregationTTL time.Duration `mapstructure:"minute_aggregation_ttl"`
			HourAggregationTTL   time.Duration `mapstructure:"hour_aggregation_ttl"`
			DayAggregationTTL    time.Duration `mapstructure:"day_aggregation_ttl"`
			MonthAggregationTTL  time.Duration `mapstructure:"month_aggregation_ttl"`
		} `mapstructure:"redis"`
121
122
123
124
		Prometheus struct {
			EndpointEnabled    bool   `mapstructure:"endpoint_enabled"`
			Bind               string `mapstructure:"bind"`
			APITimingHistogram bool   `mapstructure:"api_timing_histogram"`
125
		} `mapstructure:"prometheus"`
126
	} `mapstructure:"metrics"`
127
128
129
130
131
132

	Monitoring struct {
		Bind                         string `mapstructure:"bind"`
		PrometheusEndpoint           bool   `mapstructure:"prometheus_endpoint"`
		PrometheusAPITimingHistogram bool   `mapstructure:"prometheus_api_timing_histogram"`
		HealthcheckEndpoint          bool   `mapstructure:"healthcheck_endpoint"`
133
		PerDeviceEventLogMaxHistory  int64  `mapstructure:"per_device_event_log_max_history"`
134
	} `mapstructure:"monitoring"`
135
136
}

137
138
// IntegrationMQTTConfig holds the configuration for the MQTT integration.
type IntegrationMQTTConfig struct {
139
140
	Client IntegrationMQTTClientConfig `mapstructure:"client"`

141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
	Server               string        `mapstructure:"server"`
	Username             string        `mapstructure:"username"`
	Password             string        `mapstructure:"password"`
	MaxReconnectInterval time.Duration `mapstructure:"max_reconnect_interval"`
	QOS                  uint8         `mapstructure:"qos"`
	CleanSession         bool          `mapstructure:"clean_session"`
	ClientID             string        `mapstructure:"client_id"`
	CACert               string        `mapstructure:"ca_cert"`
	TLSCert              string        `mapstructure:"tls_cert"`
	TLSKey               string        `mapstructure:"tls_key"`
	EventTopicTemplate   string        `mapstructure:"event_topic_template"`
	CommandTopicTemplate string        `mapstructure:"command_topic_template"`
	RetainEvents         bool          `mapstructure:"retain_events"`

	// For backards compatibility
	UplinkTopicTemplate        string `mapstructure:"uplink_topic_template"`
	DownlinkTopicTemplate      string `mapstructure:"downlink_topic_template"`
	JoinTopicTemplate          string `mapstructure:"join_topic_template"`
	AckTopicTemplate           string `mapstructure:"ack_topic_template"`
	ErrorTopicTemplate         string `mapstructure:"error_topic_template"`
	StatusTopicTemplate        string `mapstructure:"status_topic_template"`
	LocationTopicTemplate      string `mapstructure:"location_topic_template"`
	TxAckTopicTemplate         string `mapstructure:"tx_ack_topic_template"`
	IntegrationTopicTemplate   string `mapstructure:"integration_topic_template"`
	UplinkRetainedMessage      bool   `mapstructure:"uplink_retained_message"`
	JoinRetainedMessage        bool   `mapstructure:"join_retained_message"`
	AckRetainedMessage         bool   `mapstructure:"ack_retained_message"`
	ErrorRetainedMessage       bool   `mapstructure:"error_retained_message"`
	StatusRetainedMessage      bool   `mapstructure:"status_retained_message"`
	LocationRetainedMessage    bool   `mapstructure:"location_retained_message"`
	TxAckRetainedMessage       bool   `mapstructure:"tx_ack_retained_message"`
	IntegrationRetainedMessage bool   `mapstructure:"integration_retained_message"`
173
174
}

175
176
177
178
179
180
181
// IntegrationMQTTClientConfig holds the additional client config.
type IntegrationMQTTClientConfig struct {
	CACert             string        `mapstructure:"ca_cert"`
	CAKey              string        `mapstructure:"ca_key"`
	ClientCertLifetime time.Duration `mapstructure:"client_cert_lifetime"`
}

182
// IntegrationAWSSNSConfig holds the AWS SNS integration configuration.
183
type IntegrationAWSSNSConfig struct {
184
185
186
187
188
	Marshaler          string `mapstructure:"marshaler" json:"marshaler"`
	AWSRegion          string `mapstructure:"aws_region" json:"region"`
	AWSAccessKeyID     string `mapstructure:"aws_access_key_id" json:"accessKeyID"`
	AWSSecretAccessKey string `mapstructure:"aws_secret_access_key" json:"secretAccessKey"`
	TopicARN           string `mapstructure:"topic_arn" json:"topicARN"`
189
190
191
192
}

// IntegrationAzureConfig holds the Azure Service-Bus integration configuration.
type IntegrationAzureConfig struct {
193
194
195
196
	Marshaler        string           `mapstructure:"marshaler" json:"marshaler"`
	ConnectionString string           `mapstructure:"connection_string" json:"connectionString"`
	PublishMode      AzurePublishMode `mapstructure:"publish_mode" json:"-"`
	PublishName      string           `mapstructure:"publish_name" json:"publishName"`
197
198
199
200
}

// IntegrationGCPConfig holds the GCP Pub/Sub integration configuration.
type IntegrationGCPConfig struct {
201
202
203
204
205
	Marshaler            string `mapstructure:"marshaler" json:"marshaler"`
	CredentialsFile      string `mapstructure:"credentials_file" json:"-"`
	CredentialsFileBytes []byte `mapstructure:"-" json:"credentialsFile"`
	ProjectID            string `mapstructure:"project_id" json:"projectID"`
	TopicName            string `mapstructure:"topic_name" json:"topicName"`
206
207
208
209
}

// IntegrationPostgreSQLConfig holds the PostgreSQL integration configuration.
type IntegrationPostgreSQLConfig struct {
210
211
212
	DSN                string `json:"dsn"`
	MaxOpenConnections int    `mapstructure:"max_open_connections"`
	MaxIdleConnections int    `mapstructure:"max_idle_connections"`
213
214
}

215
216
217
218
219
220
// IntegrationAMQPConfig holds the AMQP integration configuration.
type IntegrationAMQPConfig struct {
	URL                     string `mapstructure:"url"`
	EventRoutingKeyTemplate string `mapstructure:"event_routing_key_template"`
}

221
222
223
// IntegrationKafkaConfig holds the Kafka integration configuration.
type IntegrationKafkaConfig struct {
	Brokers          []string `mapstructure:"brokers"`
224
	TLS              bool     `mapstructure:"tls"`
225
226
	Topic            string   `mapstructure:"topic"`
	EventKeyTemplate string   `mapstructure:"event_key_template"`
227
228
	Username         string   `mapstructure:"username"`
	Password         string   `mapstructure:"password"`
229
230
	Mechanism        string   `mapstructure:"mechanism"`
	Algorithm        string   `mapstructure:"algorithm"`
231
232
}

233
234
235
236
237
238
239
240
241
// AzurePublishMode defines the publish-mode type.
type AzurePublishMode string

// Publish modes.
const (
	AzurePublishModeTopic AzurePublishMode = "topic"
	AzurePublishModeQueue AzurePublishMode = "queue"
)

242
243
// C holds the global configuration.
var C Config
244
245
246
247
248

// Get returns the configuration.
func Get() *Config {
	return &C
}
249
250
251
252
253

// Set sets the configuration.
func Set(c Config) {
	C = c
}