API struct instead of DI magic
This commit is contained in:
parent
82890165de
commit
d852b3f7ef
91
node/api.go
91
node/api.go
@ -2,91 +2,50 @@ package node
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"reflect"
|
|
||||||
|
|
||||||
"github.com/filecoin-project/go-lotus/api"
|
"github.com/filecoin-project/go-lotus/api"
|
||||||
"github.com/filecoin-project/go-lotus/build"
|
"github.com/filecoin-project/go-lotus/build"
|
||||||
|
|
||||||
"github.com/libp2p/go-libp2p-core/host"
|
"github.com/libp2p/go-libp2p-core/host"
|
||||||
"github.com/libp2p/go-libp2p-core/peer"
|
"github.com/libp2p/go-libp2p-core/peer"
|
||||||
ma "github.com/multiformats/go-multiaddr"
|
ma "github.com/multiformats/go-multiaddr"
|
||||||
"go.uber.org/fx"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var errTyp = reflect.TypeOf(new(error)).Elem()
|
type API struct {
|
||||||
|
Host host.Host
|
||||||
// TODO: type checking, this isn't JS
|
|
||||||
func provideApi(f interface{}, toProvide interface{}) fx.Option {
|
|
||||||
rf := reflect.ValueOf(f)
|
|
||||||
tp := reflect.ValueOf(toProvide).Elem()
|
|
||||||
|
|
||||||
ins := make([]reflect.Type, rf.Type().NumIn())
|
|
||||||
for i := range ins {
|
|
||||||
ins[i] = rf.Type().In(i)
|
|
||||||
}
|
|
||||||
|
|
||||||
ctyp := reflect.FuncOf(ins, []reflect.Type{errTyp}, rf.Type().IsVariadic())
|
|
||||||
|
|
||||||
return fx.Invoke(reflect.MakeFunc(ctyp, func(args []reflect.Value) (results []reflect.Value) {
|
|
||||||
provided := rf.Call(args)
|
|
||||||
tp.Set(provided[0].Elem().Convert(tp.Type()))
|
|
||||||
return []reflect.Value{reflect.ValueOf(new(error)).Elem()}
|
|
||||||
}).Interface())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func apiOption(resAPI *api.Struct) fx.Option {
|
func (a *API) ID(context.Context) (peer.ID, error) {
|
||||||
in := &resAPI.Internal
|
return a.Host.ID(), nil
|
||||||
|
|
||||||
return fx.Options(
|
|
||||||
provideApi(versionAPI, &in.Version),
|
|
||||||
provideApi(idAPI, &in.ID),
|
|
||||||
|
|
||||||
provideApi(netPeersAPI, &in.NetPeers),
|
|
||||||
provideApi(netConnectAPI, &in.NetConnect),
|
|
||||||
provideApi(netAddrsListenAPI, &in.NetAddrsListen),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func idAPI(id peer.ID) interface{} {
|
func (a *API) Version(context.Context) (api.Version, error) {
|
||||||
return func(ctx context.Context) (peer.ID, error) {
|
return api.Version{
|
||||||
return id, nil
|
Version: build.Version,
|
||||||
}
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func versionAPI() interface{} {
|
func (a *API) NetPeers(context.Context) ([]peer.AddrInfo, error) {
|
||||||
return func(context.Context) (api.Version, error) {
|
conns := a.Host.Network().Conns()
|
||||||
return api.Version{
|
out := make([]peer.AddrInfo, len(conns))
|
||||||
Version: build.Version,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func netPeersAPI(h host.Host) interface{} {
|
for i, conn := range conns {
|
||||||
return func(ctx context.Context) ([]peer.AddrInfo, error) {
|
out[i] = peer.AddrInfo{
|
||||||
conns := h.Network().Conns()
|
ID: conn.RemotePeer(),
|
||||||
out := make([]peer.AddrInfo, len(conns))
|
Addrs: []ma.Multiaddr{
|
||||||
|
conn.RemoteMultiaddr(),
|
||||||
for i, conn := range conns {
|
},
|
||||||
out[i] = peer.AddrInfo{
|
|
||||||
ID: conn.RemotePeer(),
|
|
||||||
Addrs: []ma.Multiaddr{
|
|
||||||
conn.RemoteMultiaddr(),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return out, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return out, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func netConnectAPI(h host.Host) interface{} {
|
func (a *API) NetConnect(ctx context.Context, p peer.AddrInfo) error {
|
||||||
return func(ctx context.Context, p peer.AddrInfo) error {
|
return a.Host.Connect(ctx, p)
|
||||||
return h.Connect(ctx, p)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func netAddrsListenAPI(h host.Host) interface{} {
|
func (a *API) NetAddrsListen(context.Context) ([]ma.Multiaddr, error) {
|
||||||
return func(context.Context) ([]ma.Multiaddr, error) {
|
return a.Host.Addrs(), nil
|
||||||
return h.Addrs(), nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var _ api.API = &API{}
|
||||||
|
@ -193,7 +193,7 @@ func Config(cfg *config.Root) Option {
|
|||||||
|
|
||||||
// New builds and starts new Filecoin node
|
// New builds and starts new Filecoin node
|
||||||
func New(ctx context.Context, opts ...Option) (api.API, error) {
|
func New(ctx context.Context, opts ...Option) (api.API, error) {
|
||||||
var resAPI api.Struct
|
resAPI := &API{}
|
||||||
settings := settings{
|
settings := settings{
|
||||||
modules: map[interface{}]fx.Option{},
|
modules: map[interface{}]fx.Option{},
|
||||||
invokes: make([]fx.Option, _nInvokes),
|
invokes: make([]fx.Option, _nInvokes),
|
||||||
@ -221,7 +221,7 @@ func New(ctx context.Context, opts ...Option) (api.API, error) {
|
|||||||
fx.Options(ctors...),
|
fx.Options(ctors...),
|
||||||
fx.Options(settings.invokes...),
|
fx.Options(settings.invokes...),
|
||||||
|
|
||||||
apiOption(&resAPI),
|
fx.Extract(resAPI),
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO: we probably should have a 'firewall' for Closing signal
|
// TODO: we probably should have a 'firewall' for Closing signal
|
||||||
@ -231,7 +231,7 @@ func New(ctx context.Context, opts ...Option) (api.API, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &resAPI, nil
|
return resAPI, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// In-memory / testing
|
// In-memory / testing
|
||||||
|
Loading…
Reference in New Issue
Block a user