-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.go_bak
201 lines (186 loc) · 8.8 KB
/
config.go_bak
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
195
196
197
198
199
200
201
package evo
import (
"context"
"fmt"
"github.com/creasty/defaults"
"github.com/getevo/evo-ng/lib/args"
consul_api "github.com/hashicorp/consul/api"
vault_api "github.com/hashicorp/vault/api"
"github.com/heetch/confita"
"github.com/heetch/confita/backend/consul"
"github.com/heetch/confita/backend/env"
"github.com/heetch/confita/backend/file"
"github.com/heetch/confita/backend/flags"
"github.com/heetch/confita/backend/vault"
"reflect"
"time"
)
type Configuration struct {
WebServer struct {
Bind string `default:"0.0.0.0" config:"bind" json:"bind"`
Port string `default:"8080" config:"port" json:"port"`
StrictRouting bool `default:"false" config:"strict_routing" json:"strict_routing"`
ServerHeader string `default:"evo-ng" config:"server_header" json:"server_header"`
ETag bool `default:"true" config:"etag" json:"etag"`
Immutable bool `default:"false" config:"immutable" json:"immutable"`
UnescapePath bool `default:"false" config:"unescape_path" json:"unescape_path"`
Recover bool `default:"true" config:"recover" json:"recover"`
BodyLimit string `default:"4mb" config:"body_limit" json:"body_limit"`
ProxyHeader string `default:"x-forwarded-for" config:"proxy_header" json:"proxy_header"`
DisableKeepalive bool `default:"true" config:"disable_keepalive" json:"disable_keepalive"`
DisablePreParseMultipartForm bool `default:"false" config:"disable_pre_parse_multipart_form" json:"disable_pre_parse_multipart_form"`
StaticDir []string `default:"[\"./web\"]" config:"static_dir" json:"static_dir"`
ReadTimeout time.Duration `default:"0" config:"read_timeout" json:"read_timeout"`
WriteTimeout time.Duration `default:"0" config:"write_timeout" json:"write_timeout"`
IdleTimeout time.Duration `default:"0" config:"idle_timeout" json:"idle_timeout"`
ReadBufferSize int `default:"4096" config:"read_buffer_size" json:"read_buffer_size"`
WriteBufferSize int `default:"4096" config:"write_buffer_size" json:"write_buffer_size"`
CORS bool `default:"false" config:"cors_control" json:"cors_control"`
AllowOrigins string `default:"" config:"allow_origins" json:"allow_origins"`
AllowHeaders string `default:"" config:"allow_headers" json:"allow_headers"`
AllowMethods string `default:"" config:"allow_methods" json:"allow_methods"`
AllowCredentials bool `default:"false" config:"allow_credentials" json:"allow_credentials"`
ExposeHeaders string `default:"" config:"expose_headers" json:"expose_headers"`
PreflightMaxCacheAge int `default:"0" config:"preflight_max_cache_age" json:"preflight_max_cache_age"`
Debug bool `default:"true" json:"debug" config:"debug"`
CompressLevel int `default:"-1" json:"compress_level" config:"compress_level"`
WebSocket bool `default:"false" json:"websocket" config:"websocket"`
} `config:"web_server" json:"web_server"`
Database struct {
Enabled bool `default:"true" config:"enabled" json:"enabled"`
Dialect string `default:"mysql" config:"dialect" json:"dialect"`
DSN string `default:"user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local" config:"dsn" json:"dsn"`
Database string `default:"dbname" config:"database" json:"database"`
Replicas []string `config:"replicas" json:"replicas"`
SkipDefaultTransaction bool `default:"false" config:"skip_default_transaction" json:"skip_default_transaction"`
FullSaveAssociations bool `default:"true" config:"full_save_associations" json:"full_save_associations"`
DisableAutomaticPing bool `default:"false" config:"disable_automatic_ping" json:"disable_automatic_ping"`
DisableForeignKeyConstraintWhenMigrating bool `default:"false" config:"disable_foreign_key_constraint_when_migrating" json:"disable_foreign_key_constraint_when_migrating"`
DisableNestedTransaction bool `default:"false" config:"disable_nested_transaction" json:"disable_nested_transaction"`
CreateBatchSize int `default:"100" config:"create_batch_size" json:"create_batch_size"`
QueryFields bool `default:"true" config:"query_fields" json:"query_fields"`
StmtCache bool `default:"true" config:"stmt_cache" json:"stmt_cache"`
TablePrefix string `default:"" config:"table_prefix" json:"table_prefix"`
Cache string `config:"cache" json:"cache"`
CacheSize string `config:"cache_size" json:"cache_size"`
Debug string `config:"debug" json:"debug"`
MaxOpenConns int `default:"100" config:"max_open_connections" json:"max_open_connections"`
MaxIdleConns int `default:"10" config:"max_idle_connections" json:"max_idle_connections"`
ConnMaxLifeTime string `default:"1h" config:"connection_max_lifetime" json:"connection_max_lifetime"`
ConnMaxIdleTime string `default:"10m" config:"connection_max_idle_time" json:"connection_max_idle_time"`
} `config:"database" json:"database"`
}
func (c Configuration) Default() Configuration {
if err := defaults.Set(&c); err != nil {
Panic(err)
}
return c
}
func ParseConfig(params ...interface{}) error {
var path = ""
var out int
for i, item := range params {
switch val := item.(type) {
case string:
if path == "" {
path = val
}
default:
if reflect.TypeOf(val).Kind() == reflect.Struct {
out = i
}
}
}
if path == "" {
if v := args.Get("-c"); v != "" {
path = v
} else {
path = "config.yml"
}
}
var loader *confita.Loader
if args.Get("-consul") != "" {
fmt.Println("Load config from consul")
var cfg = consul_api.DefaultConfig()
if args.Get("-consul") != "default" {
cfg.Address = args.Get("-consul")
}
if args.Get("-consul-scheme") != "" {
cfg.Scheme = args.Get("-consul-scheme")
}
if args.Get("-consul-datacenter") != "" {
cfg.Datacenter = args.Get("-consul-datacenter")
}
if args.Get("-consul-token") != "" {
cfg.Token = args.Get("-consul-token")
}
if args.Get("-consul-token-file") != "" {
cfg.TokenFile = args.Get("-consul-token-file")
}
if args.Get("-consul-username") != "" {
cfg.HttpAuth = &consul_api.HttpBasicAuth{
Username: args.Get("-consul-username"),
Password: args.Get("-consul-password"),
}
}
client, err := consul_api.NewClient(cfg)
if err == nil {
return err
}
loader = confita.NewLoader(
env.NewBackend(),
consul.NewBackend(client),
flags.NewBackend(),
)
} else if args.Get("-etcd") != "" {
fmt.Println("Load config from etcd")
/* client, err := clientv3.New(clientv3.Config{
Username: args.Get("-etcd-username"),
Password: args.Get("-etcd-password"),
Endpoints: strings.Split(args.Get("-etcd"), ","),
})
if err == nil {
return err
}
loader = confita.NewLoader(
etcd.NewBackend(client, etcd.WithPrefix("-etcd-prefix")),
)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
loader.Load(ctx, params[out])*/
} else if args.Get("-vault") != "" {
var cfg = vault_api.DefaultConfig()
if args.Get("-vault") != "default" {
cfg.Address = args.Get("-vault")
}
if args.Get("-vault-agent-address") != "" {
cfg.AgentAddress = args.Get("-vault-agent-address")
}
client, err := vault_api.NewClient(cfg)
if err == nil {
return err
}
if args.Get("-vault-version") == "1" {
loader = confita.NewLoader(
env.NewBackend(),
vault.NewBackend(client.Logical(), args.Get("-vault-path")),
flags.NewBackend(),
)
} else {
loader = confita.NewLoader(
env.NewBackend(),
vault.NewBackendV2(client.Logical(), args.Get("-vault-path")),
flags.NewBackend(),
)
}
} else {
loader = confita.NewLoader(
env.NewBackend(),
file.NewBackend(path),
)
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
var err = loader.Load(ctx, params[out])
return err
}