-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
121 lines (100 loc) · 3.38 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"log/slog"
"strings"
"time"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
type Config struct {
Server Server `mapstructure:"server"`
SocketMaps map[string]Request `mapstructure:"socket_maps"`
PolicyServices map[string]Request `mapstructure:"policy_services"`
}
type Server struct {
Listen []Listen `mapstructure:"listen"`
Logging Logging `mapstructure:"logging"`
HTTPClient HTTPClient `mapstructure:"http_client"`
TLS TLS `mapstructure:"tls"`
SockmapMaxReplySize int `mapstructure:"socketmap_max_reply_size"`
}
type Listen struct {
Kind string `mapstructure:"kind"`
Name string `mapstructure:"name"`
Type string `mapstructure:"type"`
Address string `mapstructure:"address"`
Port int `mapstructure:"port"`
Mode string `mapstructure:"mode"`
}
type Logging struct {
JSON bool `mapstructure:"json"`
Level string `mapstructure:"level"`
}
type HTTPClient struct {
MaxConnsPerHost int `mapstructure:"max_connections_per_host"`
MaxIdleConns int `mapstructure:"max_idle_connections"`
MaxIdleConnsPerHost int `mapstructure:"max_idle_connections_per_host"`
IdleConnTimeout time.Duration `mapstructure:"idle_connection_timeout"`
Proxy string `mapstructure:"proxy"`
}
type TLS struct {
Enabled bool `mapstructure:"enabled"`
Cert string `mapstructure:"cert"`
Key string `mapstructure:"key"`
SkipVerify bool `mapstructure:"http_client_skip_verify"`
}
type Request struct {
Target string `mapstructure:"target"`
CustomHeaders []string `mapstructure:"custom_headers"`
Payload string `mapstructure:"payload"`
StatusCode int `mapstructure:"status_code"`
ValueField string `mapstructure:"value_field"`
ErrorField string `mapstructure:"error_field"`
NoErrorValue string `mapstructure:"no_error_value"`
}
func (cfg *Config) HandleConfig() error {
if cfg.Server.SockmapMaxReplySize <= 0 {
cfg.Server.SockmapMaxReplySize = 100000
}
return viper.Unmarshal(cfg)
}
func NewConfigFile() (cfg *Config, err error) {
cfg = &Config{}
// Define command-line flags for config file and format
pflag.String("config", "", "Path to the configuration file")
pflag.String("format", "yaml", "Format of the configuration file (e.g., yaml, json, toml)")
pflag.Parse()
// Bind flags to Viper
err = viper.BindPFlags(pflag.CommandLine)
if err != nil {
return nil, err
}
// Read values from the flags
configPath := viper.GetString("config")
configFormat := viper.GetString("format")
// Use the passed --config and --format values
if configPath != "" {
viper.SetConfigFile(configPath)
viper.SetConfigType(configFormat)
} else {
// Default case: look up in standard paths
viper.SetConfigName("pfxhttp")
viper.SetConfigType(configFormat)
viper.AddConfigPath("/usr/local/etc/pfxhttp/")
viper.AddConfigPath("/etc/pfxhttp/")
viper.AddConfigPath("$HOME/.pfxhttp")
viper.AddConfigPath(".")
}
// Attempt to read configuration
err = viper.ReadInConfig()
if err != nil {
slog.Info("No configuration file found")
}
// Enable reading environment variables
viper.AutomaticEnv()
viper.SetEnvPrefix("PFXHTTP")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
// Parse the configuration into the struct
err = cfg.HandleConfig()
return cfg, err
}