github.com/iotexproject/iotex-core@v1.14.1-rc1/tools/bot/server/main.go (about)

     1  // Copyright (c) 2019 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package main
     7  
     8  import (
     9  	"context"
    10  	"flag"
    11  	"fmt"
    12  	"os"
    13  
    14  	_ "go.uber.org/automaxprocs"
    15  	"go.uber.org/zap"
    16  
    17  	"github.com/iotexproject/iotex-core/pkg/log"
    18  	"github.com/iotexproject/iotex-core/tools/bot/config"
    19  	"github.com/iotexproject/iotex-core/tools/bot/server/bot"
    20  )
    21  
    22  func init() {
    23  	flag.Usage = func() {
    24  		_, _ = fmt.Fprintf(os.Stderr,
    25  			"usage: bot -config-path=[string]\n")
    26  		flag.PrintDefaults()
    27  		os.Exit(2)
    28  	}
    29  	flag.Parse()
    30  }
    31  
    32  func main() {
    33  	cfg, err := config.New()
    34  	if err != nil {
    35  		fmt.Println("Failed to new config.", zap.Error(err))
    36  		return
    37  	}
    38  	err = initLogger(cfg)
    39  	if err != nil {
    40  		return
    41  	}
    42  	b, err := bot.NewServer(cfg)
    43  	if err != nil {
    44  		log.L().Fatal("new server:", zap.Error(err))
    45  	}
    46  
    47  	// transfer
    48  	transfer, err := bot.NewTransfer(cfg, "transfer")
    49  	if err != nil {
    50  		log.L().Fatal("new transfer:", zap.Error(err))
    51  	}
    52  
    53  	// xrc20
    54  	xrc20, err := bot.NewXrc20(cfg, "xrc20")
    55  	if err != nil {
    56  		log.L().Fatal("new xrc20 transfer:", zap.Error(err))
    57  	}
    58  
    59  	// multisend
    60  	multisend, err := bot.NewExecution(cfg, "multisend")
    61  	if err != nil {
    62  		log.L().Fatal("new multisend:", zap.Error(err))
    63  	}
    64  
    65  	err = b.Register(transfer)
    66  	if err != nil {
    67  		log.L().Fatal("Register transfer:", zap.Error(err))
    68  	}
    69  	err = b.Register(xrc20)
    70  	if err != nil {
    71  		log.L().Fatal("Register xrc20 transfer:", zap.Error(err))
    72  	}
    73  	err = b.Register(multisend)
    74  	if err != nil {
    75  		log.L().Fatal("Register multisend:", zap.Error(err))
    76  	}
    77  
    78  	if err := b.Start(context.Background()); err != nil {
    79  		log.L().Fatal("Failed to start server.", zap.Error(err))
    80  	}
    81  	defer b.Stop()
    82  	select {}
    83  }
    84  
    85  func initLogger(cfg config.Config) error {
    86  	if err := log.InitLoggers(cfg.Log, cfg.SubLogs); err != nil {
    87  		fmt.Println("Cannot config global logger, use default one: ", err)
    88  		return err
    89  	}
    90  	return nil
    91  }