@@ -2,19 +2,50 @@ package config
2
2
3
3
import (
4
4
"crypto/ecdsa"
5
+ "encoding/json"
5
6
"fantom-api-graphql/internal/types"
6
7
"flag"
7
8
"fmt"
8
9
"github.com/ethereum/go-ethereum/common"
9
10
"github.com/ethereum/go-ethereum/crypto"
10
11
"github.com/mitchellh/mapstructure"
11
12
"github.com/spf13/viper"
13
+ "io/ioutil"
12
14
"log"
15
+ "os"
13
16
"reflect"
14
17
)
15
18
16
19
// Load provides a loaded configuration for Fantom API server.
17
20
func Load () (* Config , error ) {
21
+ // Get the config reader
22
+ cfg , err := readConfigFile ()
23
+ if err != nil {
24
+ return nil , err
25
+ }
26
+
27
+ // prep the container and try to unmarshal
28
+ // the config file into the config structure
29
+ var config Config
30
+ if err = cfg .Unmarshal (& config , setupConfigUnmarshaler ); err != nil {
31
+ log .Println ("can not extract API server configuration" )
32
+ log .Println (err .Error ())
33
+ return nil , err
34
+ }
35
+
36
+ // try to load the logo map file
37
+ loadErc20LogMap (& config )
38
+
39
+ // return the final config
40
+ return & config , nil
41
+ }
42
+
43
+ // readConfigFile reads the config file and provides instance
44
+ // of the loaded configuration.
45
+ func readConfigFile () (* viper.Viper , error ) {
46
+ // inform about tokens loading
47
+ log .Printf ("loading app configuration" )
48
+
18
49
// Get the config reader
19
50
cfg := reader ()
20
51
@@ -23,27 +54,56 @@ func Load() (*Config, error) {
23
54
24
55
// Try to read the file
25
56
if err := cfg .ReadInConfig (); err != nil {
26
- if _ , ok := err .(viper.ConfigFileNotFoundError ); ok {
27
- // Config file not found; ignore the error, we may not need the config file
28
- log .Print ("configuration file not found, using default values" )
29
- } else {
57
+ // is this an error notifying missing config file?
58
+ if _ , ok := err .(viper.ConfigFileNotFoundError ); ! ok {
30
59
// Config file was found but another error was produced
31
60
log .Printf ("can not read the server configuration" )
32
61
return nil , err
33
62
}
63
+
64
+ // Config file not found; ignore the error, we may not need the config file
65
+ log .Print ("configuration file not found, using default values" )
34
66
}
35
67
36
- // prep the container and try to unmarshal
37
- // the config file into the config structure
38
- var config Config
39
- err := cfg .Unmarshal (& config , setupConfigUnmarshaler )
68
+ return cfg , nil
69
+ }
70
+
71
+ // loadErc20LogMap loads the map of ERC20 token logos.
72
+ func loadErc20LogMap (cfg * Config ) {
73
+ // is there any path at all?
74
+ if cfg .TokenLogoFilePath == "" {
75
+ log .Print ("ERC20 tokens map file path not available" )
76
+ return
77
+ }
78
+
79
+ // try to open the file
80
+ f , err := os .Open (cfg .TokenLogoFilePath )
40
81
if err != nil {
41
- log .Println ("can not extract API server configuration" )
42
- log .Println (err .Error ())
43
- return nil , err
82
+ log .Printf ("can not open ERC20 tokens map file; %s" , err .Error ())
83
+ return
44
84
}
45
85
46
- return & config , nil
86
+ // make sure to close the file
87
+ defer f .Close ()
88
+
89
+ // inform about tokens loading
90
+ log .Printf ("loading ERC20 tokens from %s" , cfg .TokenLogoFilePath )
91
+
92
+ // read the whole file
93
+ data , err := ioutil .ReadAll (f )
94
+ if err != nil {
95
+ log .Printf ("can not read ERC20 tokens map file; %s" , err .Error ())
96
+ return
97
+ }
98
+
99
+ // try to unmarshal the data
100
+ if err := json .Unmarshal (data , & cfg .TokenLogo ); err != nil {
101
+ log .Printf ("can not decode ERC20 tokens map file; %s" , err .Error ())
102
+ return
103
+ }
104
+
105
+ // inform about tokens
106
+ log .Printf ("found %d ERC20 tokens" , len (cfg .TokenLogo ))
47
107
}
48
108
49
109
// setupConfigUnmarshaler configures the Config loader to properly unmarshal
0 commit comments