-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappstate.go
More file actions
132 lines (113 loc) · 3.54 KB
/
Copy pathappstate.go
File metadata and controls
132 lines (113 loc) · 3.54 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
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
// SPDX-License-Identifier: Apache-2.0
//
// Copyright 2025 NASD Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package appstate
import (
"fmt"
"log/slog"
"net/http"
"os"
"strings"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/phsym/console-slog"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"jester.noble.xyz/v2/ethereum"
"jester.noble.xyz/v2/metrics"
)
// appState is the modifiable state of the application.
type AppState struct {
Home string
Log *slog.Logger
Viper *viper.Viper
Mux *http.ServeMux
Metrics *metrics.PrometheusMetrics
Config *Config
Key keyring.Keyring
cdc *codec.ProtoCodec
*ethereum.Eth
}
func (a *AppState) InitLogger() {
viper := a.Viper
var level slog.Level
logLevel := strings.ToLower(viper.GetString(FlagLogLevel))
switch logLevel {
case "debug":
level = slog.LevelDebug
case "info":
level = slog.LevelInfo
case "warn":
level = slog.LevelWarn
case "error":
level = slog.LevelError
default:
fmt.Printf("\ninvalid log_level (%s); using 'info", logLevel)
level = slog.LevelInfo
}
opts := &slog.HandlerOptions{Level: level}
var logHandler slog.Handler
logStyle := strings.ToLower(viper.GetString(FlagLogStyle))
switch logStyle {
case "text":
logHandler = slog.NewTextHandler(os.Stderr, opts)
case "json":
logHandler = slog.NewJSONHandler(os.Stderr, opts)
case "pretty":
logHandler = console.NewHandler(os.Stderr, &console.HandlerOptions{Level: level})
default:
fmt.Printf("\ninvalid log_style (%s); using 'pretty'", logStyle)
logHandler = console.NewHandler(os.Stderr, &console.HandlerOptions{Level: level})
}
a.Log = slog.New(logHandler)
}
func (a *AppState) ConfigureViper(cmd *cobra.Command) {
viper := a.Viper
// bindAllFlags binds all flags to viper (both local and persistent).
cmd.Flags().VisitAll(func(f *pflag.Flag) {
if err := viper.BindPFlag(f.Name, cmd.Flags().Lookup(f.Name)); err != nil {
panic(err)
}
})
a.Home = viper.GetString(FlagHome)
viper.AddConfigPath(a.Home)
viper.SetConfigType("toml")
viper.SetConfigName("config")
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
viper.AutomaticEnv() // after reading in config, check for matching env vars
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_", ".", "_"))
}
// loadConfigFile reads configuration from file, env, OR args into a.Config
// This allows access to configuration via the appstate instead of using viper.
func (a *AppState) LoadConfig() {
viper := a.Viper
a.Config = &Config{
Log_level: viper.GetString(FlagLogLevel),
Log_style: viper.GetString(FlagLogStyle),
Testnet: viper.GetBool(flagTestnet),
ServerAddress: viper.GetString(flagServerAddr),
Metrics: &Metrics{
Enabled: viper.GetBool(flagMetricsEnabled),
Address: viper.GetString(flagMetricsAddress),
},
Ethereum: &Ethereum{
WebsocketURL: viper.GetString(flagEthWebsocket),
RPCURL: viper.GetString(flagEthRPC),
},
}
}