github.com/igggame/nebulas-go@v2.1.0+incompatible/cmd/neb/main.go (about)

     1  // Copyright (C) 2017 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // the go-nebulas library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU General Public License
    16  // along with the go-nebulas library.  If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  
    19  package main
    20  
    21  import (
    22  	"fmt"
    23  	"os"
    24  	"os/signal"
    25  	"sort"
    26  	"strconv"
    27  	"syscall"
    28  	"time"
    29  
    30  	"github.com/nebulasio/go-nebulas/core"
    31  	"github.com/nebulasio/go-nebulas/neblet"
    32  	"github.com/nebulasio/go-nebulas/util/logging"
    33  	"github.com/urfave/cli"
    34  )
    35  
    36  var (
    37  	version   string
    38  	commit    string
    39  	branch    string
    40  	compileAt string
    41  	config    string
    42  )
    43  
    44  func main() {
    45  
    46  	app := cli.NewApp()
    47  	app.Action = neb
    48  	app.Name = "neb"
    49  	app.Version = fmt.Sprintf("%s, branch %s, commit %s", version, branch, commit)
    50  	timestamp, _ := strconv.ParseInt(compileAt, 10, 64)
    51  	app.Compiled = time.Unix(timestamp, 0)
    52  	app.Usage = "the go-nebulas command line interface"
    53  	app.Copyright = "Copyright 2017-2018 The go-nebulas Authors"
    54  
    55  	app.Flags = append(app.Flags, ConfigFlag)
    56  	app.Flags = append(app.Flags, NetworkFlags...)
    57  	app.Flags = append(app.Flags, ChainFlags...)
    58  	app.Flags = append(app.Flags, RPCFlags...)
    59  	app.Flags = append(app.Flags, AppFlags...)
    60  	app.Flags = append(app.Flags, StatsFlags...)
    61  
    62  	sort.Sort(cli.FlagsByName(app.Flags))
    63  
    64  	app.Commands = []cli.Command{
    65  		initCommand,
    66  		genesisCommand,
    67  		accountCommand,
    68  		consoleCommand,
    69  		networkCommand,
    70  		versionCommand,
    71  		licenseCommand,
    72  		configCommand,
    73  		blockDumpCommand,
    74  	}
    75  	sort.Sort(cli.CommandsByName(app.Commands))
    76  
    77  	app.Run(os.Args)
    78  }
    79  
    80  func neb(ctx *cli.Context) error {
    81  	n, err := makeNeb(ctx)
    82  	if err != nil {
    83  		return err
    84  	}
    85  
    86  	logging.Init(n.Config().App.LogFile, n.Config().App.LogLevel, n.Config().App.LogAge)
    87  
    88  	core.SetCompatibilityOptions(n.Config().Chain.ChainId)
    89  
    90  	// enable crash report if open the switch and configure the url
    91  	if n.Config().App.EnableCrashReport && len(n.Config().App.CrashReportUrl) > 0 {
    92  		InitCrashReporter(n.Config().App)
    93  	}
    94  
    95  	select {
    96  	case <-runNeb(ctx, n):
    97  		return nil
    98  	}
    99  }
   100  
   101  func runNeb(ctx *cli.Context, n *neblet.Neblet) chan bool {
   102  	c := make(chan os.Signal, 1)
   103  	signal.Notify(c, os.Interrupt, syscall.SIGTERM)
   104  
   105  	// start net pprof if config.App.Pprof.HttpListen configured
   106  	err := n.StartPprof(n.Config().App.Pprof.HttpListen)
   107  	if err != nil {
   108  		FatalF("start pprof failed:%s", err)
   109  	}
   110  
   111  	n.Setup()
   112  	n.Start()
   113  
   114  	quitCh := make(chan bool, 1)
   115  
   116  	go func() {
   117  		<-c
   118  
   119  		n.Stop()
   120  
   121  		quitCh <- true
   122  		return
   123  	}()
   124  
   125  	return quitCh
   126  }
   127  
   128  func makeNeb(ctx *cli.Context) (*neblet.Neblet, error) {
   129  	conf := neblet.LoadConfig(config)
   130  	conf.App.Version = version
   131  
   132  	// load config from cli args
   133  	networkConfig(ctx, conf.Network)
   134  	chainConfig(ctx, conf.Chain)
   135  	rpcConfig(ctx, conf.Rpc)
   136  	appConfig(ctx, conf.App)
   137  	statsConfig(ctx, conf.Stats)
   138  
   139  	n, err := neblet.New(conf)
   140  	if err != nil {
   141  		return nil, err
   142  	}
   143  	return n, nil
   144  }
   145  
   146  // FatalF fatal format err
   147  func FatalF(format string, args ...interface{}) {
   148  	err := fmt.Sprintf(format, args...)
   149  	fmt.Println(err)
   150  	os.Exit(1)
   151  }