WIP node api

This commit is contained in:
mcrakhman committed 2022-12-03 12:44:02 +01:00
1 parent 9ae5710854
commit 4062a76e21
9 files changed
+1801 -7

No files matched your search

File diff suppressed because it is too large. Load diff
+193
View File
@@ -0,0 +1,193 @@
// Code generated by protoc-gen-go-drpc. DO NOT EDIT.
// protoc-gen-go-drpc version: v0.0.32
// source: api/apiproto/protos/api.proto
package apiproto
import (
bytes "bytes"
context "context"
errors "errors"
jsonpb "github.com/gogo/protobuf/jsonpb"
proto "github.com/gogo/protobuf/proto"
drpc "storj.io/drpc"
drpcerr "storj.io/drpc/drpcerr"
)
type drpcEncoding_File_api_apiproto_protos_api_proto struct{}
func (drpcEncoding_File_api_apiproto_protos_api_proto) Marshal(msg drpc.Message) ([]byte, error) {
return proto.Marshal(msg.(proto.Message))
}
func (drpcEncoding_File_api_apiproto_protos_api_proto) Unmarshal(buf []byte, msg drpc.Message) error {
return proto.Unmarshal(buf, msg.(proto.Message))
}
func (drpcEncoding_File_api_apiproto_protos_api_proto) JSONMarshal(msg drpc.Message) ([]byte, error) {
var buf bytes.Buffer
err := new(jsonpb.Marshaler).Marshal(&buf, msg.(proto.Message))
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (drpcEncoding_File_api_apiproto_protos_api_proto) JSONUnmarshal(buf []byte, msg drpc.Message) error {
return jsonpb.Unmarshal(bytes.NewReader(buf), msg.(proto.Message))
}
type DRPCNodeApiClient interface {
DRPCConn() drpc.Conn
DumpTree(ctx context.Context, in *DumpTreeRequest) (*DumpTreeResponse, error)
AllTrees(ctx context.Context, in *AllTreesRequest) (*AllTreesResponse, error)
AllSpaces(ctx context.Context, in *AllSpacesRequest) (*AllSpacesResponse, error)
}
type drpcNodeApiClient struct {
cc drpc.Conn
}
func NewDRPCNodeApiClient(cc drpc.Conn) DRPCNodeApiClient {
return &drpcNodeApiClient{cc}
}
func (c *drpcNodeApiClient) DRPCConn() drpc.Conn { return c.cc }
func (c *drpcNodeApiClient) DumpTree(ctx context.Context, in *DumpTreeRequest) (*DumpTreeResponse, error) {
out := new(DumpTreeResponse)
err := c.cc.Invoke(ctx, "/api.NodeApi/DumpTree", drpcEncoding_File_api_apiproto_protos_api_proto{}, in, out)
if err != nil {
return nil, err
}
return out, nil
}
func (c *drpcNodeApiClient) AllTrees(ctx context.Context, in *AllTreesRequest) (*AllTreesResponse, error) {
out := new(AllTreesResponse)
err := c.cc.Invoke(ctx, "/api.NodeApi/AllTrees", drpcEncoding_File_api_apiproto_protos_api_proto{}, in, out)
if err != nil {
return nil, err
}
return out, nil
}
func (c *drpcNodeApiClient) AllSpaces(ctx context.Context, in *AllSpacesRequest) (*AllSpacesResponse, error) {
out := new(AllSpacesResponse)
err := c.cc.Invoke(ctx, "/api.NodeApi/AllSpaces", drpcEncoding_File_api_apiproto_protos_api_proto{}, in, out)
if err != nil {
return nil, err
}
return out, nil
}
type DRPCNodeApiServer interface {
DumpTree(context.Context, *DumpTreeRequest) (*DumpTreeResponse, error)
AllTrees(context.Context, *AllTreesRequest) (*AllTreesResponse, error)
AllSpaces(context.Context, *AllSpacesRequest) (*AllSpacesResponse, error)
}
type DRPCNodeApiUnimplementedServer struct{}
func (s *DRPCNodeApiUnimplementedServer) DumpTree(context.Context, *DumpTreeRequest) (*DumpTreeResponse, error) {
return nil, drpcerr.WithCode(errors.New("Unimplemented"), drpcerr.Unimplemented)
}
func (s *DRPCNodeApiUnimplementedServer) AllTrees(context.Context, *AllTreesRequest) (*AllTreesResponse, error) {
return nil, drpcerr.WithCode(errors.New("Unimplemented"), drpcerr.Unimplemented)
}
func (s *DRPCNodeApiUnimplementedServer) AllSpaces(context.Context, *AllSpacesRequest) (*AllSpacesResponse, error) {
return nil, drpcerr.WithCode(errors.New("Unimplemented"), drpcerr.Unimplemented)
}
type DRPCNodeApiDescription struct{}
func (DRPCNodeApiDescription) NumMethods() int { return 3 }
func (DRPCNodeApiDescription) Method(n int) (string, drpc.Encoding, drpc.Receiver, interface{}, bool) {
switch n {
case 0:
return "/api.NodeApi/DumpTree", drpcEncoding_File_api_apiproto_protos_api_proto{},
func(srv interface{}, ctx context.Context, in1, in2 interface{}) (drpc.Message, error) {
return srv.(DRPCNodeApiServer).
DumpTree(
ctx,
in1.(*DumpTreeRequest),
)
}, DRPCNodeApiServer.DumpTree, true
case 1:
return "/api.NodeApi/AllTrees", drpcEncoding_File_api_apiproto_protos_api_proto{},
func(srv interface{}, ctx context.Context, in1, in2 interface{}) (drpc.Message, error) {
return srv.(DRPCNodeApiServer).
AllTrees(
ctx,
in1.(*AllTreesRequest),
)
}, DRPCNodeApiServer.AllTrees, true
case 2:
return "/api.NodeApi/AllSpaces", drpcEncoding_File_api_apiproto_protos_api_proto{},
func(srv interface{}, ctx context.Context, in1, in2 interface{}) (drpc.Message, error) {
return srv.(DRPCNodeApiServer).
AllSpaces(
ctx,
in1.(*AllSpacesRequest),
)
}, DRPCNodeApiServer.AllSpaces, true
default:
return "", nil, nil, nil, false
}
}
func DRPCRegisterNodeApi(mux drpc.Mux, impl DRPCNodeApiServer) error {
return mux.Register(impl, DRPCNodeApiDescription{})
}
type DRPCNodeApi_DumpTreeStream interface {
drpc.Stream
SendAndClose(*DumpTreeResponse) error
}
type drpcNodeApi_DumpTreeStream struct {
drpc.Stream
}
func (x *drpcNodeApi_DumpTreeStream) SendAndClose(m *DumpTreeResponse) error {
if err := x.MsgSend(m, drpcEncoding_File_api_apiproto_protos_api_proto{}); err != nil {
return err
}
return x.CloseSend()
}
type DRPCNodeApi_AllTreesStream interface {
drpc.Stream
SendAndClose(*AllTreesResponse) error
}
type drpcNodeApi_AllTreesStream struct {
drpc.Stream
}
func (x *drpcNodeApi_AllTreesStream) SendAndClose(m *AllTreesResponse) error {
if err := x.MsgSend(m, drpcEncoding_File_api_apiproto_protos_api_proto{}); err != nil {
return err
}
return x.CloseSend()
}
type DRPCNodeApi_AllSpacesStream interface {
drpc.Stream
SendAndClose(*AllSpacesResponse) error
}
type drpcNodeApi_AllSpacesStream struct {
drpc.Stream
}
func (x *drpcNodeApi_AllSpacesStream) SendAndClose(m *AllSpacesResponse) error {
if err := x.MsgSend(m, drpcEncoding_File_api_apiproto_protos_api_proto{}); err != nil {
return err
}
return x.CloseSend()
}
+39
View File
@@ -0,0 +1,39 @@
syntax = "proto3";
package api;
option go_package = "api/apiproto";
service NodeApi {
rpc DumpTree(DumpTreeRequest) returns(DumpTreeResponse);
rpc AllTrees(AllTreesRequest) returns(AllTreesResponse);
rpc AllSpaces(AllSpacesRequest) returns(AllSpacesResponse);
}
message DumpTreeRequest {
string spaceId = 1;
string documentId = 2;
}
message DumpTreeResponse {
string dump = 1;
}
message AllTreesRequest {
string spaceId = 1;
}
message Tree {
string id = 1;
repeated string heads = 2;
}
message AllTreesResponse {
repeated Tree trees = 1;
}
message AllSpacesRequest {
}
message AllSpacesResponse {
repeated string spaceIds = 1;
}
+56
View File
@@ -0,0 +1,56 @@
package api
import (
"context"
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/treegetter"
"github.com/anytypeio/go-anytype-infrastructure-experiments/node/api/apiproto"
"github.com/anytypeio/go-anytype-infrastructure-experiments/node/nodespace"
"github.com/anytypeio/go-anytype-infrastructure-experiments/node/storage"
)
type rpcHandler struct {
treeCache treegetter.TreeGetter
spaceService nodespace.Service
storageService storage.NodeStorage
}
func (r *rpcHandler) DumpTree(ctx context.Context, request *apiproto.DumpTreeRequest) (resp *apiproto.DumpTreeResponse, err error) {
tree, err := r.treeCache.GetTree(context.Background(), request.SpaceId, request.DocumentId)
if err != nil {
return
}
dump, err := tree.DebugDump()
if err != nil {
return
}
resp = &apiproto.DumpTreeResponse{
Dump: dump,
}
return
}
func (r *rpcHandler) AllTrees(ctx context.Context, request *apiproto.AllTreesRequest) (resp *apiproto.AllTreesResponse, err error) {
space, err := r.spaceService.GetSpace(ctx, request.SpaceId)
if err != nil {
return
}
heads := space.DebugAllHeads()
var trees []*apiproto.Tree
for _, head := range heads {
trees = append(trees, &apiproto.Tree{
Id: head.Id,
Heads: head.Heads,
})
}
resp = &apiproto.AllTreesResponse{Trees: trees}
return
}
func (r *rpcHandler) AllSpaces(ctx context.Context, request *apiproto.AllSpacesRequest) (resp *apiproto.AllSpacesResponse, err error) {
ids, err := r.storageService.AllSpaceIds()
if err != nil {
return
}
resp = &apiproto.AllSpacesResponse{SpaceIds: ids}
return
}
+69
View File
@@ -0,0 +1,69 @@
package api
import (
"context"
"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/common/commonspace/storage"
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/treegetter"
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/config"
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/net/rpc/server"
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/net/secure"
"github.com/anytypeio/go-anytype-infrastructure-experiments/node/api/apiproto"
"github.com/anytypeio/go-anytype-infrastructure-experiments/node/nodespace"
nodestorage "github.com/anytypeio/go-anytype-infrastructure-experiments/node/storage"
"storj.io/drpc"
)
const CName = "api.service"
var log = logger.NewNamed(CName)
func New() Service {
return &service{BaseDrpcServer: server.NewBaseDrpcServer()}
}
type Service interface {
app.ComponentRunnable
drpc.Mux
}
type service struct {
transport secure.Service
cfg *config.Config
treeCache treegetter.TreeGetter
spaceService nodespace.Service
storageService nodestorage.NodeStorage
*server.BaseDrpcServer
}
func (s *service) Init(a *app.App) (err error) {
s.treeCache = a.MustComponent(treegetter.CName).(treegetter.TreeGetter)
s.spaceService = a.MustComponent(nodespace.CName).(nodespace.Service)
s.storageService = a.MustComponent(storage.CName).(nodestorage.NodeStorage)
s.cfg = a.MustComponent(config.CName).(*config.Config)
s.transport = a.MustComponent(secure.CName).(secure.Service)
return nil
}
func (s *service) Name() (name string) {
return CName
}
func (s *service) Run(ctx context.Context) (err error) {
err = s.BaseDrpcServer.Run(
ctx,
s.cfg.APIServer.ListenAddrs,
func(handler drpc.Handler) drpc.Handler {
return handler
},
s.transport.BasicListener)
if err != nil {
return
}
return apiproto.DRPCRegisterNodeApi(s, &rpcHandler{s.treeCache, s.spaceService, s.storageService})
}
func (s *service) Close(ctx context.Context) (err error) {
return s.BaseDrpcServer.Close(ctx)
}