提交 e4cf6600 作者: Łukasz Magiera 提交者: Steven Allen

Move option parsing to BuildCfg; fix imports

License: MIT
Signed-off-by: 's avatarŁukasz Magiera <magik6k@gmail.com>
上级 3ac60574
...@@ -3,6 +3,7 @@ package core ...@@ -3,6 +3,7 @@ package core
import ( import (
"context" "context"
"github.com/ipfs/go-metrics-interface"
"go.uber.org/fx" "go.uber.org/fx"
"github.com/ipfs/go-ipfs/core/bootstrap" "github.com/ipfs/go-ipfs/core/bootstrap"
...@@ -13,6 +14,8 @@ type BuildCfg = node.BuildCfg // Alias for compatibility until we properly refac ...@@ -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. // NewNode constructs and returns an IpfsNode using the given cfg.
func NewNode(ctx context.Context, cfg *BuildCfg) (*IpfsNode, error) { func NewNode(ctx context.Context, cfg *BuildCfg) (*IpfsNode, error) {
ctx = metrics.CtxScope(ctx, "ipfs")
n := &IpfsNode{ n := &IpfsNode{
ctx: ctx, ctx: ctx,
} }
......
...@@ -5,7 +5,7 @@ import ( ...@@ -5,7 +5,7 @@ import (
"fmt" "fmt"
gopath "path" gopath "path"
node2 "github.com/ipfs/go-ipfs/namesys/resolve" "github.com/ipfs/go-ipfs/namesys/resolve"
"github.com/ipfs/go-cid" "github.com/ipfs/go-cid"
ipld "github.com/ipfs/go-ipld-format" ipld "github.com/ipfs/go-ipld-format"
...@@ -42,8 +42,8 @@ func (api *CoreAPI) ResolvePath(ctx context.Context, p path.Path) (path.Resolved ...@@ -42,8 +42,8 @@ func (api *CoreAPI) ResolvePath(ctx context.Context, p path.Path) (path.Resolved
} }
ipath := ipfspath.Path(p.String()) ipath := ipfspath.Path(p.String())
ipath, err := node2.ResolveIPNS(ctx, api.namesys, ipath) ipath, err := resolve.ResolveIPNS(ctx, api.namesys, ipath)
if err == node2.ErrNoNamesys { if err == resolve.ErrNoNamesys {
return nil, coreiface.ErrOffline return nil, coreiface.ErrOffline
} else if err != nil { } else if err != nil {
return nil, err return nil, err
......
package node package node
import ( import (
"context"
"crypto/rand" "crypto/rand"
"encoding/base64" "encoding/base64"
"errors" "errors"
"go.uber.org/fx"
"github.com/ipfs/go-ipfs/repo"
ds "github.com/ipfs/go-datastore" ds "github.com/ipfs/go-datastore"
dsync "github.com/ipfs/go-datastore/sync" dsync "github.com/ipfs/go-datastore/sync"
cfg "github.com/ipfs/go-ipfs-config" cfg "github.com/ipfs/go-ipfs-config"
ci "github.com/libp2p/go-libp2p-crypto" ci "github.com/libp2p/go-libp2p-crypto"
peer "github.com/libp2p/go-libp2p-peer" peer "github.com/libp2p/go-libp2p-peer"
"github.com/ipfs/go-ipfs/repo"
) )
type BuildCfg struct { type BuildCfg struct {
...@@ -75,6 +78,42 @@ func (cfg *BuildCfg) fillDefaults() error { ...@@ -75,6 +78,42 @@ func (cfg *BuildCfg) fillDefaults() error {
return nil 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) { func defaultRepo(dstore repo.Datastore) (repo.Repo, error) {
c := cfg.Config{} c := cfg.Config{}
priv, pub, err := ci.GenerateKeyPairWithReader(ci.RSA, 1024, rand.Reader) priv, pub, err := ci.GenerateKeyPairWithReader(ci.RSA, 1024, rand.Reader)
......
...@@ -4,24 +4,24 @@ import ( ...@@ -4,24 +4,24 @@ import (
"context" "context"
"fmt" "fmt"
"github.com/ipfs/go-ipfs/pin"
"github.com/ipfs/go-ipfs/repo"
"github.com/ipfs/go-bitswap" "github.com/ipfs/go-bitswap"
"github.com/ipfs/go-bitswap/network" "github.com/ipfs/go-bitswap/network"
"github.com/ipfs/go-blockservice" "github.com/ipfs/go-blockservice"
"github.com/ipfs/go-cid" "github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore" "github.com/ipfs/go-datastore"
blockstore "github.com/ipfs/go-ipfs-blockstore" "github.com/ipfs/go-ipfs-blockstore"
exchange "github.com/ipfs/go-ipfs-exchange-interface" "github.com/ipfs/go-ipfs-exchange-interface"
offline "github.com/ipfs/go-ipfs-exchange-offline" "github.com/ipfs/go-ipfs-exchange-offline"
format "github.com/ipfs/go-ipld-format" "github.com/ipfs/go-ipld-format"
"github.com/ipfs/go-merkledag" "github.com/ipfs/go-merkledag"
"github.com/ipfs/go-mfs" "github.com/ipfs/go-mfs"
"github.com/ipfs/go-unixfs" "github.com/ipfs/go-unixfs"
host "github.com/libp2p/go-libp2p-host" "github.com/libp2p/go-libp2p-host"
routing "github.com/libp2p/go-libp2p-routing" "github.com/libp2p/go-libp2p-routing"
"go.uber.org/fx" "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 { func BlockServiceCtor(lc fx.Lifecycle, bs blockstore.Blockstore, rem exchange.Interface) blockservice.BlockService {
......
...@@ -3,15 +3,13 @@ package node ...@@ -3,15 +3,13 @@ package node
import ( import (
"context" "context"
"github.com/ipfs/go-ipfs/p2p"
"github.com/ipfs/go-ipfs/provider"
offline "github.com/ipfs/go-ipfs-exchange-offline" 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" "github.com/ipfs/go-path/resolver"
"go.uber.org/fx" "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( var BaseLibP2P = fx.Options(
...@@ -117,44 +115,9 @@ func IPFS(ctx context.Context, cfg *BuildCfg) fx.Option { ...@@ -117,44 +115,9 @@ func IPFS(ctx context.Context, cfg *BuildCfg) fx.Option {
cfg = new(BuildCfg) 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( return fx.Options(
params, cfg.options(ctx),
fx.Provide(baseProcess), fx.Provide(baseProcess),
fx.Invoke(setupSharding), fx.Invoke(setupSharding),
......
...@@ -123,6 +123,7 @@ type Libp2pOpts struct { ...@@ -123,6 +123,7 @@ type Libp2pOpts struct {
} }
type PNetFingerprint []byte type PNetFingerprint []byte
func P2PPNet(repo repo.Repo) (opts Libp2pOpts, fp PNetFingerprint, err error) { func P2PPNet(repo repo.Repo) (opts Libp2pOpts, fp PNetFingerprint, err error) {
swarmkey, err := repo.SwarmKey() swarmkey, err := repo.SwarmKey()
if err != nil || swarmkey == nil { if err != nil || swarmkey == nil {
......
...@@ -13,7 +13,7 @@ import ( ...@@ -13,7 +13,7 @@ import (
core "github.com/ipfs/go-ipfs/core" core "github.com/ipfs/go-ipfs/core"
namesys "github.com/ipfs/go-ipfs/namesys" 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" dag "github.com/ipfs/go-merkledag"
path "github.com/ipfs/go-path" path "github.com/ipfs/go-path"
...@@ -98,7 +98,7 @@ func loadRoot(ctx context.Context, rt *keyRoot, ipfs *core.IpfsNode, name string ...@@ -98,7 +98,7 @@ func loadRoot(ctx context.Context, rt *keyRoot, ipfs *core.IpfsNode, name string
return nil, err 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 { switch err {
case nil: case nil:
case namesys.ErrResolveFailed: case namesys.ErrResolveFailed:
......
...@@ -6,7 +6,6 @@ import ( ...@@ -6,7 +6,6 @@ import (
"strings" "strings"
"github.com/ipfs/go-ipld-format" "github.com/ipfs/go-ipld-format"
log2 "github.com/ipfs/go-log"
logging "github.com/ipfs/go-log" logging "github.com/ipfs/go-log"
"github.com/ipfs/go-path" "github.com/ipfs/go-path"
"github.com/ipfs/go-path/resolver" "github.com/ipfs/go-path/resolver"
...@@ -30,34 +29,34 @@ func ResolveIPNS(ctx context.Context, nsys namesys.NameSystem, p path.Path) (pat ...@@ -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 // TODO(cryptix): we should be able to query the local cache for the path
if nsys == nil { if nsys == nil {
evt.Append(log2.LoggableMap{"error": ErrNoNamesys.Error()}) evt.Append(logging.LoggableMap{"error": ErrNoNamesys.Error()})
return "", ErrNoNamesys return "", ErrNoNamesys
} }
seg := p.Segments() seg := p.Segments()
if len(seg) < 2 || seg[1] == "" { // just "/<protocol/>" without further 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 return "", path.ErrNoComponents
} }
extensions := seg[2:] extensions := seg[2:]
resolvable, err := path.FromSegments("/", seg[0], seg[1]) resolvable, err := path.FromSegments("/", seg[0], seg[1])
if err != nil { if err != nil {
evt.Append(log2.LoggableMap{"error": err.Error()}) evt.Append(logging.LoggableMap{"error": err.Error()})
return "", err return "", err
} }
respath, err := nsys.Resolve(ctx, resolvable.String()) respath, err := nsys.Resolve(ctx, resolvable.String())
if err != nil { if err != nil {
evt.Append(log2.LoggableMap{"error": err.Error()}) evt.Append(logging.LoggableMap{"error": err.Error()})
return "", err return "", err
} }
segments := append(respath.Segments(), extensions...) segments := append(respath.Segments(), extensions...)
p, err = path.FromSegments("/", segments...) p, err = path.FromSegments("/", segments...)
if err != nil { if err != nil {
evt.Append(log2.LoggableMap{"error": err.Error()}) evt.Append(logging.LoggableMap{"error": err.Error()})
return "", err return "", err
} }
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论