WIP debug client
This commit is contained in:
parent
8b630dc7be
commit
17d24547aa
43
util/cmd/debug/api/client/service.go
Normal file
43
util/cmd/debug/api/client/service.go
Normal file
@ -0,0 +1,43 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/client/api/apiproto"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/app"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/util/cmd/debug/drpcclient"
|
||||
)
|
||||
|
||||
const CName = "api.client"
|
||||
|
||||
var log = logger.NewNamed(CName)
|
||||
|
||||
type Service interface {
|
||||
CreateSpace(ctx context.Context, ip string, request *apiproto.CreateSpaceRequest) (resp *apiproto.CreateSpaceResponse, err error)
|
||||
app.Component
|
||||
}
|
||||
|
||||
type service struct {
|
||||
client drpcclient.Service
|
||||
}
|
||||
|
||||
func New() Service {
|
||||
return &service{}
|
||||
}
|
||||
|
||||
func (s *service) Init(a *app.App) (err error) {
|
||||
s.client = a.MustComponent(drpcclient.CName).(drpcclient.Service)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *service) Name() (name string) {
|
||||
return CName
|
||||
}
|
||||
|
||||
func (s *service) CreateSpace(ctx context.Context, ip string, request *apiproto.CreateSpaceRequest) (resp *apiproto.CreateSpaceResponse, err error) {
|
||||
cl, err := s.client.GetClient(ctx, ip)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return cl.CreateSpace(ctx, request)
|
||||
}
|
||||
32
util/cmd/debug/api/node/service.go
Normal file
32
util/cmd/debug/api/node/service.go
Normal file
@ -0,0 +1,32 @@
|
||||
package node
|
||||
|
||||
import (
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/app"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/util/cmd/debug/drpcclient"
|
||||
)
|
||||
|
||||
const CName = "api.node"
|
||||
|
||||
var log = logger.NewNamed(CName)
|
||||
|
||||
type Service interface {
|
||||
app.Component
|
||||
}
|
||||
|
||||
type service struct {
|
||||
client drpcclient.Service
|
||||
}
|
||||
|
||||
func New() Service {
|
||||
return &service{}
|
||||
}
|
||||
|
||||
func (s *service) Init(a *app.App) (err error) {
|
||||
s.client = a.MustComponent(drpcclient.CName).(drpcclient.Service)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *service) Name() (name string) {
|
||||
return CName
|
||||
}
|
||||
@ -2,31 +2,47 @@ package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/client/api/apiproto"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/app"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/util/cmd/debug/client"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/util/cmd/debug/api/client"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/util/cmd/debug/api/node"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/util/cmd/debug/peers"
|
||||
)
|
||||
|
||||
const CName = "debug.api"
|
||||
|
||||
var log = logger.NewNamed(CName)
|
||||
|
||||
var (
|
||||
ErrNoSuchCommand = errors.New("no such command")
|
||||
ErrIncorrectPeerType = errors.New("incorrect peer type")
|
||||
ErrIncorrectParamsCount = errors.New("incorrect params count")
|
||||
)
|
||||
|
||||
type Command struct {
|
||||
Cmd func(server peers.Peer, params []string) (string, error)
|
||||
}
|
||||
|
||||
type Service interface {
|
||||
CreateSpace(ctx context.Context, ip string, request *apiproto.CreateSpaceRequest) (resp *apiproto.CreateSpaceResponse, err error)
|
||||
app.Component
|
||||
Call(server peers.Peer, cmdName string, params []string) (string, error)
|
||||
}
|
||||
|
||||
type service struct {
|
||||
client client.Service
|
||||
clientCommands map[string]Command
|
||||
nodeCommands map[string]Command
|
||||
}
|
||||
|
||||
func New() Service {
|
||||
return &service{}
|
||||
return &service{clientCommands: map[string]Command{}, nodeCommands: map[string]Command{}}
|
||||
}
|
||||
|
||||
func (s *service) Init(a *app.App) (err error) {
|
||||
s.client = a.MustComponent(client.CName).(client.Service)
|
||||
s.registerClientCommands(a.MustComponent(client.CName).(client.Service))
|
||||
s.registerNodeCommands(a.MustComponent(node.CName).(node.Service))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -34,10 +50,40 @@ func (s *service) Name() (name string) {
|
||||
return CName
|
||||
}
|
||||
|
||||
func (s *service) CreateSpace(ctx context.Context, ip string, request *apiproto.CreateSpaceRequest) (resp *apiproto.CreateSpaceResponse, err error) {
|
||||
cl, err := s.client.Get(ctx, ip)
|
||||
if err != nil {
|
||||
func (s *service) Call(server peers.Peer, cmdName string, params []string) (res string, err error) {
|
||||
var (
|
||||
cmd Command
|
||||
commands map[string]Command
|
||||
)
|
||||
|
||||
switch server.PeerType {
|
||||
case peers.PeerTypeClient:
|
||||
commands = s.clientCommands
|
||||
case peers.PeerTypeNode:
|
||||
commands = s.nodeCommands
|
||||
}
|
||||
cmd, ok := commands[cmdName]
|
||||
if !ok {
|
||||
err = ErrNoSuchCommand
|
||||
return
|
||||
}
|
||||
return cl.CreateSpace(ctx, request)
|
||||
return cmd.Cmd(server, params)
|
||||
}
|
||||
|
||||
func (s *service) registerClientCommands(client client.Service) {
|
||||
s.clientCommands["create-space"] = Command{Cmd: func(server peers.Peer, params []string) (res string, err error) {
|
||||
if len(params) != 0 {
|
||||
err = ErrIncorrectParamsCount
|
||||
return
|
||||
}
|
||||
resp, err := client.CreateSpace(context.Background(), server.Address, &apiproto.CreateSpaceRequest{})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
res = resp.Id
|
||||
return
|
||||
}}
|
||||
}
|
||||
|
||||
func (s *service) registerNodeCommands(node node.Service) {
|
||||
}
|
||||
|
||||
@ -7,7 +7,10 @@ import (
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/app"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/util/cmd/debug/api"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/util/cmd/debug/client"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/util/cmd/debug/api/client"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/util/cmd/debug/api/node"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/util/cmd/debug/drpcclient"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/util/cmd/debug/peers"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/util/cmd/debug/stdin"
|
||||
"go.uber.org/zap"
|
||||
"net/http"
|
||||
@ -71,7 +74,10 @@ func main() {
|
||||
}
|
||||
|
||||
func Bootstrap(a *app.App) {
|
||||
a.Register(client.New()).
|
||||
a.Register(drpcclient.New()).
|
||||
Register(peers.New()).
|
||||
Register(client.New()).
|
||||
Register(node.New()).
|
||||
Register(api.New()).
|
||||
Register(stdin.New())
|
||||
}
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
package client
|
||||
package drpcclient
|
||||
|
||||
import (
|
||||
"context"
|
||||
@ -12,11 +12,12 @@ import (
|
||||
)
|
||||
|
||||
type Service interface {
|
||||
Get(ctx context.Context, ip string) (apiproto.DRPCClientApiClient, error)
|
||||
GetClient(ctx context.Context, ip string) (apiproto.DRPCClientApiClient, error)
|
||||
GetNode(ctx context.Context, ip string) (apiproto.DRPCClientApiClient, error)
|
||||
app.ComponentRunnable
|
||||
}
|
||||
|
||||
const CName = "debug.client"
|
||||
const CName = "debug.drpcclient"
|
||||
|
||||
var log = logger.NewNamed(CName)
|
||||
|
||||
@ -53,7 +54,7 @@ func (s *service) Run(ctx context.Context) (err error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *service) Get(ctx context.Context, ip string) (apiproto.DRPCClientApiClient, error) {
|
||||
func (s *service) GetClient(ctx context.Context, ip string) (apiproto.DRPCClientApiClient, error) {
|
||||
v, err := s.cache.Get(ctx, ip)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -65,7 +66,23 @@ func (s *service) Get(ctx context.Context, ip string) (apiproto.DRPCClientApiCli
|
||||
return apiproto.NewDRPCClientApiClient(conn), nil
|
||||
}
|
||||
s.cache.Remove(ip)
|
||||
return s.Get(ctx, ip)
|
||||
return s.GetClient(ctx, ip)
|
||||
}
|
||||
|
||||
func (s *service) GetNode(ctx context.Context, ip string) (apiproto.DRPCClientApiClient, error) {
|
||||
v, err := s.cache.Get(ctx, ip)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
conn := v.(*drpcconn.Conn)
|
||||
select {
|
||||
case <-conn.Closed():
|
||||
default:
|
||||
panic("should return node")
|
||||
return apiproto.NewDRPCClientApiClient(conn), nil
|
||||
}
|
||||
s.cache.Remove(ip)
|
||||
return s.GetClient(ctx, ip)
|
||||
}
|
||||
|
||||
func (s *service) Close(ctx context.Context) (err error) {
|
||||
59
util/cmd/debug/peers/peers.go
Normal file
59
util/cmd/debug/peers/peers.go
Normal file
@ -0,0 +1,59 @@
|
||||
package peers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/app"
|
||||
)
|
||||
|
||||
type PeerType int
|
||||
|
||||
const (
|
||||
PeerTypeClient PeerType = iota
|
||||
PeerTypeNode
|
||||
)
|
||||
|
||||
const CName = "debug.peers"
|
||||
|
||||
var ErrNoSuchPeer = errors.New("no such peer")
|
||||
|
||||
type Service interface {
|
||||
app.Component
|
||||
Get(alias string) (Peer, error)
|
||||
}
|
||||
|
||||
type Peer struct {
|
||||
PeerType PeerType
|
||||
Address string
|
||||
}
|
||||
|
||||
type service struct {
|
||||
peerMap map[string]Peer
|
||||
}
|
||||
|
||||
func New() Service {
|
||||
return &service{peerMap: map[string]Peer{}}
|
||||
}
|
||||
|
||||
func (s *service) Init(a *app.App) (err error) {
|
||||
s.peerMap["client1"] = Peer{
|
||||
PeerType: PeerTypeClient,
|
||||
Address: "127.0.0.1:8090",
|
||||
}
|
||||
s.peerMap["client2"] = Peer{
|
||||
PeerType: PeerTypeClient,
|
||||
Address: "127.0.0.1:8091",
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s *service) Name() (name string) {
|
||||
return CName
|
||||
}
|
||||
|
||||
func (s *service) Get(alias string) (Peer, error) {
|
||||
peer, ok := s.peerMap[alias]
|
||||
if !ok {
|
||||
return Peer{}, ErrNoSuchPeer
|
||||
}
|
||||
return peer, nil
|
||||
}
|
||||
@ -3,11 +3,12 @@ package stdin
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/client/api/apiproto"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/app"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/util/cmd/debug/api"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/util/cmd/debug/peers"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const CName = "debug.stdin"
|
||||
@ -19,7 +20,8 @@ type Service interface {
|
||||
}
|
||||
|
||||
type service struct {
|
||||
api api.Service
|
||||
api api.Service
|
||||
peers peers.Service
|
||||
}
|
||||
|
||||
func New() Service {
|
||||
@ -28,6 +30,7 @@ func New() Service {
|
||||
|
||||
func (s *service) Init(a *app.App) (err error) {
|
||||
s.api = a.MustComponent(api.CName).(api.Service)
|
||||
s.peers = a.MustComponent(peers.CName).(peers.Service)
|
||||
return
|
||||
}
|
||||
|
||||
@ -45,21 +48,29 @@ func (s *service) Close(ctx context.Context) (err error) {
|
||||
}
|
||||
|
||||
func (s *service) readStdin() {
|
||||
// create new reader from stdin
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
// start infinite loop to continuously listen to input
|
||||
for {
|
||||
// read by one line (enter pressed)
|
||||
str, err := reader.ReadString('\n')
|
||||
// check for errors
|
||||
if err != nil {
|
||||
// close channel just to inform others
|
||||
log.Errorf("Error in read string: %s", err)
|
||||
return
|
||||
}
|
||||
log.Debug(str)
|
||||
// trimming newline
|
||||
str = str[:len(str)-1]
|
||||
|
||||
res, err := s.api.CreateSpace(context.Background(), "127.0.0.1:8090", &apiproto.CreateSpaceRequest{})
|
||||
log.Debug(str)
|
||||
split := strings.Split(str, " ")
|
||||
if len(split) < 2 {
|
||||
log.Error("incorrect number of arguments")
|
||||
continue
|
||||
}
|
||||
|
||||
peer, err := s.peers.Get(split[0])
|
||||
if err != nil {
|
||||
log.Error("no such peer")
|
||||
continue
|
||||
}
|
||||
res, err := s.api.Call(peer, split[1], split[2:])
|
||||
if err != nil {
|
||||
log.Errorf("Error in performing request: %s", err)
|
||||
return
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user