Skip to content

Commit 4df7364

Browse files
committed
refacto app using kilnfi/go-utils/app
1 parent d786ee1 commit 4df7364

19 files changed

Lines changed: 1014 additions & 503 deletions

cmd/root.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
6+
kilnutils "github.com/kilnfi/go-utils/cmd/utils"
7+
"github.com/spf13/cobra"
8+
"github.com/spf13/viper"
9+
)
10+
11+
func NewRootCommand() *cobra.Command {
12+
v := viper.New()
13+
14+
ctx := context.Background()
15+
ctx = kilnutils.WithViper(ctx, v)
16+
17+
cmd := &cobra.Command{
18+
Use: "near-exporter",
19+
}
20+
21+
cmd.SetContext(ctx)
22+
cmd.AddCommand(NewRunCommand())
23+
24+
return cmd
25+
}

cmd/run.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"os"
7+
"strings"
8+
"time"
9+
10+
"github.com/kilnfi/near-exporter/pkg/app"
11+
"github.com/kilnfi/near-exporter/pkg/near"
12+
"github.com/spf13/cobra"
13+
)
14+
15+
func NewRunCommand() *cobra.Command {
16+
cmd := &cobra.Command{
17+
Use: "run",
18+
Short: "Run exporter",
19+
RunE: func(cmd *cobra.Command, args []string) error {
20+
rpcAddr := os.Getenv("NEAR_EXPORTER_RPC_ADDR")
21+
if rpcAddr == "" {
22+
return fmt.Errorf("missing env var NEAR_EXPORTER_RPC_ADDR")
23+
}
24+
trackedAccounts := strings.Split(
25+
os.Getenv("NEAR_EXPORTER_TRACKED_ACCOUNTS"), ",",
26+
)
27+
28+
client := near.NewClient(rpcAddr, &http.Client{
29+
Timeout: time.Duration(10 * time.Second),
30+
})
31+
32+
config := &app.Config{
33+
TrackedAccounts: trackedAccounts,
34+
RefreshRate: 15 * time.Second,
35+
}
36+
37+
app, err := app.NewApp(config, client)
38+
if err != nil {
39+
return err
40+
}
41+
42+
app.Logger().SetOutput(cmd.OutOrStderr())
43+
44+
app.Logger().
45+
WithField("rpc_addr", rpcAddr).
46+
WithField("tracked_account", trackedAccounts).
47+
Info("config")
48+
49+
return app.Run()
50+
},
51+
}
52+
53+
return cmd
54+
}

collector/collector.go

Lines changed: 0 additions & 302 deletions
This file was deleted.

0 commit comments

Comments
 (0)