github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/cmd/bootstrap.go (about)

     1  //go:build !ios && !android
     2  
     3  /*
     4   * Copyright (C) 2018 The "MysteriumNetwork/node" Authors.
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License as published by
     8   * the Free Software Foundation, either version 3 of the License, or
     9   * (at your option) any later version.
    10   *
    11   * This program is distributed in the hope that it will be useful,
    12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14   * GNU General Public License for more details.
    15   *
    16   * You should have received a copy of the GNU General Public License
    17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18   */
    19  
    20  package cmd
    21  
    22  import (
    23  	"net"
    24  	"os"
    25  	"time"
    26  
    27  	"github.com/gin-gonic/gin"
    28  
    29  	"github.com/mysteriumnetwork/node/config"
    30  	"github.com/mysteriumnetwork/node/consumer/entertainment"
    31  	"github.com/mysteriumnetwork/node/core/node"
    32  	"github.com/mysteriumnetwork/node/services"
    33  	"github.com/mysteriumnetwork/node/tequilapi"
    34  	tequilapi_client "github.com/mysteriumnetwork/node/tequilapi/client"
    35  	tequilapi_endpoints "github.com/mysteriumnetwork/node/tequilapi/endpoints"
    36  	"github.com/mysteriumnetwork/node/ui"
    37  	uinoop "github.com/mysteriumnetwork/node/ui/noop"
    38  	"github.com/mysteriumnetwork/node/ui/versionmanager"
    39  	"github.com/mysteriumnetwork/node/utils"
    40  )
    41  
    42  func (di *Dependencies) bootstrapTequilapi(nodeOptions node.Options, listener net.Listener) (tequilapi.APIServer, error) {
    43  	if !nodeOptions.TequilapiEnabled {
    44  		return tequilapi.NewNoopAPIServer(), nil
    45  	}
    46  	tequilaApiClient := tequilapi_client.NewClient(nodeOptions.TequilapiAddress, nodeOptions.TequilapiPort)
    47  
    48  	return tequilapi.NewServer(
    49  		listener,
    50  		nodeOptions,
    51  		di.JWTAuthenticator,
    52  		[]func(engine *gin.Engine) error{
    53  			func(e *gin.Engine) error {
    54  				if err := tequilapi_endpoints.AddRoutesForSSE(e, di.StateKeeper, di.EventBus); err != nil {
    55  					return err
    56  				}
    57  				return nil
    58  			},
    59  			func(e *gin.Engine) error {
    60  				if config.GetBool(config.FlagPProfEnable) {
    61  					tequilapi_endpoints.AddRoutesForPProf(e)
    62  				}
    63  				return nil
    64  			},
    65  			func(e *gin.Engine) error {
    66  				e.GET("/healthcheck", tequilapi_endpoints.HealthCheckEndpointFactory(time.Now, os.Getpid).HealthCheck)
    67  				return nil
    68  			},
    69  			tequilapi_endpoints.AddRouteForStop(utils.SoftKiller(di.Shutdown)),
    70  			tequilapi_endpoints.AddRoutesForAuthentication(di.Authenticator, di.JWTAuthenticator, di.SSOMystnodes),
    71  			tequilapi_endpoints.AddRoutesForIdentities(di.IdentityManager, di.IdentitySelector, di.IdentityRegistry, di.ConsumerBalanceTracker, di.AddressProvider, di.HermesChannelRepository, di.BCHelper, di.Transactor, di.BeneficiaryProvider, di.IdentityMover, di.BeneficiaryAddressStorage, di.HermesMigrator),
    72  			tequilapi_endpoints.AddRoutesForConnection(di.MultiConnectionManager, di.StateKeeper, di.ProposalRepository, di.IdentityRegistry, di.EventBus, di.AddressProvider),
    73  			tequilapi_endpoints.AddRoutesForSessions(di.SessionStorage),
    74  			tequilapi_endpoints.AddRoutesForConnectionLocation(di.IPResolver, di.LocationResolver, di.LocationResolver),
    75  			tequilapi_endpoints.AddRoutesForProposals(di.ProposalRepository, di.PricingHelper, di.LocationResolver, di.FilterPresetStorage, di.NATProber),
    76  			tequilapi_endpoints.AddRoutesForService(di.ServicesManager, services.JSONParsersByType, di.ProposalRepository, tequilaApiClient),
    77  			tequilapi_endpoints.AddRoutesForAccessPolicies(di.HTTPClient, config.GetString(config.FlagAccessPolicyAddress)),
    78  			tequilapi_endpoints.AddRoutesForNAT(di.StateKeeper, di.NATProber),
    79  			tequilapi_endpoints.AddRoutesForNodeUI(versionmanager.NewVersionManager(di.UIServer, di.HTTPClient, di.uiVersionConfig)),
    80  			tequilapi_endpoints.AddRoutesForNode(di.NodeStatusTracker, di.NodeStatsTracker),
    81  			tequilapi_endpoints.AddRoutesForTransactor(di.IdentityRegistry, di.Transactor, di.Affiliator, di.HermesPromiseSettler, di.SettlementHistoryStorage, di.AddressProvider, di.BeneficiaryProvider, di.BeneficiarySaver, di.PilvytisAPI),
    82  			tequilapi_endpoints.AddRoutesForAffiliator(di.Affiliator),
    83  			tequilapi_endpoints.AddRoutesForConfig,
    84  			tequilapi_endpoints.AddRoutesForMMN(di.MMN, di.SSOMystnodes, di.Authenticator),
    85  			tequilapi_endpoints.AddRoutesForFeedback(di.Reporter),
    86  			tequilapi_endpoints.AddRoutesForConnectivityStatus(di.SessionConnectivityStatusStorage),
    87  			tequilapi_endpoints.AddRoutesForDocs,
    88  			tequilapi_endpoints.AddRoutesForCurrencyExchange(di.PilvytisAPI),
    89  			tequilapi_endpoints.AddRoutesForPilvytis(di.PilvytisAPI, di.PilvytisOrderIssuer, di.LocationResolver),
    90  			tequilapi_endpoints.AddRoutesForTerms,
    91  			tequilapi_endpoints.AddEntertainmentRoutes(entertainment.NewEstimator(
    92  				config.FlagPaymentPriceGiB.Value,
    93  				config.FlagPaymentPriceHour.Value,
    94  			)),
    95  			tequilapi_endpoints.AddRoutesForValidator,
    96  		},
    97  	)
    98  }
    99  
   100  func (di *Dependencies) bootstrapNodeUIVersionConfig(nodeOptions node.Options) error {
   101  	if !nodeOptions.TequilapiEnabled || nodeOptions.Directories.NodeUI == "" {
   102  		noopCfg, _ := versionmanager.NewNoOpVersionConfig()
   103  		di.uiVersionConfig = noopCfg
   104  		return nil
   105  	}
   106  
   107  	versionConfig, err := versionmanager.NewVersionConfig(nodeOptions.Directories.NodeUI)
   108  	if err != nil {
   109  		return err
   110  	}
   111  	di.uiVersionConfig = versionConfig
   112  	return nil
   113  }
   114  
   115  func (di *Dependencies) bootstrapUIServer(options node.Options) (err error) {
   116  	if !options.UI.UIEnabled {
   117  		di.UIServer = uinoop.NewServer()
   118  		return nil
   119  	}
   120  
   121  	bindAddress := options.UI.UIBindAddress
   122  	if bindAddress == "" {
   123  		bindAddress, err = di.IPResolver.GetOutboundIP()
   124  		if err != nil {
   125  			return err
   126  		}
   127  		bindAddress = bindAddress + ",127.0.0.1"
   128  	}
   129  	di.UIServer = ui.NewServer(bindAddress, options.UI.UIPort, options.TequilapiAddress, options.TequilapiPort, di.JWTAuthenticator, di.HTTPClient, di.uiVersionConfig)
   130  	return nil
   131  }