libp2p.go 1.38 KB
Newer Older
1
2
3
4
5
package libp2p

import (
	"context"

Steven Allen's avatar
Steven Allen committed
6
7
	config "github.com/libp2p/go-libp2p/config"

8
9
10
	host "github.com/libp2p/go-libp2p-host"
)

Jeromy's avatar
Jeromy committed
11
// Config describes a set of settings for a libp2p node
Steven Allen's avatar
Steven Allen committed
12
type Config = config.Config
Jeromy's avatar
Jeromy committed
13

Steven Allen's avatar
Steven Allen committed
14
15
16
// Option is a libp2p config option that can be given to the libp2p constructor
// (`libp2p.New`).
type Option = config.Option
Jeromy's avatar
Jeromy committed
17

Steven Allen's avatar
Steven Allen committed
18
19
// ChainOptions chains multiple options into a single option.
func ChainOptions(opts ...Option) Option {
Jeromy's avatar
Jeromy committed
20
	return func(cfg *Config) error {
Steven Allen's avatar
Steven Allen committed
21
22
		for _, opt := range opts {
			if err := opt(cfg); err != nil {
Jeromy's avatar
Jeromy committed
23
24
				return err
			}
Jeromy's avatar
Jeromy committed
25
		}
Jeromy's avatar
Jeromy committed
26
27
		return nil
	}
Jeromy's avatar
Jeromy committed
28
29
}

Steven Allen's avatar
Steven Allen committed
30
31
32
33
// New constructs a new libp2p node with the given options (falling back on
// reasonable defaults).
//
// Canceling the passed context will stop the returned libp2p node.
Jeromy's avatar
Jeromy committed
34
func New(ctx context.Context, opts ...Option) (host.Host, error) {
Steven Allen's avatar
Steven Allen committed
35
	return NewWithoutDefaults(ctx, append(opts, FallbackDefaults)...)
Jeromy's avatar
Jeromy committed
36
37
}

Steven Allen's avatar
Steven Allen committed
38
39
40
41
42
43
44
45
46
// NewWithoutDefaults constructs a new libp2p node with the given options but
// *without* falling back on reasonable defaults.
//
// Warning: This function should not be considered a stable interface. We may
// choose to add required services at any time and, by using this function, you
// opt-out of any defaults we may provide.
func NewWithoutDefaults(ctx context.Context, opts ...Option) (host.Host, error) {
	var cfg Config
	if err := cfg.Apply(opts...); err != nil {
47
48
		return nil, err
	}
Steven Allen's avatar
Steven Allen committed
49
	return cfg.NewNode(ctx)
50
}