code.vegaprotocol.io/vega@v0.79.0/cmd/data-node/commands/gateway.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package commands
    17  
    18  import (
    19  	"context"
    20  	"errors"
    21  	"fmt"
    22  	"os"
    23  	"os/signal"
    24  	"syscall"
    25  
    26  	"code.vegaprotocol.io/vega/datanode/config"
    27  	"code.vegaprotocol.io/vega/datanode/gateway"
    28  	"code.vegaprotocol.io/vega/datanode/gateway/server"
    29  	"code.vegaprotocol.io/vega/logging"
    30  	"code.vegaprotocol.io/vega/paths"
    31  
    32  	"github.com/jessevdk/go-flags"
    33  	"golang.org/x/sync/errgroup"
    34  )
    35  
    36  type gatewayCmd struct {
    37  	gateway.Config
    38  	config.VegaHomeFlag
    39  }
    40  
    41  func (opts *gatewayCmd) Execute(_ []string) error {
    42  	ctx, cancel := context.WithCancel(context.Background())
    43  	defer cancel()
    44  	eg, ctx := errgroup.WithContext(ctx)
    45  
    46  	log := logging.NewLoggerFromConfig(logging.NewDefaultConfig())
    47  	defer log.AtExit()
    48  
    49  	vegaPaths := paths.New(opts.VegaHome)
    50  
    51  	cfgwatchr, err := config.NewWatcher(ctx, log, vegaPaths)
    52  	if err != nil {
    53  		log.Error("unable to start config watcher", logging.Error(err))
    54  		return errors.New("unable to start config watcher")
    55  	}
    56  
    57  	conf := cfgwatchr.Get()
    58  	opts.Config = conf.Gateway
    59  
    60  	// parse the remaining command line options again to ensure they
    61  	// take precedence.
    62  	if _, err := flags.Parse(opts); err != nil {
    63  		return err
    64  	}
    65  
    66  	// waitSig will wait for a sigterm or sigint interrupt.
    67  	eg.Go(func() error {
    68  		gracefulStop := make(chan os.Signal, 1)
    69  		signal.Notify(gracefulStop, syscall.SIGTERM, syscall.SIGINT)
    70  
    71  		select {
    72  		case sig := <-gracefulStop:
    73  			log.Info("Caught signal", logging.String("name", fmt.Sprintf("%+v", sig)))
    74  			cancel()
    75  		case <-ctx.Done():
    76  			return ctx.Err()
    77  		}
    78  
    79  		return nil
    80  	})
    81  
    82  	eg.Go(func() error {
    83  		srv := server.New(opts.Config, log, vegaPaths)
    84  		if err := srv.Start(ctx); err != nil {
    85  			return err
    86  		}
    87  
    88  		return nil
    89  	})
    90  
    91  	return eg.Wait()
    92  }
    93  
    94  func Gateway(ctx context.Context, parser *flags.Parser) error {
    95  	opts := &gatewayCmd{
    96  		Config: gateway.NewDefaultConfig(),
    97  	}
    98  
    99  	_, err := parser.AddCommand("gateway", "The API gateway", "The gateway for all the vega APIs", opts)
   100  	return err
   101  }