Move option parsing to BuildCfg; fix imports

License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
This commit is contained in:
Łukasz Magiera 2019-04-03 16:56:45 +02:00 committed by Steven Allen
parent 3ac605744f
commit e4cf66008f
8 changed files with 71 additions and 66 deletions

View File

@ -3,6 +3,7 @@ package core
import (
"context"
"github.com/ipfs/go-metrics-interface"
"go.uber.org/fx"
"github.com/ipfs/go-ipfs/core/bootstrap"
@ -13,6 +14,8 @@ type BuildCfg = node.BuildCfg // Alias for compatibility until we properly refac
// NewNode constructs and returns an IpfsNode using the given cfg.
func NewNode(ctx context.Context, cfg *BuildCfg) (*IpfsNode, error) {
ctx = metrics.CtxScope(ctx, "ipfs")
n := &IpfsNode{
ctx: ctx,
}

View File

@ -5,7 +5,7 @@ import (
"fmt"
gopath "path"
node2 "github.com/ipfs/go-ipfs/namesys/resolve"
"github.com/ipfs/go-ipfs/namesys/resolve"
"github.com/ipfs/go-cid"
ipld "github.com/ipfs/go-ipld-format"
@ -42,8 +42,8 @@ func (api *CoreAPI) ResolvePath(ctx context.Context, p path.Path) (path.Resolved
}
ipath := ipfspath.Path(p.String())
ipath, err := node2.ResolveIPNS(ctx, api.namesys, ipath)
if err == node2.ErrNoNamesys {
ipath, err := resolve.ResolveIPNS(ctx, api.namesys, ipath)
if err == resolve.ErrNoNamesys {
return nil, coreiface.ErrOffline
} else if err != nil {
return nil, err

View File

@ -1,17 +1,20 @@
package node
import (
"context"
"crypto/rand"
"encoding/base64"
"errors"
"go.uber.org/fx"
"github.com/ipfs/go-ipfs/repo"
ds "github.com/ipfs/go-datastore"
dsync "github.com/ipfs/go-datastore/sync"
cfg "github.com/ipfs/go-ipfs-config"
ci "github.com/libp2p/go-libp2p-crypto"
peer "github.com/libp2p/go-libp2p-peer"
"github.com/ipfs/go-ipfs/repo"
)
type BuildCfg struct {
@ -75,6 +78,42 @@ func (cfg *BuildCfg) fillDefaults() error {
return nil
}
func (cfg *BuildCfg) options(ctx context.Context) fx.Option {
err := cfg.fillDefaults()
if err != nil {
return fx.Error(err)
}
repoOption := fx.Provide(func(lc fx.Lifecycle) repo.Repo {
lc.Append(fx.Hook{
OnStop: func(ctx context.Context) error {
return cfg.Repo.Close()
},
})
return cfg.Repo
})
metricsCtx := fx.Provide(func() MetricsCtx {
return MetricsCtx(ctx)
})
hostOption := fx.Provide(func() HostOption {
return cfg.Host
})
routingOption := fx.Provide(func() RoutingOption {
return cfg.Routing
})
return fx.Options(
repoOption,
hostOption,
routingOption,
metricsCtx,
)
}
func defaultRepo(dstore repo.Datastore) (repo.Repo, error) {
c := cfg.Config{}
priv, pub, err := ci.GenerateKeyPairWithReader(ci.RSA, 1024, rand.Reader)

View File

@ -4,24 +4,24 @@ import (
"context"
"fmt"
"github.com/ipfs/go-ipfs/pin"
"github.com/ipfs/go-ipfs/repo"
"github.com/ipfs/go-bitswap"
"github.com/ipfs/go-bitswap/network"
"github.com/ipfs/go-blockservice"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
blockstore "github.com/ipfs/go-ipfs-blockstore"
exchange "github.com/ipfs/go-ipfs-exchange-interface"
offline "github.com/ipfs/go-ipfs-exchange-offline"
format "github.com/ipfs/go-ipld-format"
"github.com/ipfs/go-ipfs-blockstore"
"github.com/ipfs/go-ipfs-exchange-interface"
"github.com/ipfs/go-ipfs-exchange-offline"
"github.com/ipfs/go-ipld-format"
"github.com/ipfs/go-merkledag"
"github.com/ipfs/go-mfs"
"github.com/ipfs/go-unixfs"
host "github.com/libp2p/go-libp2p-host"
routing "github.com/libp2p/go-libp2p-routing"
"github.com/libp2p/go-libp2p-host"
"github.com/libp2p/go-libp2p-routing"
"go.uber.org/fx"
"github.com/ipfs/go-ipfs/pin"
"github.com/ipfs/go-ipfs/repo"
)
func BlockServiceCtor(lc fx.Lifecycle, bs blockstore.Blockstore, rem exchange.Interface) blockservice.BlockService {

View File

@ -3,15 +3,13 @@ package node
import (
"context"
offline "github.com/ipfs/go-ipfs-exchange-offline"
"github.com/ipfs/go-metrics-interface"
"github.com/ipfs/go-path/resolver"
"go.uber.org/fx"
offroute "github.com/ipfs/go-ipfs-routing/offline"
"github.com/ipfs/go-ipfs/p2p"
"github.com/ipfs/go-ipfs/provider"
"github.com/ipfs/go-ipfs/repo"
offline "github.com/ipfs/go-ipfs-exchange-offline"
offroute "github.com/ipfs/go-ipfs-routing/offline"
"github.com/ipfs/go-path/resolver"
"go.uber.org/fx"
)
var BaseLibP2P = fx.Options(
@ -117,44 +115,9 @@ func IPFS(ctx context.Context, cfg *BuildCfg) fx.Option {
cfg = new(BuildCfg)
}
err := cfg.fillDefaults()
if err != nil {
return fx.Error(err)
}
ctx = metrics.CtxScope(ctx, "ipfs")
repoOption := fx.Provide(func(lc fx.Lifecycle) repo.Repo {
lc.Append(fx.Hook{
OnStop: func(ctx context.Context) error {
return cfg.Repo.Close()
},
})
return cfg.Repo
})
metricsCtx := fx.Provide(func() MetricsCtx {
return MetricsCtx(ctx)
})
hostOption := fx.Provide(func() HostOption {
return cfg.Host
})
routingOption := fx.Provide(func() RoutingOption {
return cfg.Routing
})
params := fx.Options(
repoOption,
hostOption,
routingOption,
metricsCtx,
)
return fx.Options(
params,
cfg.options(ctx),
fx.Provide(baseProcess),
fx.Invoke(setupSharding),

View File

@ -123,6 +123,7 @@ type Libp2pOpts struct {
}
type PNetFingerprint []byte
func P2PPNet(repo repo.Repo) (opts Libp2pOpts, fp PNetFingerprint, err error) {
swarmkey, err := repo.SwarmKey()
if err != nil || swarmkey == nil {

View File

@ -13,7 +13,7 @@ import (
core "github.com/ipfs/go-ipfs/core"
namesys "github.com/ipfs/go-ipfs/namesys"
node2 "github.com/ipfs/go-ipfs/namesys/resolve"
resolve "github.com/ipfs/go-ipfs/namesys/resolve"
dag "github.com/ipfs/go-merkledag"
path "github.com/ipfs/go-path"
@ -98,7 +98,7 @@ func loadRoot(ctx context.Context, rt *keyRoot, ipfs *core.IpfsNode, name string
return nil, err
}
node, err := node2.Resolve(ctx, ipfs.Namesys, ipfs.Resolver, p)
node, err := resolve.Resolve(ctx, ipfs.Namesys, ipfs.Resolver, p)
switch err {
case nil:
case namesys.ErrResolveFailed:

View File

@ -6,7 +6,6 @@ import (
"strings"
"github.com/ipfs/go-ipld-format"
log2 "github.com/ipfs/go-log"
logging "github.com/ipfs/go-log"
"github.com/ipfs/go-path"
"github.com/ipfs/go-path/resolver"
@ -30,34 +29,34 @@ func ResolveIPNS(ctx context.Context, nsys namesys.NameSystem, p path.Path) (pat
// TODO(cryptix): we should be able to query the local cache for the path
if nsys == nil {
evt.Append(log2.LoggableMap{"error": ErrNoNamesys.Error()})
evt.Append(logging.LoggableMap{"error": ErrNoNamesys.Error()})
return "", ErrNoNamesys
}
seg := p.Segments()
if len(seg) < 2 || seg[1] == "" { // just "/<protocol/>" without further segments
evt.Append(log2.LoggableMap{"error": path.ErrNoComponents.Error()})
evt.Append(logging.LoggableMap{"error": path.ErrNoComponents.Error()})
return "", path.ErrNoComponents
}
extensions := seg[2:]
resolvable, err := path.FromSegments("/", seg[0], seg[1])
if err != nil {
evt.Append(log2.LoggableMap{"error": err.Error()})
evt.Append(logging.LoggableMap{"error": err.Error()})
return "", err
}
respath, err := nsys.Resolve(ctx, resolvable.String())
if err != nil {
evt.Append(log2.LoggableMap{"error": err.Error()})
evt.Append(logging.LoggableMap{"error": err.Error()})
return "", err
}
segments := append(respath.Segments(), extensions...)
p, err = path.FromSegments("/", segments...)
if err != nil {
evt.Append(log2.LoggableMap{"error": err.Error()})
evt.Append(logging.LoggableMap{"error": err.Error()})
return "", err
}
}