-
Notifications
You must be signed in to change notification settings - Fork 12
/
config.go
84 lines (74 loc) · 2.08 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
package config
import (
"errors"
"fmt"
"os"
"github.com/spf13/viper"
)
// Init configure and check environment configuration
func Init() viper.Viper {
// Configure defaults
if len(os.Getenv("PORT")) == 0 {
err := os.Setenv("PORT", "8080")
if err != nil {
fmt.Println("Error setting PORT environment variable")
}
}
config := viper.New()
config.SetEnvPrefix("GSH")
config.AutomaticEnv()
return *config
}
// Check verify configuration
func Check(config viper.Viper) error {
var fails uint
// Check envs
if len(os.Getenv("PORT")) == 0 {
fmt.Printf("Environment variable PORT not defined\n")
fails++
}
if len(config.GetString("AUTH_TYPE")) == 0 {
fmt.Printf("Environment variable GSH_AUTH_TYPE not defined\n")
fails++
}
if config.GetString("AUTH_TYPE") == "OPENID" {
if len(config.GetString("AUTH_REALM_URL")) == 0 {
fmt.Println("Environment variable GSH_AUTH_REALM_URL not defined")
fails++
}
if len(config.GetString("AUTH_REDIRECT")) == 0 {
fmt.Println("Environment variable GSH_AUTH_REDIRECT not defined")
fails++
}
if len(config.GetString("AUTH_RESOURCE")) == 0 {
fmt.Println("Environment variable GSH_AUTH_RESOURCE not defined")
fails++
}
if len(config.GetString("AUTH_CREDENTIALS_SECRET")) == 0 {
fmt.Println("Environment variable GSH_AUTH_CREDENTIALS_SECRET not defined")
fails++
}
if len(config.GetString("AUTH_USERNAME_CLAIM")) == 0 {
fmt.Println("Environment variable GSH_AUTH_USERNAME_CLAIM not defined")
fails++
}
}
if config.GetString("SESSION_STORE") == "COOKIE" {
if len(config.GetString("SESSION_STORE_AUTHENTICATION_SECRET")) == 0 {
fmt.Println("Environment variable GSH_SESSION_STORE_AUTHENTICATION_SECRET not defined")
fails++
}
if len(config.GetString("SESSION_STORE_ENCRYPTION_SECRET")) == 0 {
fmt.Println("Environment variable GSH_SESSION_STORE_ENCRYPTION_SECRET not defined")
fails++
}
}
if len(config.GetString("API_ENDPOINT")) == 0 {
fmt.Println("Environment variable GSH_API_ENDPOINT not defined")
fails++
}
if fails > 0 {
return errors.New("Configuration error")
}
return nil
}