cli.go 2.65 KB
Newer Older
Will Cory's avatar
Will Cory committed
1 2 3
package cli

import (
4
	"context"
Will Cory's avatar
Will Cory committed
5
	"fmt"
6
	"strconv"
Will Cory's avatar
Will Cory committed
7

8
	"github.com/ethereum-optimism/optimism/indexer"
9
	"github.com/ethereum-optimism/optimism/indexer/api"
Will Cory's avatar
Will Cory committed
10
	"github.com/ethereum-optimism/optimism/indexer/config"
11
	"github.com/ethereum-optimism/optimism/indexer/database"
12 13
	"github.com/ethereum-optimism/optimism/op-service/log"
	"github.com/ethereum-optimism/optimism/op-service/opio"
Will Cory's avatar
Will Cory committed
14
	"github.com/ethereum/go-ethereum/params"
15

Will Cory's avatar
Will Cory committed
16 17 18 19 20 21 22 23 24 25 26 27
	"github.com/urfave/cli/v2"
)

type Cli struct {
	GitVersion string
	GitCommit  string
	GitDate    string
	app        *cli.App
	Flags      []cli.Flag
}

func runIndexer(ctx *cli.Context) error {
28
	logger := log.NewLogger(log.ReadCLIConfig(ctx))
29

Will Cory's avatar
Will Cory committed
30
	configPath := ctx.String(ConfigFlag.Name)
31
	cfg, err := config.LoadConfig(logger, configPath)
32
	if err != nil {
33
		logger.Error("failed to load config", "err", err)
34 35
		return err
	}
Will Cory's avatar
Will Cory committed
36

37 38 39 40 41 42
	db, err := database.NewDB(cfg.DB)

	if err != nil {
		return err
	}

43
	indexer, err := indexer.NewIndexer(logger, cfg.Chain, cfg.RPCs, db)
Will Cory's avatar
Will Cory committed
44
	if err != nil {
45
		return err
Will Cory's avatar
Will Cory committed
46 47
	}

Hamdi Allam's avatar
Hamdi Allam committed
48
	indexerCtx, indexerCancel := context.WithCancel(context.Background())
49
	go func() {
50
		opio.BlockOnInterrupts()
Hamdi Allam's avatar
Hamdi Allam committed
51
		indexerCancel()
52 53 54
	}()

	return indexer.Run(indexerCtx)
Will Cory's avatar
Will Cory committed
55 56 57
}

func runApi(ctx *cli.Context) error {
58
	logger := log.NewLogger(log.ReadCLIConfig(ctx))
Will Cory's avatar
Will Cory committed
59

60
	configPath := ctx.String(ConfigFlag.Name)
61
	cfg, err := config.LoadConfig(logger, configPath)
Will Cory's avatar
Will Cory committed
62
	if err != nil {
63 64
		logger.Error("failed to load config", "err", err)
		return err
Will Cory's avatar
Will Cory committed
65
	}
66

67 68 69 70 71 72 73
	db, err := database.NewDB(cfg.DB)

	if err != nil {
		logger.Crit("Failed to connect to database", "err", err)
	}

	server := api.NewApi(db.BridgeTransfers, logger)
74

75
	return server.Listen(strconv.Itoa(cfg.API.Port))
Will Cory's avatar
Will Cory committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
}

var (
	ConfigFlag = &cli.StringFlag{
		Name:    "config",
		Value:   "./indexer.toml",
		Aliases: []string{"c"},
		Usage:   "path to config file",
		EnvVars: []string{"INDEXER_CONFIG"},
	}
)

// make a instance method on Cli called Run that runs cli
// and returns an error
func (c *Cli) Run(args []string) error {
	return c.app.Run(args)
}

func NewCli(GitVersion string, GitCommit string, GitDate string) *Cli {
95
	flags := []cli.Flag{ConfigFlag}
Will Cory's avatar
Will Cory committed
96
	flags = append(flags, log.CLIFlags("INDEXER")...)
Will Cory's avatar
Will Cory committed
97 98
	app := &cli.App{
		Version:     fmt.Sprintf("%s-%s", GitVersion, params.VersionWithCommit(GitCommit, GitDate)),
Will Cory's avatar
Will Cory committed
99
		Description: "An indexer of all optimism events with a serving api layer",
Will Cory's avatar
Will Cory committed
100 101 102 103 104 105 106 107
		Commands: []*cli.Command{
			{
				Name:        "api",
				Flags:       flags,
				Description: "Runs the api service",
				Action:      runApi,
			},
			{
108
				Name:        "index",
Will Cory's avatar
Will Cory committed
109 110 111 112 113 114 115 116 117 118 119 120
				Flags:       flags,
				Description: "Runs the indexing service",
				Action:      runIndexer,
			},
		},
	}

	return &Cli{
		app:   app,
		Flags: flags,
	}
}