-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
95 lines (75 loc) · 1.88 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
89
90
91
92
93
94
95
package main
import (
"fmt"
"net/url"
"github.com/spf13/viper"
"errors"
"os"
"strings"
)
type proxyConfig struct {
address string
port int
username string
password string
targetUrl *url.URL
verboseLogs bool
tlsCertFile string
tlsKeyFile string
}
func init(){
viper.SetDefault("tlsCert","")
viper.SetDefault("tlsKey","")
viper.SetDefault("targetUrl","")
viper.SetDefault("port","8080")
viper.SetDefault("address","localhost")
viper.SetDefault("username","")
viper.SetDefault("password","")
viper.SetDefault("debug","false")
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer("-","_") )
}
func parseProxyConfig() (*proxyConfig, error){
var (
address = viper.GetString("address")
port = viper.GetInt("port")
username = viper.GetString("username")
password = viper.GetString("password")
targetUrl = viper.GetString("targetUrl")
verboseLogs = viper.GetBool("debug")
tlsCertFile = viper.GetString("tlsCert")
tlsKeyFile = viper.GetString("tlsKey")
uri *url.URL
err error
)
if len(targetUrl) == 0 {
return nil,errors.New("targetUrl cannot be empty")
}
if len(username) == 0 {
return nil,errors.New("username cannot be empty")
}
if len(password) == 0 {
return nil,errors.New("password cannot be empty")
}
if _, err := os.Stat(tlsKeyFile);len(tlsKeyFile) > 0 && os.IsNotExist(err) {
return nil,fmt.Errorf("The file %s does not exist",tlsKeyFile)
}
if _, err := os.Stat(tlsCertFile);len(tlsCertFile) > 0 && os.IsNotExist(err) {
return nil,fmt.Errorf("The file %s does not exist",tlsCertFile)
}
// Configure reverse proxy
if uri, err = url.ParseRequestURI(targetUrl); err != nil {
return nil, err
}
rt := proxyConfig{
address: address,
port:port,
username:username,
password:password,
targetUrl:uri,
verboseLogs:verboseLogs,
tlsCertFile:tlsCertFile,
tlsKeyFile:tlsKeyFile,
}
return &rt, nil
}