forked from agntcy/dir
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
54 lines (41 loc) · 1.19 KB
/
Copy pathconfig.go
File metadata and controls
54 lines (41 loc) · 1.19 KB
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
// Copyright AGNTCY Contributors (https://github.com/agntcy)
// SPDX-License-Identifier: Apache-2.0
package client
import (
"fmt"
"strings"
"github.com/mitchellh/mapstructure"
"github.com/spf13/viper"
)
const (
DefaultEnvPrefix = "DIRECTORY_CLIENT"
DefaultServerAddress = "0.0.0.0:8888"
)
var DefaultConfig = Config{
ServerAddress: DefaultServerAddress,
}
type Config struct {
ServerAddress string `json:"server_address,omitempty" mapstructure:"server_address"`
}
func LoadConfig() (*Config, error) {
v := viper.NewWithOptions(
viper.KeyDelimiter("."),
viper.EnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_")),
)
v.SetEnvPrefix(DefaultEnvPrefix)
v.AllowEmptyEnv(true)
v.AutomaticEnv()
_ = v.BindEnv("server_address")
v.SetDefault("server_address", DefaultServerAddress)
// Load configuration into struct
decodeHooks := mapstructure.ComposeDecodeHookFunc(
mapstructure.TextUnmarshallerHookFunc(),
mapstructure.StringToTimeDurationHookFunc(),
mapstructure.StringToSliceHookFunc(","),
)
config := &Config{}
if err := v.Unmarshal(config, viper.DecodeHook(decodeHooks)); err != nil {
return nil, fmt.Errorf("failed to load configuration: %w", err)
}
return config, nil
}