builder.go 1.13 KB
Newer Older
Jeromy's avatar
Jeromy committed
1 2 3
package core

import (
Jeromy's avatar
Jeromy committed
4
	"context"
Jeromy's avatar
Jeromy committed
5

6
	"github.com/ipfs/go-metrics-interface"
7 8
	"go.uber.org/fx"

Łukasz Magiera's avatar
Łukasz Magiera committed
9 10
	"github.com/ipfs/go-ipfs/core/bootstrap"
	"github.com/ipfs/go-ipfs/core/node"
Łukasz Magiera's avatar
Łukasz Magiera committed
11
)
Jeromy's avatar
Jeromy committed
12

13
type BuildCfg = node.BuildCfg // Alias for compatibility until we properly refactor the constructor interface
Jeromy's avatar
Jeromy committed
14

15
// NewNode constructs and returns an IpfsNode using the given cfg.
16
func NewNode(ctx context.Context, cfg *BuildCfg) (*IpfsNode, error) {
17 18
	ctx = metrics.CtxScope(ctx, "ipfs")

Łukasz Magiera's avatar
Łukasz Magiera committed
19 20 21
	n := &IpfsNode{
		ctx: ctx,
	}
22

Łukasz Magiera's avatar
Łukasz Magiera committed
23
	app := fx.New(
24
		node.IPFS(ctx, cfg),
25

26
		fx.NopLogger,
27 28 29
		fx.Extract(n),
	)

30
	go func() {
Łukasz Magiera's avatar
Łukasz Magiera committed
31 32
		// Note that some services use contexts to signal shutting down, which is
		// very suboptimal. This needs to be here until that's addressed somehow
33
		<-ctx.Done()
Steven Allen's avatar
Steven Allen committed
34 35 36 37
		err := app.Stop(context.Background())
		if err != nil {
			log.Error("failure on stop: ", err)
		}
38 39
	}()

40
	n.IsOnline = cfg.Online
Łukasz Magiera's avatar
Łukasz Magiera committed
41
	n.app = app
42

Łukasz Magiera's avatar
Łukasz Magiera committed
43 44 45 46 47 48 49 50
	if app.Err() != nil {
		return nil, app.Err()
	}

	if err := app.Start(ctx); err != nil {
		return nil, err
	}

Łukasz Magiera's avatar
Łukasz Magiera committed
51
	// TODO: How soon will bootstrap move to libp2p?
Łukasz Magiera's avatar
Łukasz Magiera committed
52 53 54
	if !cfg.Online {
		return n, nil
	}
Jeromy's avatar
Jeromy committed
55

Łukasz Magiera's avatar
Łukasz Magiera committed
56
	return n, n.Bootstrap(bootstrap.DefaultBootstrapConfig)
57
}