-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
45 lines (36 loc) · 767 Bytes
/
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
package config
import (
"bytes"
_ "embed"
"strings"
"github.com/spf13/viper"
)
//go:embed config.yml
var defaultConfiguration []byte
type InfluxDB struct {
URL string
Org string
Bucket string
Token string
}
type Config struct {
InfluxDB *InfluxDB
}
func Read() (*Config, error) {
// Environment variables
viper.AutomaticEnv()
viper.SetEnvPrefix("APP")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
// Configuration file
viper.SetConfigType("yml")
// Read configuration
if err := viper.ReadConfig(bytes.NewBuffer(defaultConfiguration)); err != nil {
return nil, err
}
// Unmarshal the configuration
var config Config
if err := viper.Unmarshal(&config); err != nil {
return nil, err
}
return &config, nil
}