make the file service multi-tenant and add read endpoint

This commit is contained in:
Asim Aslam
2021-05-23 15:40:34 +01:00
parent b7980a1ed6
commit 525ecc9dea
4 changed files with 299 additions and 78 deletions

View File

@@ -2,13 +2,13 @@ package handler
import (
"context"
"errors"
"strings"
"github.com/micro/micro/v3/service/auth"
"github.com/micro/micro/v3/service/errors"
log "github.com/micro/micro/v3/service/logger"
"github.com/micro/micro/v3/service/model"
file "github.com/micro/services/file/proto"
"github.com/micro/services/pkg/tenant"
)
type File struct {
@@ -32,50 +32,95 @@ func NewFile() *File {
}
}
func (e *File) Save(ctx context.Context, req *file.SaveRequest, rsp *file.SaveResponse) error {
// @todo return proper micro errors
acc, ok := auth.AccountFromContext(ctx)
func (e *File) Read(ctx context.Context, req *file.ReadRequest, rsp *file.ReadResponse) error {
log.Info("Received File.Read request")
if len(req.Path) == 0 {
return errors.BadRequest("file.read", "missing file path")
}
tenantId, ok := tenant.FromContext(ctx)
if !ok {
return errors.New("File.Save requires authentication")
tenantId = "micro"
}
var files []*file.Record
project := tenantId + "/" + req.Project
// read all the files for the project
err := e.db.Read(model.QueryEquals("project", project), &files)
if err != nil {
return err
}
// filter the file
for _, file := range files {
if file.Path == req.Path {
// strip the tenant id
file.Id = strings.TrimPrefix(file.Id, tenantId+"/")
file.Project = strings.TrimPrefix(file.Project, tenantId+"/")
rsp.File = file
}
}
return nil
}
func (e *File) Save(ctx context.Context, req *file.SaveRequest, rsp *file.SaveResponse) error {
tenantId, ok := tenant.FromContext(ctx)
if !ok {
tenantId = "micro"
}
log.Info("Received File.Save request")
for _, reqFile := range req.Files {
f := file.Record{}
err := e.db.Read(model.QueryEquals("Id", reqFile.Id), &f)
if err != nil && err != model.ErrorNotFound {
return err
}
// if file exists check ownership
if f.Id != "" && f.Owner != acc.ID {
return errors.New("Not authorized")
}
err = e.db.Create(reqFile)
// prefix the tenant
reqFile.Id = tenantId + "/" + reqFile.Id
reqFile.Project = tenantId + "/" + reqFile.Project
// create the file
err := e.db.Create(reqFile)
if err != nil {
return err
}
}
return nil
}
func (e *File) List(ctx context.Context, req *file.ListRequest, rsp *file.ListResponse) error {
log.Info("Received File.List request")
rsp.Files = []*file.Record{}
err := e.db.Read(model.QueryEquals("project", req.GetProject()), &rsp.Files)
if err != nil {
tenantId, ok := tenant.FromContext(ctx)
if !ok {
tenantId = "micro"
}
// prefix tenant id
project := tenantId + "/" + req.Project
var files []*file.Record
// read all the files for the project
if err := e.db.Read(model.QueryEquals("project", project), &files); err != nil {
return err
}
// @todo funnily while this is the archetypical
// query for the KV store interface, it's not supported by the model
// so we do client side filtering here
if req.Path != "" {
filtered := []*file.Record{}
for _, file := range rsp.Files {
if strings.HasPrefix(file.Path, req.Path) {
filtered = append(filtered, file)
}
for _, file := range rsp.Files {
// strip the prefixes
file.Id = strings.TrimPrefix(file.Id, tenantId+"/")
file.Project = strings.TrimPrefix(file.Project, tenantId+"/")
// if requesting all files or path matches
if req.Path == "" || strings.HasPrefix(file.Path, req.Path) {
rsp.Files = append(rsp.Files, file)
}
rsp.Files = filtered
}
return nil
}

View File

@@ -1,7 +1,7 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.26.0
// protoc v3.6.1
// protoc v3.15.6
// source: proto/file.proto
package file
@@ -137,6 +137,112 @@ func (x *Record) GetOwner() string {
return ""
}
// Read a file by path
type ReadRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
// project name
Project string `protobuf:"bytes,1,opt,name=project,proto3" json:"project,omitempty"`
// path to the file
Path string `protobuf:"bytes,2,opt,name=path,proto3" json:"path,omitempty"`
}
func (x *ReadRequest) Reset() {
*x = ReadRequest{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_file_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *ReadRequest) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*ReadRequest) ProtoMessage() {}
func (x *ReadRequest) ProtoReflect() protoreflect.Message {
mi := &file_proto_file_proto_msgTypes[1]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use ReadRequest.ProtoReflect.Descriptor instead.
func (*ReadRequest) Descriptor() ([]byte, []int) {
return file_proto_file_proto_rawDescGZIP(), []int{1}
}
func (x *ReadRequest) GetProject() string {
if x != nil {
return x.Project
}
return ""
}
func (x *ReadRequest) GetPath() string {
if x != nil {
return x.Path
}
return ""
}
type ReadResponse struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
// Returns the file
File *Record `protobuf:"bytes,1,opt,name=file,proto3" json:"file,omitempty"`
}
func (x *ReadResponse) Reset() {
*x = ReadResponse{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_file_proto_msgTypes[2]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *ReadResponse) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*ReadResponse) ProtoMessage() {}
func (x *ReadResponse) ProtoReflect() protoreflect.Message {
mi := &file_proto_file_proto_msgTypes[2]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use ReadResponse.ProtoReflect.Descriptor instead.
func (*ReadResponse) Descriptor() ([]byte, []int) {
return file_proto_file_proto_rawDescGZIP(), []int{2}
}
func (x *ReadResponse) GetFile() *Record {
if x != nil {
return x.File
}
return nil
}
// The save endpoint lets you batch save text file.
type SaveRequest struct {
state protoimpl.MessageState
@@ -149,7 +255,7 @@ type SaveRequest struct {
func (x *SaveRequest) Reset() {
*x = SaveRequest{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_file_proto_msgTypes[1]
mi := &file_proto_file_proto_msgTypes[3]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@@ -162,7 +268,7 @@ func (x *SaveRequest) String() string {
func (*SaveRequest) ProtoMessage() {}
func (x *SaveRequest) ProtoReflect() protoreflect.Message {
mi := &file_proto_file_proto_msgTypes[1]
mi := &file_proto_file_proto_msgTypes[3]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@@ -175,7 +281,7 @@ func (x *SaveRequest) ProtoReflect() protoreflect.Message {
// Deprecated: Use SaveRequest.ProtoReflect.Descriptor instead.
func (*SaveRequest) Descriptor() ([]byte, []int) {
return file_proto_file_proto_rawDescGZIP(), []int{1}
return file_proto_file_proto_rawDescGZIP(), []int{3}
}
func (x *SaveRequest) GetFiles() []*Record {
@@ -194,7 +300,7 @@ type SaveResponse struct {
func (x *SaveResponse) Reset() {
*x = SaveResponse{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_file_proto_msgTypes[2]
mi := &file_proto_file_proto_msgTypes[4]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@@ -207,7 +313,7 @@ func (x *SaveResponse) String() string {
func (*SaveResponse) ProtoMessage() {}
func (x *SaveResponse) ProtoReflect() protoreflect.Message {
mi := &file_proto_file_proto_msgTypes[2]
mi := &file_proto_file_proto_msgTypes[4]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@@ -220,7 +326,7 @@ func (x *SaveResponse) ProtoReflect() protoreflect.Message {
// Deprecated: Use SaveResponse.ProtoReflect.Descriptor instead.
func (*SaveResponse) Descriptor() ([]byte, []int) {
return file_proto_file_proto_rawDescGZIP(), []int{2}
return file_proto_file_proto_rawDescGZIP(), []int{4}
}
// List file by their project and optionally a path.
@@ -241,7 +347,7 @@ type ListRequest struct {
func (x *ListRequest) Reset() {
*x = ListRequest{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_file_proto_msgTypes[3]
mi := &file_proto_file_proto_msgTypes[5]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@@ -254,7 +360,7 @@ func (x *ListRequest) String() string {
func (*ListRequest) ProtoMessage() {}
func (x *ListRequest) ProtoReflect() protoreflect.Message {
mi := &file_proto_file_proto_msgTypes[3]
mi := &file_proto_file_proto_msgTypes[5]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@@ -267,7 +373,7 @@ func (x *ListRequest) ProtoReflect() protoreflect.Message {
// Deprecated: Use ListRequest.ProtoReflect.Descriptor instead.
func (*ListRequest) Descriptor() ([]byte, []int) {
return file_proto_file_proto_rawDescGZIP(), []int{3}
return file_proto_file_proto_rawDescGZIP(), []int{5}
}
func (x *ListRequest) GetProject() string {
@@ -295,7 +401,7 @@ type ListResponse struct {
func (x *ListResponse) Reset() {
*x = ListResponse{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_file_proto_msgTypes[4]
mi := &file_proto_file_proto_msgTypes[6]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@@ -308,7 +414,7 @@ func (x *ListResponse) String() string {
func (*ListResponse) ProtoMessage() {}
func (x *ListResponse) ProtoReflect() protoreflect.Message {
mi := &file_proto_file_proto_msgTypes[4]
mi := &file_proto_file_proto_msgTypes[6]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@@ -321,7 +427,7 @@ func (x *ListResponse) ProtoReflect() protoreflect.Message {
// Deprecated: Use ListResponse.ProtoReflect.Descriptor instead.
func (*ListResponse) Descriptor() ([]byte, []int) {
return file_proto_file_proto_rawDescGZIP(), []int{4}
return file_proto_file_proto_rawDescGZIP(), []int{6}
}
func (x *ListResponse) GetFiles() []*Record {
@@ -350,26 +456,36 @@ var file_proto_file_proto_rawDesc = []byte{
0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x75, 0x70, 0x64, 0x61, 0x74,
0x65, 0x64, 0x18, 0x08, 0x20, 0x01, 0x28, 0x03, 0x52, 0x07, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65,
0x64, 0x12, 0x14, 0x0a, 0x05, 0x6f, 0x77, 0x6e, 0x65, 0x72, 0x18, 0x09, 0x20, 0x01, 0x28, 0x09,
0x52, 0x05, 0x6f, 0x77, 0x6e, 0x65, 0x72, 0x22, 0x31, 0x0a, 0x0b, 0x53, 0x61, 0x76, 0x65, 0x52,
0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x22, 0x0a, 0x05, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x18,
0x52, 0x05, 0x6f, 0x77, 0x6e, 0x65, 0x72, 0x22, 0x3b, 0x0a, 0x0b, 0x52, 0x65, 0x61, 0x64, 0x52,
0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x70, 0x72, 0x6f, 0x6a, 0x65, 0x63,
0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x70, 0x72, 0x6f, 0x6a, 0x65, 0x63, 0x74,
0x12, 0x12, 0x0a, 0x04, 0x70, 0x61, 0x74, 0x68, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04,
0x70, 0x61, 0x74, 0x68, 0x22, 0x30, 0x0a, 0x0c, 0x52, 0x65, 0x61, 0x64, 0x52, 0x65, 0x73, 0x70,
0x6f, 0x6e, 0x73, 0x65, 0x12, 0x20, 0x0a, 0x04, 0x66, 0x69, 0x6c, 0x65, 0x18, 0x01, 0x20, 0x01,
0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x66, 0x69, 0x6c, 0x65, 0x2e, 0x52, 0x65, 0x63, 0x6f, 0x72, 0x64,
0x52, 0x04, 0x66, 0x69, 0x6c, 0x65, 0x22, 0x31, 0x0a, 0x0b, 0x53, 0x61, 0x76, 0x65, 0x52, 0x65,
0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x22, 0x0a, 0x05, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x18, 0x01,
0x20, 0x03, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x66, 0x69, 0x6c, 0x65, 0x2e, 0x52, 0x65, 0x63, 0x6f,
0x72, 0x64, 0x52, 0x05, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x22, 0x0e, 0x0a, 0x0c, 0x53, 0x61, 0x76,
0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x3b, 0x0a, 0x0b, 0x4c, 0x69, 0x73,
0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x70, 0x72, 0x6f, 0x6a,
0x65, 0x63, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x70, 0x72, 0x6f, 0x6a, 0x65,
0x63, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x70, 0x61, 0x74, 0x68, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09,
0x52, 0x04, 0x70, 0x61, 0x74, 0x68, 0x22, 0x32, 0x0a, 0x0c, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65,
0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x22, 0x0a, 0x05, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x18,
0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x66, 0x69, 0x6c, 0x65, 0x2e, 0x52, 0x65, 0x63,
0x6f, 0x72, 0x64, 0x52, 0x05, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x22, 0x0e, 0x0a, 0x0c, 0x53, 0x61,
0x76, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x3b, 0x0a, 0x0b, 0x4c, 0x69,
0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x70, 0x72, 0x6f,
0x6a, 0x65, 0x63, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x70, 0x72, 0x6f, 0x6a,
0x65, 0x63, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x70, 0x61, 0x74, 0x68, 0x18, 0x02, 0x20, 0x01, 0x28,
0x09, 0x52, 0x04, 0x70, 0x61, 0x74, 0x68, 0x22, 0x32, 0x0a, 0x0c, 0x4c, 0x69, 0x73, 0x74, 0x52,
0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x22, 0x0a, 0x05, 0x66, 0x69, 0x6c, 0x65, 0x73,
0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x66, 0x69, 0x6c, 0x65, 0x2e, 0x52, 0x65,
0x63, 0x6f, 0x72, 0x64, 0x52, 0x05, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x32, 0x68, 0x0a, 0x04, 0x46,
0x69, 0x6c, 0x65, 0x12, 0x2f, 0x0a, 0x04, 0x53, 0x61, 0x76, 0x65, 0x12, 0x11, 0x2e, 0x66, 0x69,
0x6c, 0x65, 0x2e, 0x53, 0x61, 0x76, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x12,
0x2e, 0x66, 0x69, 0x6c, 0x65, 0x2e, 0x53, 0x61, 0x76, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e,
0x73, 0x65, 0x22, 0x00, 0x12, 0x2f, 0x0a, 0x04, 0x4c, 0x69, 0x73, 0x74, 0x12, 0x11, 0x2e, 0x66,
0x69, 0x6c, 0x65, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a,
0x12, 0x2e, 0x66, 0x69, 0x6c, 0x65, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f,
0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x0e, 0x5a, 0x0c, 0x2e, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f,
0x3b, 0x66, 0x69, 0x6c, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
0x6f, 0x72, 0x64, 0x52, 0x05, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x32, 0x99, 0x01, 0x0a, 0x04, 0x46,
0x69, 0x6c, 0x65, 0x12, 0x2f, 0x0a, 0x04, 0x52, 0x65, 0x61, 0x64, 0x12, 0x11, 0x2e, 0x66, 0x69,
0x6c, 0x65, 0x2e, 0x52, 0x65, 0x61, 0x64, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x12,
0x2e, 0x66, 0x69, 0x6c, 0x65, 0x2e, 0x52, 0x65, 0x61, 0x64, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e,
0x73, 0x65, 0x22, 0x00, 0x12, 0x2f, 0x0a, 0x04, 0x53, 0x61, 0x76, 0x65, 0x12, 0x11, 0x2e, 0x66,
0x69, 0x6c, 0x65, 0x2e, 0x53, 0x61, 0x76, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a,
0x12, 0x2e, 0x66, 0x69, 0x6c, 0x65, 0x2e, 0x53, 0x61, 0x76, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f,
0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x2f, 0x0a, 0x04, 0x4c, 0x69, 0x73, 0x74, 0x12, 0x11, 0x2e,
0x66, 0x69, 0x6c, 0x65, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74,
0x1a, 0x12, 0x2e, 0x66, 0x69, 0x6c, 0x65, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x73, 0x70,
0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x0e, 0x5a, 0x0c, 0x2e, 0x2f, 0x70, 0x72, 0x6f, 0x74,
0x6f, 0x3b, 0x66, 0x69, 0x6c, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
@@ -384,26 +500,31 @@ func file_proto_file_proto_rawDescGZIP() []byte {
return file_proto_file_proto_rawDescData
}
var file_proto_file_proto_msgTypes = make([]protoimpl.MessageInfo, 5)
var file_proto_file_proto_msgTypes = make([]protoimpl.MessageInfo, 7)
var file_proto_file_proto_goTypes = []interface{}{
(*Record)(nil), // 0: file.Record
(*SaveRequest)(nil), // 1: file.SaveRequest
(*SaveResponse)(nil), // 2: file.SaveResponse
(*ListRequest)(nil), // 3: file.ListRequest
(*ListResponse)(nil), // 4: file.ListResponse
(*ReadRequest)(nil), // 1: file.ReadRequest
(*ReadResponse)(nil), // 2: file.ReadResponse
(*SaveRequest)(nil), // 3: file.SaveRequest
(*SaveResponse)(nil), // 4: file.SaveResponse
(*ListRequest)(nil), // 5: file.ListRequest
(*ListResponse)(nil), // 6: file.ListResponse
}
var file_proto_file_proto_depIdxs = []int32{
0, // 0: file.SaveRequest.files:type_name -> file.Record
0, // 1: file.ListResponse.files:type_name -> file.Record
1, // 2: file.File.Save:input_type -> file.SaveRequest
3, // 3: file.File.List:input_type -> file.ListRequest
2, // 4: file.File.Save:output_type -> file.SaveResponse
4, // 5: file.File.List:output_type -> file.ListResponse
4, // [4:6] is the sub-list for method output_type
2, // [2:4] is the sub-list for method input_type
2, // [2:2] is the sub-list for extension type_name
2, // [2:2] is the sub-list for extension extendee
0, // [0:2] is the sub-list for field type_name
0, // 0: file.ReadResponse.file:type_name -> file.Record
0, // 1: file.SaveRequest.files:type_name -> file.Record
0, // 2: file.ListResponse.files:type_name -> file.Record
1, // 3: file.File.Read:input_type -> file.ReadRequest
3, // 4: file.File.Save:input_type -> file.SaveRequest
5, // 5: file.File.List:input_type -> file.ListRequest
2, // 6: file.File.Read:output_type -> file.ReadResponse
4, // 7: file.File.Save:output_type -> file.SaveResponse
6, // 8: file.File.List:output_type -> file.ListResponse
6, // [6:9] is the sub-list for method output_type
3, // [3:6] is the sub-list for method input_type
3, // [3:3] is the sub-list for extension type_name
3, // [3:3] is the sub-list for extension extendee
0, // [0:3] is the sub-list for field type_name
}
func init() { file_proto_file_proto_init() }
@@ -425,7 +546,7 @@ func file_proto_file_proto_init() {
}
}
file_proto_file_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*SaveRequest); i {
switch v := v.(*ReadRequest); i {
case 0:
return &v.state
case 1:
@@ -437,7 +558,7 @@ func file_proto_file_proto_init() {
}
}
file_proto_file_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*SaveResponse); i {
switch v := v.(*ReadResponse); i {
case 0:
return &v.state
case 1:
@@ -449,7 +570,7 @@ func file_proto_file_proto_init() {
}
}
file_proto_file_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*ListRequest); i {
switch v := v.(*SaveRequest); i {
case 0:
return &v.state
case 1:
@@ -461,6 +582,30 @@ func file_proto_file_proto_init() {
}
}
file_proto_file_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*SaveResponse); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_proto_file_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*ListRequest); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_proto_file_proto_msgTypes[6].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*ListResponse); i {
case 0:
return &v.state
@@ -479,7 +624,7 @@ func file_proto_file_proto_init() {
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_proto_file_proto_rawDesc,
NumEnums: 0,
NumMessages: 5,
NumMessages: 7,
NumExtensions: 0,
NumServices: 1,
},

View File

@@ -42,6 +42,7 @@ func NewFileEndpoints() []*api.Endpoint {
// Client API for File service
type FileService interface {
Read(ctx context.Context, in *ReadRequest, opts ...client.CallOption) (*ReadResponse, error)
Save(ctx context.Context, in *SaveRequest, opts ...client.CallOption) (*SaveResponse, error)
List(ctx context.Context, in *ListRequest, opts ...client.CallOption) (*ListResponse, error)
}
@@ -58,6 +59,16 @@ func NewFileService(name string, c client.Client) FileService {
}
}
func (c *fileService) Read(ctx context.Context, in *ReadRequest, opts ...client.CallOption) (*ReadResponse, error) {
req := c.c.NewRequest(c.name, "File.Read", in)
out := new(ReadResponse)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *fileService) Save(ctx context.Context, in *SaveRequest, opts ...client.CallOption) (*SaveResponse, error) {
req := c.c.NewRequest(c.name, "File.Save", in)
out := new(SaveResponse)
@@ -81,12 +92,14 @@ func (c *fileService) List(ctx context.Context, in *ListRequest, opts ...client.
// Server API for File service
type FileHandler interface {
Read(context.Context, *ReadRequest, *ReadResponse) error
Save(context.Context, *SaveRequest, *SaveResponse) error
List(context.Context, *ListRequest, *ListResponse) error
}
func RegisterFileHandler(s server.Server, hdlr FileHandler, opts ...server.HandlerOption) error {
type file interface {
Read(ctx context.Context, in *ReadRequest, out *ReadResponse) error
Save(ctx context.Context, in *SaveRequest, out *SaveResponse) error
List(ctx context.Context, in *ListRequest, out *ListResponse) error
}
@@ -101,6 +114,10 @@ type fileHandler struct {
FileHandler
}
func (h *fileHandler) Read(ctx context.Context, in *ReadRequest, out *ReadResponse) error {
return h.FileHandler.Read(ctx, in, out)
}
func (h *fileHandler) Save(ctx context.Context, in *SaveRequest, out *SaveResponse) error {
return h.FileHandler.Save(ctx, in, out)
}

View File

@@ -5,6 +5,7 @@ package file;
option go_package = "./proto;file";
service File {
rpc Read(ReadRequest) returns (ReadResponse) {}
rpc Save(SaveRequest) returns (SaveResponse) {}
rpc List(ListRequest) returns (ListResponse) {}
}
@@ -27,6 +28,19 @@ message Record {
string owner = 9;
}
// Read a file by path
message ReadRequest {
// project name
string project = 1;
// path to the file
string path = 2;
}
message ReadResponse {
// Returns the file
Record file = 1;
}
// The save endpoint lets you batch save text file.
message SaveRequest {
repeated Record files = 1;