forked from motiv-labs/janus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
specification.go
194 lines (165 loc) · 5.78 KB
/
specification.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package config
import (
"time"
"github.com/hellofresh/logging-go"
"github.com/kelseyhightower/envconfig"
"github.com/pkg/errors"
"github.com/spf13/viper"
)
// Specification for basic configurations
type Specification struct {
Port int `envconfig:"PORT"`
Debug bool `envconfig:"DEBUG"`
GraceTimeOut int64 `envconfig:"GRACE_TIMEOUT"`
MaxIdleConnsPerHost int `envconfig:"MAX_IDLE_CONNS_PER_HOST"`
BackendFlushInterval time.Duration `envconfig:"BACKEND_FLUSH_INTERVAL"`
CloseIdleConnsPeriod time.Duration `envconfig:"CLOSE_IDLE_CONNS_PERIOD"`
Log logging.LogConfig
Web Web
Database Database
Storage Storage
Stats Stats
Tracing Tracing
TLS TLS
Prometheus Prometheus
}
// Web represents the API configurations
type Web struct {
Port int `envconfig:"API_PORT"`
ReadOnly bool `envconfig:"API_READONLY"`
Credentials Credentials
TLS TLS
}
// TLS represents the TLS configurations
type TLS struct {
Port int `envconfig:"PORT"`
CertFile string `envconfig:"CERT_PATH"`
KeyFile string `envconfig:"KEY_PATH"`
Redirect bool `envconfig:"REDIRECT"`
}
// IsHTTPS checks if you have https enabled
func (s *TLS) IsHTTPS() bool {
return s.CertFile != "" && s.KeyFile != ""
}
// Storage holds the configuration for a storage
type Storage struct {
DSN string `envconfig:"STORAGE_DSN"`
}
// Database holds the configuration for a database
type Database struct {
DSN string `envconfig:"DATABASE_DSN"`
}
// Stats holds the configuration for stats
type Stats struct {
DSN string `envconfig:"STATS_DSN"`
Prefix string `envconfig:"STATS_PREFIX"`
IDs string `envconfig:"STATS_IDS"`
AutoDiscoverThreshold uint `envconfig:"STATS_AUTO_DISCOVER_THRESHOLD"`
AutoDiscoverWhiteList []string `envconfig:"STATS_AUTO_DISCOVER_WHITE_LIST"`
ErrorsSection string `envconfig:"STATS_ERRORS_SECTION"`
}
// Prometheus represents prometheus integration configuration
type Prometheus struct {
Enabled bool `envconfig:"PROMETHEUS_ENABLED"`
MetricPath string `envconfig:"PROMETHEUS_METRIC_PATH"`
}
// Credentials represents the credentials that are going to be
// used by admin JWT configuration
type Credentials struct {
// Algorithm defines admin JWT signing algorithm.
// Currently the following algorithms are supported: HS256, HS384, HS512.
Algorithm string `envconfig:"ALGORITHM"`
Secret string `envconfig:"SECRET"`
JanusAdminTeam string `envconfig:"JANUS_ADMIN_TEAM"`
Github Github
Basic Basic
}
// Basic holds the basic users configurations
type Basic struct {
Users map[string]string `envconfig:"BASIC_USERS"`
}
// Github holds the github configurations
type Github struct {
Organizations []string `envconfig:"GITHUB_ORGANIZATIONS"`
Teams map[string]string `envconfig:"GITHUB_TEAMS"`
}
// IsConfigured checks if github is enabled
func (auth *Github) IsConfigured() bool {
return len(auth.Organizations) > 0 ||
len(auth.Teams) > 0
}
// GoogleCloudTracing holds the Google Application Default Credentials
type GoogleCloudTracing struct {
ProjectID string `envconfig:"TRACING_GC_PROJECT_ID"`
Email string `envconfig:"TRACING_GC_EMAIL"`
PrivateKey string `envconfig:"TRACING_GC_PRIVATE_KEY"`
PrivateKeyID string `envconfig:"TRACING_GC_PRIVATE_ID"`
}
// AppdashTracing holds the Appdash tracing configuration
type AppdashTracing struct {
DSN string `envconfig:"TRACING_APPDASH_DSN"`
URL string `envconfig:"TRACING_APPDASH_URL"`
}
// Tracing represents the distributed tracing configuration
type Tracing struct {
GoogleCloudTracing GoogleCloudTracing `mapstructure:"googleCloud"`
AppdashTracing AppdashTracing `mapstructure:"appdash"`
}
// IsGoogleCloudEnabled checks if google cloud is enabled
func (t Tracing) IsGoogleCloudEnabled() bool {
return len(t.GoogleCloudTracing.Email) > 0 && len(t.GoogleCloudTracing.PrivateKey) > 0 && len(t.GoogleCloudTracing.PrivateKeyID) > 0 && len(t.GoogleCloudTracing.ProjectID) > 0
}
// IsAppdashEnabled checks if appdash is enabled
func (t Tracing) IsAppdashEnabled() bool {
return len(t.AppdashTracing.DSN) > 0
}
func init() {
viper.SetDefault("port", "8080")
viper.SetDefault("tls.port", "8433")
viper.SetDefault("tls.redirect", true)
viper.SetDefault("backendFlushInterval", "20ms")
viper.SetDefault("database.dsn", "file:///etc/janus")
viper.SetDefault("storage.dsn", "memory://localhost")
viper.SetDefault("web.port", "8081")
viper.SetDefault("web.tls.port", "8444")
viper.SetDefault("web.tls.redisrect", true)
viper.SetDefault("web.credentials.algorithm", "HS256")
viper.SetDefault("web.credentials.basic.users", map[string]string{
"admin": "admin",
})
viper.SetDefault("stats.dsn", "log://")
viper.SetDefault("stats.errorsSection", "error-log")
viper.SetDefault("prometheus.metricPath", "/metrics")
logging.InitDefaults(viper.GetViper(), "log")
}
//Load configuration variables
func Load(configFile string) (*Specification, error) {
if configFile != "" {
viper.SetConfigFile(configFile)
} else {
viper.SetConfigName("janus")
viper.AddConfigPath("/etc/janus")
viper.AddConfigPath(".")
}
if err := viper.ReadInConfig(); err != nil {
return nil, errors.Wrap(err, "No config file found")
}
var config Specification
if err := viper.Unmarshal(&config); err != nil {
return nil, err
}
return &config, nil
}
//LoadEnv loads configuration from environment variables
func LoadEnv() (*Specification, error) {
var config Specification
// ensure the defaults are loaded
if err := viper.Unmarshal(&config); err != nil {
return nil, err
}
err := envconfig.Process("", &config)
if err != nil {
return nil, err
}
return &config, nil
}