Notes Service (#12)

* Notes Service

* go mod tidy

* go mod tidy
This commit is contained in:
ben-toogood
2020-10-16 13:13:18 +01:00
committed by GitHub
parent 2083802ed5
commit 25993c656a
7 changed files with 929 additions and 61 deletions

25
notes/README.md Normal file
View File

@@ -0,0 +1,25 @@
# Notes Service
Notes service is an RPC service which offers CRUD for notes. It demonstrates usage of the store, errors and logger pacakges. Example usage:
```bash
> micro notes create --title="HelloWorld" --text="MyFirstNote"
{
"id": "6d3fa5c0-6e79-4418-a72a-c1650efb65d2"
}
> micro notes update --id=6d3fa5c0-6e79-4418-a72a-c1650efb65d2 --title="HelloWorld" --text="MyFirstNote (v2)"
{}
> micro notes list
{
"notes": [
{
"id": "6d3fa5c0-6e79-4418-a72a-c1650efb65d2",
"created": "1602849877",
"title": "HelloWorld",
"text": "MyFirstNote (v2)"
}
]
}
> micro notes delete --id=6d3fa5c0-6e79-4418-a72a-c1650efb65d2
{}
```

161
notes/handler/handler.go Normal file
View File

@@ -0,0 +1,161 @@
package handler
import (
"context"
"encoding/json"
"io"
"time"
"github.com/google/uuid"
"github.com/micro/micro/v3/service/errors"
"github.com/micro/micro/v3/service/logger"
"github.com/micro/micro/v3/service/store"
pb "github.com/micro/services/notes/proto"
)
const storePrefix = "notes/"
// New returns an initialized notes handler
func New() pb.NotesHandler {
return new(handler)
}
type handler struct{}
// List all the notes
func (h *handler) List(ctx context.Context, req *pb.ListRequest, rsp *pb.ListResponse) error {
// query the store
recs, err := store.Read("", store.Prefix(storePrefix))
if err != nil {
logger.Errorf("Error reading notes from the store: %v", err)
return errors.InternalServerError("notes.List.Unknown", "Error reading from the store")
}
// serialize the response
rsp.Notes = make([]*pb.Note, len(recs))
for i, r := range recs {
var note pb.Note
if err := json.Unmarshal(r.Value, &note); err != nil {
logger.Errorf("Error unmarshaling note: %v", err)
return errors.InternalServerError("notes.List.Unknown", "Error unmarshaling note")
}
rsp.Notes[i] = &note
}
return nil
}
// Create a note
func (h *handler) Create(ctx context.Context, req *pb.CreateRequest, rsp *pb.CreateResponse) error {
// validate the request
if len(req.Title) == 0 {
return errors.BadRequest("notes.Create.MissingTitle", "Missing title")
}
// construct the note
note := &pb.Note{
Id: uuid.New().String(),
Created: time.Now().Unix(),
Title: req.Title,
Text: req.Text,
}
// marshal the note to bytes
bytes, err := json.Marshal(note)
if err != nil {
logger.Errorf("Error marshaling note: %v", err)
return errors.InternalServerError("notes.Create.Unknown", "Error marshaling note")
}
// write to the store
key := storePrefix + note.Id
if err := store.Write(&store.Record{Key: key, Value: bytes}); err != nil {
logger.Errorf("Error writing to store: %v", err)
return errors.InternalServerError("notes.Create.Unknown", "Error writing to store")
}
// return the id
rsp.Id = note.Id
return nil
}
// Delete a note
func (h *handler) Delete(ctx context.Context, req *pb.DeleteRequest, rsp *pb.DeleteResponse) error {
// validate the request
if len(req.Id) == 0 {
return errors.BadRequest("notes.Delete.MissingID", "Missing id")
}
// delete the note from the store
if err := store.Delete(storePrefix + req.Id); err == store.ErrNotFound {
return errors.NotFound("notes.Delete.InvalidID", "Note not found with this ID")
} else if err != nil {
logger.Errorf("Error deleting from the store: %v", err)
return errors.InternalServerError("notes.Delete.Unknown", "Error deleting from the store")
}
return nil
}
// Update a note
func (h *handler) Update(ctx context.Context, req *pb.UpdateRequest, rsp *pb.UpdateResponse) error {
// validate the request
if len(req.Id) == 0 {
return errors.BadRequest("notes.Update.MissingID", "Missing id")
}
if len(req.Title) == 0 {
return errors.BadRequest("notes.Update.MissingTitle", "Missing title")
}
// read the note from the store
recs, err := store.Read(storePrefix + req.Id)
if err == store.ErrNotFound {
return errors.NotFound("notes.Update.InvalidID", "Note not found with this ID")
} else if err != nil {
logger.Errorf("Error reading from the store: %v", err)
return errors.InternalServerError("notes.Update.Unknown", "Error reading from the store")
}
// unmarshal the note
var note pb.Note
if err := json.Unmarshal(recs[0].Value, &note); err != nil {
logger.Errorf("Error unmarshaling note: %v", err)
return errors.InternalServerError("notes.Update.Unknown", "Error unmarshaling note")
}
// assign the new title and text
note.Title = req.Title
note.Text = req.Text
// marshal the note to bytes
bytes, err := json.Marshal(note)
if err != nil {
logger.Errorf("Error marshaling note: %v", err)
return errors.InternalServerError("notes.Update.Unknown", "Error marshaling note")
}
// write to the store
key := storePrefix + note.Id
if err := store.Write(&store.Record{Key: key, Value: bytes}); err != nil {
logger.Errorf("Error writing to store: %v", err)
return errors.InternalServerError("notes.Update.Unknown", "Error writing to store")
}
return nil
}
// UpdateStream updates a note every time an update is sent on the stream
func (h *handler) UpdateStream(ctx context.Context, stream pb.Notes_UpdateStreamStream) error {
for {
uReq, err := stream.Recv()
if err == io.EOF {
return nil
} else if err != nil {
return errors.InternalServerError("notes.UpdateStream.Unknown", "Error reading from stream")
}
if err := h.Update(ctx, uReq, nil); err != nil {
return err
}
}
}

22
notes/main.go Normal file
View File

@@ -0,0 +1,22 @@
package main
import (
"github.com/micro/micro/v3/service"
"github.com/micro/micro/v3/service/logger"
"github.com/micro/services/notes/handler"
pb "github.com/micro/services/notes/proto"
)
func main() {
srv := service.New(
service.Name("notes"),
service.Version("latest"),
)
pb.RegisterNotesHandler(srv.Server(), handler.New())
if err := srv.Run(); err != nil {
logger.Fatal(err)
}
}

436
notes/proto/notes.pb.go Normal file
View File

@@ -0,0 +1,436 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// source: notes/proto/notes.proto
package notes
import (
fmt "fmt"
proto "github.com/golang/protobuf/proto"
math "math"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
type Note struct {
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
Created int64 `protobuf:"varint,2,opt,name=created,proto3" json:"created,omitempty"`
Title string `protobuf:"bytes,3,opt,name=title,proto3" json:"title,omitempty"`
Text string `protobuf:"bytes,4,opt,name=text,proto3" json:"text,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *Note) Reset() { *m = Note{} }
func (m *Note) String() string { return proto.CompactTextString(m) }
func (*Note) ProtoMessage() {}
func (*Note) Descriptor() ([]byte, []int) {
return fileDescriptor_7f19915b807268a5, []int{0}
}
func (m *Note) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_Note.Unmarshal(m, b)
}
func (m *Note) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_Note.Marshal(b, m, deterministic)
}
func (m *Note) XXX_Merge(src proto.Message) {
xxx_messageInfo_Note.Merge(m, src)
}
func (m *Note) XXX_Size() int {
return xxx_messageInfo_Note.Size(m)
}
func (m *Note) XXX_DiscardUnknown() {
xxx_messageInfo_Note.DiscardUnknown(m)
}
var xxx_messageInfo_Note proto.InternalMessageInfo
func (m *Note) GetId() string {
if m != nil {
return m.Id
}
return ""
}
func (m *Note) GetCreated() int64 {
if m != nil {
return m.Created
}
return 0
}
func (m *Note) GetTitle() string {
if m != nil {
return m.Title
}
return ""
}
func (m *Note) GetText() string {
if m != nil {
return m.Text
}
return ""
}
type CreateRequest struct {
Title string `protobuf:"bytes,1,opt,name=title,proto3" json:"title,omitempty"`
Text string `protobuf:"bytes,2,opt,name=text,proto3" json:"text,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *CreateRequest) Reset() { *m = CreateRequest{} }
func (m *CreateRequest) String() string { return proto.CompactTextString(m) }
func (*CreateRequest) ProtoMessage() {}
func (*CreateRequest) Descriptor() ([]byte, []int) {
return fileDescriptor_7f19915b807268a5, []int{1}
}
func (m *CreateRequest) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_CreateRequest.Unmarshal(m, b)
}
func (m *CreateRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_CreateRequest.Marshal(b, m, deterministic)
}
func (m *CreateRequest) XXX_Merge(src proto.Message) {
xxx_messageInfo_CreateRequest.Merge(m, src)
}
func (m *CreateRequest) XXX_Size() int {
return xxx_messageInfo_CreateRequest.Size(m)
}
func (m *CreateRequest) XXX_DiscardUnknown() {
xxx_messageInfo_CreateRequest.DiscardUnknown(m)
}
var xxx_messageInfo_CreateRequest proto.InternalMessageInfo
func (m *CreateRequest) GetTitle() string {
if m != nil {
return m.Title
}
return ""
}
func (m *CreateRequest) GetText() string {
if m != nil {
return m.Text
}
return ""
}
type CreateResponse struct {
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *CreateResponse) Reset() { *m = CreateResponse{} }
func (m *CreateResponse) String() string { return proto.CompactTextString(m) }
func (*CreateResponse) ProtoMessage() {}
func (*CreateResponse) Descriptor() ([]byte, []int) {
return fileDescriptor_7f19915b807268a5, []int{2}
}
func (m *CreateResponse) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_CreateResponse.Unmarshal(m, b)
}
func (m *CreateResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_CreateResponse.Marshal(b, m, deterministic)
}
func (m *CreateResponse) XXX_Merge(src proto.Message) {
xxx_messageInfo_CreateResponse.Merge(m, src)
}
func (m *CreateResponse) XXX_Size() int {
return xxx_messageInfo_CreateResponse.Size(m)
}
func (m *CreateResponse) XXX_DiscardUnknown() {
xxx_messageInfo_CreateResponse.DiscardUnknown(m)
}
var xxx_messageInfo_CreateResponse proto.InternalMessageInfo
func (m *CreateResponse) GetId() string {
if m != nil {
return m.Id
}
return ""
}
type UpdateRequest struct {
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
Title string `protobuf:"bytes,2,opt,name=title,proto3" json:"title,omitempty"`
Text string `protobuf:"bytes,3,opt,name=text,proto3" json:"text,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *UpdateRequest) Reset() { *m = UpdateRequest{} }
func (m *UpdateRequest) String() string { return proto.CompactTextString(m) }
func (*UpdateRequest) ProtoMessage() {}
func (*UpdateRequest) Descriptor() ([]byte, []int) {
return fileDescriptor_7f19915b807268a5, []int{3}
}
func (m *UpdateRequest) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_UpdateRequest.Unmarshal(m, b)
}
func (m *UpdateRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_UpdateRequest.Marshal(b, m, deterministic)
}
func (m *UpdateRequest) XXX_Merge(src proto.Message) {
xxx_messageInfo_UpdateRequest.Merge(m, src)
}
func (m *UpdateRequest) XXX_Size() int {
return xxx_messageInfo_UpdateRequest.Size(m)
}
func (m *UpdateRequest) XXX_DiscardUnknown() {
xxx_messageInfo_UpdateRequest.DiscardUnknown(m)
}
var xxx_messageInfo_UpdateRequest proto.InternalMessageInfo
func (m *UpdateRequest) GetId() string {
if m != nil {
return m.Id
}
return ""
}
func (m *UpdateRequest) GetTitle() string {
if m != nil {
return m.Title
}
return ""
}
func (m *UpdateRequest) GetText() string {
if m != nil {
return m.Text
}
return ""
}
type UpdateResponse struct {
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *UpdateResponse) Reset() { *m = UpdateResponse{} }
func (m *UpdateResponse) String() string { return proto.CompactTextString(m) }
func (*UpdateResponse) ProtoMessage() {}
func (*UpdateResponse) Descriptor() ([]byte, []int) {
return fileDescriptor_7f19915b807268a5, []int{4}
}
func (m *UpdateResponse) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_UpdateResponse.Unmarshal(m, b)
}
func (m *UpdateResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_UpdateResponse.Marshal(b, m, deterministic)
}
func (m *UpdateResponse) XXX_Merge(src proto.Message) {
xxx_messageInfo_UpdateResponse.Merge(m, src)
}
func (m *UpdateResponse) XXX_Size() int {
return xxx_messageInfo_UpdateResponse.Size(m)
}
func (m *UpdateResponse) XXX_DiscardUnknown() {
xxx_messageInfo_UpdateResponse.DiscardUnknown(m)
}
var xxx_messageInfo_UpdateResponse proto.InternalMessageInfo
type DeleteRequest struct {
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *DeleteRequest) Reset() { *m = DeleteRequest{} }
func (m *DeleteRequest) String() string { return proto.CompactTextString(m) }
func (*DeleteRequest) ProtoMessage() {}
func (*DeleteRequest) Descriptor() ([]byte, []int) {
return fileDescriptor_7f19915b807268a5, []int{5}
}
func (m *DeleteRequest) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_DeleteRequest.Unmarshal(m, b)
}
func (m *DeleteRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_DeleteRequest.Marshal(b, m, deterministic)
}
func (m *DeleteRequest) XXX_Merge(src proto.Message) {
xxx_messageInfo_DeleteRequest.Merge(m, src)
}
func (m *DeleteRequest) XXX_Size() int {
return xxx_messageInfo_DeleteRequest.Size(m)
}
func (m *DeleteRequest) XXX_DiscardUnknown() {
xxx_messageInfo_DeleteRequest.DiscardUnknown(m)
}
var xxx_messageInfo_DeleteRequest proto.InternalMessageInfo
func (m *DeleteRequest) GetId() string {
if m != nil {
return m.Id
}
return ""
}
type DeleteResponse struct {
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *DeleteResponse) Reset() { *m = DeleteResponse{} }
func (m *DeleteResponse) String() string { return proto.CompactTextString(m) }
func (*DeleteResponse) ProtoMessage() {}
func (*DeleteResponse) Descriptor() ([]byte, []int) {
return fileDescriptor_7f19915b807268a5, []int{6}
}
func (m *DeleteResponse) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_DeleteResponse.Unmarshal(m, b)
}
func (m *DeleteResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_DeleteResponse.Marshal(b, m, deterministic)
}
func (m *DeleteResponse) XXX_Merge(src proto.Message) {
xxx_messageInfo_DeleteResponse.Merge(m, src)
}
func (m *DeleteResponse) XXX_Size() int {
return xxx_messageInfo_DeleteResponse.Size(m)
}
func (m *DeleteResponse) XXX_DiscardUnknown() {
xxx_messageInfo_DeleteResponse.DiscardUnknown(m)
}
var xxx_messageInfo_DeleteResponse proto.InternalMessageInfo
type ListRequest struct {
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *ListRequest) Reset() { *m = ListRequest{} }
func (m *ListRequest) String() string { return proto.CompactTextString(m) }
func (*ListRequest) ProtoMessage() {}
func (*ListRequest) Descriptor() ([]byte, []int) {
return fileDescriptor_7f19915b807268a5, []int{7}
}
func (m *ListRequest) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_ListRequest.Unmarshal(m, b)
}
func (m *ListRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_ListRequest.Marshal(b, m, deterministic)
}
func (m *ListRequest) XXX_Merge(src proto.Message) {
xxx_messageInfo_ListRequest.Merge(m, src)
}
func (m *ListRequest) XXX_Size() int {
return xxx_messageInfo_ListRequest.Size(m)
}
func (m *ListRequest) XXX_DiscardUnknown() {
xxx_messageInfo_ListRequest.DiscardUnknown(m)
}
var xxx_messageInfo_ListRequest proto.InternalMessageInfo
type ListResponse struct {
Notes []*Note `protobuf:"bytes,1,rep,name=notes,proto3" json:"notes,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *ListResponse) Reset() { *m = ListResponse{} }
func (m *ListResponse) String() string { return proto.CompactTextString(m) }
func (*ListResponse) ProtoMessage() {}
func (*ListResponse) Descriptor() ([]byte, []int) {
return fileDescriptor_7f19915b807268a5, []int{8}
}
func (m *ListResponse) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_ListResponse.Unmarshal(m, b)
}
func (m *ListResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_ListResponse.Marshal(b, m, deterministic)
}
func (m *ListResponse) XXX_Merge(src proto.Message) {
xxx_messageInfo_ListResponse.Merge(m, src)
}
func (m *ListResponse) XXX_Size() int {
return xxx_messageInfo_ListResponse.Size(m)
}
func (m *ListResponse) XXX_DiscardUnknown() {
xxx_messageInfo_ListResponse.DiscardUnknown(m)
}
var xxx_messageInfo_ListResponse proto.InternalMessageInfo
func (m *ListResponse) GetNotes() []*Note {
if m != nil {
return m.Notes
}
return nil
}
func init() {
proto.RegisterType((*Note)(nil), "notes.Note")
proto.RegisterType((*CreateRequest)(nil), "notes.CreateRequest")
proto.RegisterType((*CreateResponse)(nil), "notes.CreateResponse")
proto.RegisterType((*UpdateRequest)(nil), "notes.UpdateRequest")
proto.RegisterType((*UpdateResponse)(nil), "notes.UpdateResponse")
proto.RegisterType((*DeleteRequest)(nil), "notes.DeleteRequest")
proto.RegisterType((*DeleteResponse)(nil), "notes.DeleteResponse")
proto.RegisterType((*ListRequest)(nil), "notes.ListRequest")
proto.RegisterType((*ListResponse)(nil), "notes.ListResponse")
}
func init() { proto.RegisterFile("notes/proto/notes.proto", fileDescriptor_7f19915b807268a5) }
var fileDescriptor_7f19915b807268a5 = []byte{
// 344 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x92, 0xcf, 0x4a, 0xc3, 0x40,
0x10, 0xc6, 0xc9, 0x9f, 0x56, 0x9c, 0x36, 0x45, 0xd6, 0x8a, 0xa1, 0x17, 0x63, 0x4e, 0x01, 0xb1,
0xc1, 0x8a, 0x07, 0x11, 0x2f, 0xea, 0x45, 0x10, 0x0f, 0x11, 0x2f, 0xbd, 0xb5, 0xc9, 0xa0, 0x0b,
0x6d, 0x37, 0x66, 0xa7, 0xe2, 0xc3, 0xf8, 0xb0, 0x92, 0xdd, 0x24, 0xcd, 0xd6, 0x7a, 0xf0, 0x36,
0xdf, 0xec, 0xfc, 0x66, 0x76, 0xbe, 0x5d, 0x38, 0x5e, 0x09, 0x42, 0x19, 0xe7, 0x85, 0x20, 0x11,
0xab, 0x78, 0xac, 0x62, 0xd6, 0x51, 0x22, 0x9c, 0x82, 0xfb, 0x2c, 0x08, 0xd9, 0x00, 0x6c, 0x9e,
0xf9, 0x56, 0x60, 0x45, 0xfb, 0x89, 0xcd, 0x33, 0xe6, 0xc3, 0x5e, 0x5a, 0xe0, 0x8c, 0x30, 0xf3,
0xed, 0xc0, 0x8a, 0x9c, 0xa4, 0x96, 0x6c, 0x08, 0x1d, 0xe2, 0xb4, 0x40, 0xdf, 0x51, 0xc5, 0x5a,
0x30, 0x06, 0x2e, 0xe1, 0x17, 0xf9, 0xae, 0x4a, 0xaa, 0x38, 0xbc, 0x06, 0xef, 0x5e, 0x41, 0x09,
0x7e, 0xac, 0x51, 0xd2, 0x06, 0xb5, 0x76, 0xa1, 0x76, 0x0b, 0x0d, 0x60, 0x50, 0xa3, 0x32, 0x17,
0x2b, 0xf9, 0xeb, 0x82, 0xe1, 0x23, 0x78, 0xaf, 0x79, 0xd6, 0x6a, 0xbe, 0xbd, 0x41, 0x33, 0xcc,
0xde, 0x35, 0xcc, 0x69, 0x0d, 0x3b, 0x80, 0x41, 0xdd, 0x4a, 0x0f, 0x0b, 0x4f, 0xc0, 0x7b, 0xc0,
0x05, 0xfe, 0xd9, 0xbc, 0x44, 0xea, 0x82, 0x0a, 0xf1, 0xa0, 0xf7, 0xc4, 0x25, 0x55, 0x40, 0x78,
0x01, 0x7d, 0x2d, 0xab, 0xeb, 0x9f, 0x82, 0x36, 0xdc, 0xb7, 0x02, 0x27, 0xea, 0x4d, 0x7a, 0x63,
0xfd, 0x16, 0xa5, 0xf7, 0x89, 0x3e, 0x99, 0x7c, 0xdb, 0xd0, 0x29, 0xb5, 0x64, 0x31, 0xb8, 0x25,
0xcc, 0x58, 0x55, 0xd5, 0x6a, 0x3c, 0x3a, 0x34, 0x72, 0x55, 0xf7, 0x2b, 0xe8, 0x6a, 0xbb, 0xd8,
0xb0, 0x3a, 0x36, 0x8c, 0x1f, 0x1d, 0x6d, 0x65, 0x37, 0x98, 0xde, 0xa2, 0xc1, 0x8c, 0xad, 0x1b,
0xcc, 0x5c, 0xb5, 0xc4, 0xb4, 0x5f, 0x0d, 0x66, 0xbc, 0x44, 0x83, 0x99, 0xa6, 0xb2, 0x5b, 0xe8,
0xeb, 0xcc, 0x0b, 0x15, 0x38, 0x5b, 0xfe, 0x0b, 0x8e, 0xac, 0xbb, 0xf3, 0xe9, 0xd9, 0x1b, 0xa7,
0xf7, 0xf5, 0x7c, 0x9c, 0x8a, 0x65, 0xbc, 0xe4, 0x69, 0x21, 0x62, 0x89, 0xc5, 0x27, 0x4f, 0x51,
0xc6, 0xad, 0x5f, 0x7e, 0xa3, 0xe2, 0x79, 0x57, 0x89, 0xcb, 0x9f, 0x00, 0x00, 0x00, 0xff, 0xff,
0x31, 0xee, 0xed, 0xda, 0x01, 0x03, 0x00, 0x00,
}

View File

@@ -0,0 +1,236 @@
// Code generated by protoc-gen-micro. DO NOT EDIT.
// source: notes/proto/notes.proto
package notes
import (
fmt "fmt"
proto "github.com/golang/protobuf/proto"
math "math"
)
import (
context "context"
api "github.com/micro/micro/v3/service/api"
client "github.com/micro/micro/v3/service/client"
server "github.com/micro/micro/v3/service/server"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
// Reference imports to suppress errors if they are not otherwise used.
var _ api.Endpoint
var _ context.Context
var _ client.Option
var _ server.Option
// Api Endpoints for Notes service
func NewNotesEndpoints() []*api.Endpoint {
return []*api.Endpoint{}
}
// Client API for Notes service
type NotesService interface {
List(ctx context.Context, in *ListRequest, opts ...client.CallOption) (*ListResponse, error)
Create(ctx context.Context, in *CreateRequest, opts ...client.CallOption) (*CreateResponse, error)
Delete(ctx context.Context, in *DeleteRequest, opts ...client.CallOption) (*DeleteResponse, error)
Update(ctx context.Context, in *UpdateRequest, opts ...client.CallOption) (*UpdateResponse, error)
UpdateStream(ctx context.Context, opts ...client.CallOption) (Notes_UpdateStreamService, error)
}
type notesService struct {
c client.Client
name string
}
func NewNotesService(name string, c client.Client) NotesService {
return &notesService{
c: c,
name: name,
}
}
func (c *notesService) List(ctx context.Context, in *ListRequest, opts ...client.CallOption) (*ListResponse, error) {
req := c.c.NewRequest(c.name, "Notes.List", in)
out := new(ListResponse)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *notesService) Create(ctx context.Context, in *CreateRequest, opts ...client.CallOption) (*CreateResponse, error) {
req := c.c.NewRequest(c.name, "Notes.Create", in)
out := new(CreateResponse)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *notesService) Delete(ctx context.Context, in *DeleteRequest, opts ...client.CallOption) (*DeleteResponse, error) {
req := c.c.NewRequest(c.name, "Notes.Delete", in)
out := new(DeleteResponse)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *notesService) Update(ctx context.Context, in *UpdateRequest, opts ...client.CallOption) (*UpdateResponse, error) {
req := c.c.NewRequest(c.name, "Notes.Update", in)
out := new(UpdateResponse)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *notesService) UpdateStream(ctx context.Context, opts ...client.CallOption) (Notes_UpdateStreamService, error) {
req := c.c.NewRequest(c.name, "Notes.UpdateStream", &UpdateRequest{})
stream, err := c.c.Stream(ctx, req, opts...)
if err != nil {
return nil, err
}
return &notesServiceUpdateStream{stream}, nil
}
type Notes_UpdateStreamService interface {
Context() context.Context
SendMsg(interface{}) error
RecvMsg(interface{}) error
CloseAndRecv() (*UpdateResponse, error)
Send(*UpdateRequest) error
}
type notesServiceUpdateStream struct {
stream client.Stream
}
func (x *notesServiceUpdateStream) CloseAndRecv() (*UpdateResponse, error) {
if err := x.stream.Close(); err != nil {
return nil, err
}
r := new(UpdateResponse)
err := x.RecvMsg(r)
return r, err
}
func (x *notesServiceUpdateStream) Context() context.Context {
return x.stream.Context()
}
func (x *notesServiceUpdateStream) SendMsg(m interface{}) error {
return x.stream.Send(m)
}
func (x *notesServiceUpdateStream) RecvMsg(m interface{}) error {
return x.stream.Recv(m)
}
func (x *notesServiceUpdateStream) Send(m *UpdateRequest) error {
return x.stream.Send(m)
}
// Server API for Notes service
type NotesHandler interface {
List(context.Context, *ListRequest, *ListResponse) error
Create(context.Context, *CreateRequest, *CreateResponse) error
Delete(context.Context, *DeleteRequest, *DeleteResponse) error
Update(context.Context, *UpdateRequest, *UpdateResponse) error
UpdateStream(context.Context, Notes_UpdateStreamStream) error
}
func RegisterNotesHandler(s server.Server, hdlr NotesHandler, opts ...server.HandlerOption) error {
type notes interface {
List(ctx context.Context, in *ListRequest, out *ListResponse) error
Create(ctx context.Context, in *CreateRequest, out *CreateResponse) error
Delete(ctx context.Context, in *DeleteRequest, out *DeleteResponse) error
Update(ctx context.Context, in *UpdateRequest, out *UpdateResponse) error
UpdateStream(ctx context.Context, stream server.Stream) error
}
type Notes struct {
notes
}
h := &notesHandler{hdlr}
return s.Handle(s.NewHandler(&Notes{h}, opts...))
}
type notesHandler struct {
NotesHandler
}
func (h *notesHandler) List(ctx context.Context, in *ListRequest, out *ListResponse) error {
return h.NotesHandler.List(ctx, in, out)
}
func (h *notesHandler) Create(ctx context.Context, in *CreateRequest, out *CreateResponse) error {
return h.NotesHandler.Create(ctx, in, out)
}
func (h *notesHandler) Delete(ctx context.Context, in *DeleteRequest, out *DeleteResponse) error {
return h.NotesHandler.Delete(ctx, in, out)
}
func (h *notesHandler) Update(ctx context.Context, in *UpdateRequest, out *UpdateResponse) error {
return h.NotesHandler.Update(ctx, in, out)
}
func (h *notesHandler) UpdateStream(ctx context.Context, stream server.Stream) error {
return h.NotesHandler.UpdateStream(ctx, &notesUpdateStreamStream{stream})
}
type Notes_UpdateStreamStream interface {
Context() context.Context
SendMsg(interface{}) error
RecvMsg(interface{}) error
SendAndClose(*UpdateResponse) error
Recv() (*UpdateRequest, error)
}
type notesUpdateStreamStream struct {
stream server.Stream
}
func (x *notesUpdateStreamStream) SendAndClose(in *UpdateResponse) error {
if err := x.SendMsg(in); err != nil {
return err
}
return x.stream.Close()
}
func (x *notesUpdateStreamStream) Context() context.Context {
return x.stream.Context()
}
func (x *notesUpdateStreamStream) SendMsg(m interface{}) error {
return x.stream.Send(m)
}
func (x *notesUpdateStreamStream) RecvMsg(m interface{}) error {
return x.stream.Recv(m)
}
func (x *notesUpdateStreamStream) Recv() (*UpdateRequest, error) {
m := new(UpdateRequest)
if err := x.stream.Recv(m); err != nil {
return nil, err
}
return m, nil
}

48
notes/proto/notes.proto Normal file
View File

@@ -0,0 +1,48 @@
syntax = "proto3";
package notes;
option go_package = "github.com/micro/services/notes/proto;notes";
service Notes {
rpc List(ListRequest) returns (ListResponse);
rpc Create(CreateRequest) returns (CreateResponse);
rpc Delete(DeleteRequest) returns (DeleteResponse);
rpc Update(UpdateRequest) returns (UpdateResponse);
rpc UpdateStream(stream UpdateRequest) returns (UpdateResponse);
}
message Note {
string id = 1;
int64 created = 2;
string title = 3;
string text = 4;
}
message CreateRequest {
string title = 1;
string text = 2;
}
message CreateResponse {
string id = 1;
}
message UpdateRequest {
string id = 1;
string title = 2;
string text = 3;
}
message UpdateResponse {}
message DeleteRequest {
string id = 1;
}
message DeleteResponse {}
message ListRequest {}
message ListResponse {
repeated Note notes = 1;
}