-
Notifications
You must be signed in to change notification settings - Fork 10
/
config.go
152 lines (130 loc) · 4.63 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
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
package main
import (
"encoding/json"
"flag"
"os"
"path"
"strings"
"github.com/omeid/uconfig"
"github.com/omeid/uconfig/plugins"
"github.com/omeid/uconfig/plugins/file"
"github.com/rs/zerolog/log"
"github.com/textileio/go-tableland/internal/tableland"
)
// configFilename is the filename of the config file automatically loaded.
var configFilename = "config.json"
type config struct {
Dir string // This will default to "", NOT the default dir value set via the flag package
BootstrapBackupURL string `default:"" env:"BOOTSTRAP_BACKUP_URL"`
HTTP HTTPConfig
Gateway GatewayConfig
TableConstraints TableConstraints
QueryConstraints QueryConstraints
Metrics struct {
Port string `default:"9090"`
}
Log struct {
Human bool `default:"false"`
Debug bool `default:"false"`
}
Analytics struct {
FetchExtraBlockInfo bool `default:"false"`
}
Backup BackupConfig
TelemetryPublisher TelemetryPublisherConfig
Chains []ChainConfig
}
// HTTPConfig contains configuration for the HTTP server serving APIs.
type HTTPConfig struct {
Port string `default:"8080"` // HTTP port (e.g. 8080)
TLSCert string `default:""`
TLSKey string `default:""`
RateLimInterval string `default:"1s"`
MaxRequestPerInterval uint64 `default:"10"`
APIKey string `default:""` // if client passes the key it will not be affected by rate limiter
}
// GatewayConfig contains configuration for the Gateway.
type GatewayConfig struct {
ExternalURIPrefix string `default:"https://testnets.tableland.network"`
MetadataRendererURI string `default:""`
AnimationRendererURI string `default:""`
}
// BackupConfig contains configuration for automatic database backups.
type BackupConfig struct {
Enabled bool `default:"true"`
Dir string `default:"backups"` // relative to dir path config (e.g. ${HOME}/.tableland/backups )
Frequency int `default:"120"` // in minutes
EnableVacuum bool `default:"true"`
EnableCompression bool `default:"true"`
Pruning struct {
Enabled bool `default:"true"`
KeepFiles int `default:"5"` // number of files to keep
}
}
// TelemetryPublisherConfig contains configuration attributes for the telemetry module.
type TelemetryPublisherConfig struct {
Enabled bool `default:"false"`
MetricsHubURL string `default:""`
MetricsHubAPIKey string `default:""`
PublishingInterval string `default:"10s"`
ChainStackCollectFrequency string `default:"15m"`
}
// TableConstraints describes contraints to be enforced for Tableland tables.
type TableConstraints struct {
MaxRowCount int `default:"100_000"`
}
// QueryConstraints describes constraints to be enforced on queries.
type QueryConstraints struct {
MaxWriteQuerySize int `default:"35000"`
MaxReadQuerySize int `default:"35000"`
}
// ChainConfig contains all the chain execution stack configuration for a particular EVM chain.
type ChainConfig struct {
Name string `default:""`
ChainID tableland.ChainID `default:"0"`
Registry struct {
EthEndpoint string `default:"eth_endpoint"`
ContractAddress string `default:"contract_address"`
ProviderAuthToken string `default:"provider_auth_token"`
}
EventFeed struct {
ChainAPIBackoff string `default:"15s"`
MinBlockDepth int `default:"5"`
NewBlockPollFreq string `default:"10s"`
PersistEvents bool `default:"true"`
}
EventProcessor struct {
BlockFailedExecutionBackoff string `default:"10s"`
DedupExecutedTxns bool `default:"false"`
WebhookURL string `default:""`
}
HashCalculationStep int64 `default:"1000"`
}
func setupConfig() (*config, string) {
flagDirPath := flag.String("dir", "${HOME}/.tableland", "Directory where the configuration and DB exist")
flag.Parse()
if flagDirPath == nil {
log.Fatal().Msg("--dir is null")
return nil, "" // Helping the linter know the next line is safe.
}
dirPath := os.ExpandEnv(*flagDirPath)
_ = os.MkdirAll(dirPath, 0o755)
var plugins []plugins.Plugin
fullPath := path.Join(dirPath, configFilename)
configFileBytes, err := os.ReadFile(fullPath)
if os.IsNotExist(err) {
log.Info().Str("config_file_path", fullPath).Msg("config file not found")
} else if err != nil {
log.Fatal().Str("config_file_path", fullPath).Err(err).Msg("opening config file")
} else {
fileStr := os.ExpandEnv(string(configFileBytes))
plugins = append(plugins, file.NewReader(strings.NewReader(fileStr), json.Unmarshal))
}
conf := &config{}
c, err := uconfig.Classic(&conf, file.Files{}, plugins...)
if err != nil {
c.Usage()
log.Fatal().Err(err).Msg("invalid configuration")
}
return conf, dirPath
}