Skip to content

Commit

Permalink
refacto app using kilnfi/go-utils/app
Browse files Browse the repository at this point in the history
  • Loading branch information
MattKetmo committed Feb 3, 2023
1 parent d786ee1 commit 4df7364
Show file tree
Hide file tree
Showing 19 changed files with 1,014 additions and 503 deletions.
25 changes: 25 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cmd

import (
"context"

kilnutils "github.com/kilnfi/go-utils/cmd/utils"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func NewRootCommand() *cobra.Command {
v := viper.New()

ctx := context.Background()
ctx = kilnutils.WithViper(ctx, v)

cmd := &cobra.Command{
Use: "near-exporter",
}

cmd.SetContext(ctx)
cmd.AddCommand(NewRunCommand())

return cmd
}
54 changes: 54 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package cmd

import (
"fmt"
"net/http"
"os"
"strings"
"time"

"github.com/kilnfi/near-exporter/pkg/app"
"github.com/kilnfi/near-exporter/pkg/near"
"github.com/spf13/cobra"
)

func NewRunCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "run",
Short: "Run exporter",
RunE: func(cmd *cobra.Command, args []string) error {
rpcAddr := os.Getenv("NEAR_EXPORTER_RPC_ADDR")
if rpcAddr == "" {
return fmt.Errorf("missing env var NEAR_EXPORTER_RPC_ADDR")
}
trackedAccounts := strings.Split(
os.Getenv("NEAR_EXPORTER_TRACKED_ACCOUNTS"), ",",
)

client := near.NewClient(rpcAddr, &http.Client{
Timeout: time.Duration(10 * time.Second),
})

config := &app.Config{
TrackedAccounts: trackedAccounts,
RefreshRate: 15 * time.Second,
}

app, err := app.NewApp(config, client)
if err != nil {
return err
}

app.Logger().SetOutput(cmd.OutOrStderr())

app.Logger().
WithField("rpc_addr", rpcAddr).
WithField("tracked_account", trackedAccounts).
Info("config")

return app.Run()
},
}

return cmd
}
302 changes: 0 additions & 302 deletions collector/collector.go

This file was deleted.

Loading

0 comments on commit 4df7364

Please sign in to comment.