-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
98 lines (83 loc) · 2.57 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
96
97
98
package main
import (
_ "embed"
"errors"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/labstack/echo/v4"
jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
// LATER: move to be embedded
//go:embed rdap-proxy-default.yaml
var defaultConfig string
var (
whoisMap map[string]string
redirectMap map[string]string
redirectStatus int
timeout time.Duration
port int
devMode bool
bindHost string
allowed []string
allowedSet map[string]bool
)
func loadConfig() {
jww.SetLogThreshold(jww.LevelTrace)
jww.SetStdoutThreshold(jww.LevelInfo)
viper.SetDefault("redirectStatus", http.StatusTemporaryRedirect)
viper.SetDefault("timeout", "10s")
viper.SetDefault("port", "4000")
viper.SetDefault("dev", "false")
viper.SetDefault("bind", "0.0.0.0")
viper.BindEnv("port", "PORT")
viper.BindEnv("allowed", "ALLOWED")
viper.BindEnv("bind", "BIND")
viper.BindEnv("dev", "DEV")
//viper.AddConfigPath("/etc")
viper.AddConfigPath("/etc")
viper.SetConfigFile("rdap-proxy-default.yaml")
//viper.SetConfigType("yaml")
err := viper.ReadInConfig()
if err != nil {
if errors.Is(err, os.ErrNotExist) {
log.Printf("config: using default (%s)", err)
viper.ReadConfig(strings.NewReader(defaultConfig))
} else {
log.Fatalf("Unable to load config: %s", err)
}
} else {
log.Printf("config: loaded from %s", viper.ConfigFileUsed())
}
pflag.BoolVar(&devMode, "dev", false, "Run in development mode")
pflag.IntVar(&port, "port", viper.GetInt("port"), "Port to run on")
pflag.StringVar(&bindHost, "bind", viper.GetString("bind"), "Network to bind to, usually either localhost (for development) or 0.0.0.0 (default, for production)")
pflag.StringSliceVar(&allowed, "allowed", viper.GetStringSlice("allowed"), "List of allowed TLDs")
pflag.Parse()
viper.BindPFlags(pflag.CommandLine)
whoisMap = viper.GetStringMapString("whois")
redirectMap = viper.GetStringMapString("redirect")
redirectStatus = viper.GetInt("redirectStatus")
var timeoutErr error
timeout, timeoutErr = time.ParseDuration(viper.GetString("timeout"))
if timeoutErr != nil {
timeout = time.Duration(10) * time.Second
}
devMode = viper.GetBool("dev")
allowedSet = make(map[string]bool)
for allowedTld := range allowed {
allowedSet[allowed[allowedTld]] = true
}
log.Printf("devmode: %s", viper.GetString("dev"))
//LATER
//res, _ := json.Marshal(viper.AllSettings())
//log.Printf("whois: %s", res)
}
// just for debugging
func configHandler(c echo.Context) error {
return c.JSONPretty(200, viper.AllSettings(), " ")
}