-
Notifications
You must be signed in to change notification settings - Fork 7
/
config.go
88 lines (72 loc) · 1.72 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
package config
import (
"os"
"strings"
"github.com/CESARBR/knot-cloud-storage/pkg/logging"
"github.com/spf13/viper"
)
// Server represents the server configuration properties
type Server struct {
Port int
}
// Logger represents the logger configuration properties
type Logger struct {
Level string
Syslog bool
}
// RabbitMQ represents the rabbitmq configuration properties
type RabbitMQ struct {
URL string
}
// MongoDB represents the database configuration properties
type MongoDB struct {
Host string
Port int
Name string
}
// Things represents the things service configuration properties
type Things struct {
Host string
Port int
}
// Expiration represents the data TTL configuration
type Expiration struct {
Time int32
}
// Config represents the service configuration
type Config struct {
Server
Logger
RabbitMQ
MongoDB
Things
Expiration
}
func readFile(name string) {
logger := logging.NewLogrus("error", false).Get("Config")
viper.SetConfigName(name)
if err := viper.ReadInConfig(); err != nil {
logger.Fatalf("Error reading config file, %s", err)
}
}
// Load returns the service configuration
func Load() Config {
var configuration Config
logger := logging.NewLogrus("error", false).Get("Config")
viper.AddConfigPath("internal/config")
viper.SetConfigType("yaml")
readFile("default")
if os.Getenv("ENV") == "development" {
readFile("development")
if err := viper.MergeInConfig(); err != nil {
logger.Fatalf("Error reading config file, %s", err)
}
}
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)
viper.AutomaticEnv()
if err := viper.Unmarshal(&configuration); err != nil {
logger.Fatalf("Error unmarshalling configuration, %s", err)
}
return configuration
}