Skip to content

Commit

Permalink
Move option parsing to BuildCfg; fix imports
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
  • Loading branch information
magik6k committed Apr 3, 2019
1 parent 58ea801 commit 3781a67
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 64 deletions.
3 changes: 3 additions & 0 deletions core/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
}
Expand Down
6 changes: 3 additions & 3 deletions core/coreapi/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -38,8 +38,8 @@ func (api *CoreAPI) ResolvePath(ctx context.Context, p coreiface.Path) (coreifac
}

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
Expand Down
43 changes: 41 additions & 2 deletions core/node/builder.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 9 additions & 9 deletions core/node/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
48 changes: 6 additions & 42 deletions core/node/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ package node
import (
"context"

"github.com/ipfs/go-ipfs/p2p"
"github.com/ipfs/go-ipfs/provider"

offline "github.com/ipfs/go-ipfs-exchange-offline"
"github.com/ipfs/go-metrics-interface"
offroute "github.com/ipfs/go-ipfs-routing/offline"
"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"
)

var BaseLibP2P = fx.Options(
Expand Down Expand Up @@ -117,44 +116,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),

Expand Down
4 changes: 2 additions & 2 deletions fuse/ipns/ipns_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down
11 changes: 5 additions & 6 deletions namesys/resolve/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
}
Expand Down

0 comments on commit 3781a67

Please sign in to comment.