-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
70 lines (59 loc) · 1.44 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
package main
import (
"bufio"
"fmt"
"os"
"reflect"
"unicode"
"github.com/naoina/toml"
"github.com/node-real/go-pkg/mysqlclient"
"github.com/bnb-chain/bsc-staking-indexer/cc"
"github.com/bnb-chain/bsc-staking-indexer/indexer"
)
type Config struct {
Indexer indexer.Config
Store mysqlclient.Config
CC cc.Config
Log LogConfig
}
func LoadConfig(file string) *Config {
f, err := os.Open(file)
if err != nil {
panic(err)
}
defer f.Close()
cfg := defaultConfig
err = tomlSettings.NewDecoder(bufio.NewReader(f)).Decode(&cfg)
// Add file name to errors that have a line number.
if _, ok := err.(*toml.LineError); ok {
panic(err)
}
return &cfg
}
// TomlSettings - These settings ensure that TOML keys use the same names as Go struct fields.
var tomlSettings = toml.Config{
NormFieldName: func(rt reflect.Type, key string) string {
return key
},
FieldToKey: func(rt reflect.Type, field string) string {
return field
},
MissingField: func(rt reflect.Type, field string) error {
link := ""
if unicode.IsUpper(rune(rt.Name()[0])) && rt.PkgPath() != "main" {
link = fmt.Sprintf(", see https://godoc.org/%s#%s for available fields", rt.PkgPath(), rt.Name())
}
_, _ = fmt.Fprintf(os.Stderr, "field '%s' is not defined in %s%s\n", field, rt.String(), link)
return nil
},
}
type LogConfig struct {
RootDir string
Level string
}
var defaultConfig = Config{
Log: LogConfig{
RootDir: "./logs",
Level: "debug",
},
}