add notes (#218)

* add notes

* fix build

* Commit from GitHub Actions (Publish APIs & Clients)

Co-authored-by: asim <asim@users.noreply.github.com>
This commit is contained in:
Asim Aslam
2021-09-29 14:02:28 +01:00
committed by GitHub
parent 29aea04423
commit 3c38d96881
29 changed files with 2223 additions and 1 deletions

29
notes/Makefile Normal file
View File

@@ -0,0 +1,29 @@
GOPATH:=$(shell go env GOPATH)
.PHONY: api
api:
protoc --openapi_out=. --proto_path=. proto/notes.proto
.PHONY: init
init:
go get -u github.com/golang/protobuf/proto
go get -u github.com/golang/protobuf/protoc-gen-go
go get github.com/micro/micro/v3/cmd/protoc-gen-micro
go get github.com/micro/micro/v3/cmd/protoc-gen-openapi
.PHONY: proto
proto:
protoc --proto_path=. --micro_out=. --go_out=:. proto/notes.proto
.PHONY: build
build:
go build -o notes *.go
.PHONY: test
test:
go test -v ./... -cover
.PHONY: docker
docker:
docker build . -t notes:latest

7
notes/README.md Normal file
View File

@@ -0,0 +1,7 @@
Store and retrieve notes
# Notes Service
The notes service lets you keep track of notes as first class objects. Subscribe to changes and
perform fast retrieval of all your notes.

93
notes/examples.json Normal file
View File

@@ -0,0 +1,93 @@
{
"create": [{
"title": "Create a note",
"description": "Create a simple text based note",
"run_check": true,
"request": {
"title": "New Note",
"text": "This is my note"
},
"response": {
"note": {
"id": "63c0cdf8-2121-11ec-a881-0242e36f037a",
"created": "2021-09-29T13:33:03+01:00",
"updated": "2021-09-29T13:33:03+01:00",
"title": "New Note",
"text": "This is my note"
}
}
}],
"read": [{
"title": "Read a note",
"description": "Read a note by its ID",
"run_check": true,
"request": {
"id": "63c0cdf8-2121-11ec-a881-0242e36f037a"
},
"response": {
"note": {
"id": "63c0cdf8-2121-11ec-a881-0242e36f037a",
"created": "2021-09-29T13:33:03+01:00",
"updated": "2021-09-29T13:33:03+01:00",
"title": "New Note",
"text": "This is my note"
}
}
}],
"list": [{
"title": "List all notes",
"description": "List all your available notes",
"run_check": true,
"request": {},
"response": {
"notes": [
{
"id": "63c0cdf8-2121-11ec-a881-0242e36f037a",
"created": "2021-09-29T13:33:03+01:00",
"updated": "2021-09-29T13:33:03+01:00",
"title": "New Note",
"text": "This is my note"
}
]
}
}],
"update": [{
"title": "Update a Note",
"description": "Update the note title and text",
"run_check": true,
"request": {
"note": {
"id": "63c0cdf8-2121-11ec-a881-0242e36f037a",
"title": "Update Note",
"text": "Updated note text"
}
},
"response": {
"note": {
"id": "63c0cdf8-2121-11ec-a881-0242e36f037a",
"created": "2021-09-29T13:33:03+01:00",
"updated": "2021-09-29T13:50:20+01:00",
"title": "Update Note",
"text": "Updated note text"
}
}
}],
"delete": [{
"title": "Delete a Note",
"description": "Delete a note by id",
"run_check": true,
"request": {
"id": "63c0cdf8-2121-11ec-a881-0242e36f037a"
},
"response": {
"note": {
"id": "63c0cdf8-2121-11ec-a881-0242e36f037a",
"created": "2021-09-29T13:33:03+01:00",
"updated": "2021-09-29T13:50:20+01:00",
"title": "Update Note",
"text": "Updated note text"
}
}
}]
}

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

@@ -0,0 +1,284 @@
package handler
import (
"context"
"fmt"
"time"
"github.com/google/uuid"
"github.com/micro/micro/v3/service/client"
"github.com/micro/micro/v3/service/errors"
"github.com/micro/micro/v3/service/store"
pb "github.com/micro/services/notes/proto"
"github.com/micro/services/pkg/tenant"
streamPb "github.com/micro/services/stream/proto"
"google.golang.org/protobuf/types/known/structpb"
)
// New returns an initialized Notes
func New(c client.Client) *Notes {
return &Notes{
Stream: streamPb.NewStreamService("stream", c),
}
}
// Notes implements the notes proto definition
type Notes struct {
Stream streamPb.StreamService
}
func newMessage(ev map[string]interface{}) *structpb.Struct {
v, _ := structpb.NewStruct(ev)
return v
}
// Create inserts a new note in the store
func (h *Notes) Create(ctx context.Context, req *pb.CreateRequest, rsp *pb.CreateResponse) error {
if len(req.Title) == 0 && len(req.Text) == 0 {
return errors.BadRequest("notes.create", "missing title and text")
}
tnt, ok := tenant.FromContext(ctx)
if !ok {
tnt = "default"
}
// generate a key (uuid v4)
id, err := uuid.NewUUID()
if err != nil {
return err
}
t := time.Now().Format(time.RFC3339)
// set the generated fields on the note
note := &pb.Note{
Id: id.String(),
Created: t,
Updated: t,
Title: req.Title,
Text: req.Text,
}
key := fmt.Sprintf("%s:%s", tnt, id)
rec := store.NewRecord(key, note)
if err = store.Write(rec); err != nil {
return errors.InternalServerError("notes.created", "failed to create note")
}
// return the note in the response
rsp.Note = note
go h.Stream.Publish(ctx, &streamPb.PublishRequest{
Topic: "notes",
Message: newMessage(map[string]interface{}{
"type": "create",
"note": note,
}),
})
return nil
}
func (h *Notes) Read(ctx context.Context, req *pb.ReadRequest, rsp *pb.ReadResponse) error {
if len(req.Id) == 0 {
return errors.BadRequest("notes.read", "Missing Note ID")
}
tnt, ok := tenant.FromContext(ctx)
if !ok {
tnt = "default"
}
key := fmt.Sprintf("%s:%s", tnt, req.Id)
// read the specific note
recs, err := store.Read(key)
if err == store.ErrNotFound {
return errors.NotFound("notes.read", "Note not found")
} else if err != nil {
return errors.InternalServerError("notes.read", "Error reading from store: %v", err.Error())
}
// Decode the note
var note *pb.Note
if err := recs[0].Decode(&note); err != nil {
return errors.InternalServerError("notes.update", "Error unmarshaling JSON: %v", err.Error())
}
// return the note
rsp.Note = note
return nil
}
// Update is a unary API which updates a note in the store
func (h *Notes) Update(ctx context.Context, req *pb.UpdateRequest, rsp *pb.UpdateResponse) error {
// Validate the request
if req.Note == nil {
return errors.BadRequest("notes.update", "Missing Note")
}
if len(req.Note.Id) == 0 {
return errors.BadRequest("notes.update", "Missing Note ID")
}
tnt, ok := tenant.FromContext(ctx)
if !ok {
tnt = "default"
}
key := fmt.Sprintf("%s:%s", tnt, req.Note.Id)
// read the specific note
recs, err := store.Read(key)
if err == store.ErrNotFound {
return errors.NotFound("notes.update", "Note not found")
} else if err != nil {
return errors.InternalServerError("notes.update", "Error reading from store: %v", err.Error())
}
// Decode the note
var note *pb.Note
if err := recs[0].Decode(&note); err != nil {
return errors.InternalServerError("notes.update", "Error unmarshaling JSON: %v", err.Error())
}
// Update the notes title and text
note.Title = req.Note.Title
note.Text = req.Note.Text
note.Updated = time.Now().Format(time.RFC3339)
rec := store.NewRecord(key, note)
// Write the updated note to the store
if err = store.Write(rec); err != nil {
return errors.InternalServerError("notes.update", "Error writing to store: %v", err.Error())
}
go h.Stream.Publish(ctx, &streamPb.PublishRequest{
Topic: "notes",
Message: newMessage(map[string]interface{}{
"type": "update",
"note": note,
}),
})
rsp.Note = note
return nil
}
func (h *Notes) Subscribe(ctx context.Context, req *pb.SubscribeRequest, stream pb.Notes_SubscribeStream) error {
backendStream, err := h.Stream.Subscribe(ctx, &streamPb.SubscribeRequest{
Topic: "notes",
})
if err != nil {
return errors.InternalServerError("notes.subscribe", "Failed to subscribe to notes")
}
for {
select {
case <-ctx.Done():
return nil
default:
}
// receive messages from the stream
msg, err := backendStream.Recv()
if err != nil {
return nil
}
ev := msg.Message.AsMap()
note := ev["note"].(*pb.Note)
// filter if necessary by id
if len(req.Id) > 0 && note.Id != req.Id {
continue
}
// send back the event to the client
if err := stream.Send(&pb.SubscribeResponse{
Event: ev["type"].(string),
Note: note,
}); err != nil {
return nil
}
}
return nil
}
// Delete removes the note from the store, looking up using ID
func (h *Notes) Delete(ctx context.Context, req *pb.DeleteRequest, rsp *pb.DeleteResponse) error {
// Validate the request
if len(req.Id) == 0 {
return errors.BadRequest("notes.delete", "Missing Note ID")
}
tnt, ok := tenant.FromContext(ctx)
if !ok {
tnt = "default"
}
key := fmt.Sprintf("%s:%s", tnt, req.Id)
// read the specific note
recs, err := store.Read(key)
if err == store.ErrNotFound {
return nil
} else if err != nil {
return errors.InternalServerError("notes.delete", "Error reading from store: %v", err.Error())
}
// Decode the note
var note *pb.Note
if err := recs[0].Decode(&note); err != nil {
return errors.InternalServerError("notes.delete", "Error unmarshaling JSON: %v", err.Error())
}
// now delete it
if err := store.Delete(key); err != nil && err != store.ErrNotFound {
return errors.InternalServerError("notes.delete", "Failed to delete note")
}
go h.Stream.Publish(ctx, &streamPb.PublishRequest{
Topic: "notes",
Message: newMessage(map[string]interface{}{
"type": "delete",
"note": note,
}),
})
rsp.Note = note
return nil
}
// List returns all of the notes in the store
func (h *Notes) List(ctx context.Context, req *pb.ListRequest, rsp *pb.ListResponse) error {
tnt, ok := tenant.FromContext(ctx)
if !ok {
tnt = "default"
}
key := fmt.Sprintf("%s:", tnt)
// Retrieve all of the records in the store
recs, err := store.Read(key, store.ReadPrefix())
if err != nil {
return errors.InternalServerError("notes.list", "Error reading from store: %v", err.Error())
}
// Initialize the response notes slice
rsp.Notes = make([]*pb.Note, len(recs))
// Unmarshal the notes into the response
for i, r := range recs {
if err := r.Decode(&rsp.Notes[i]); err != nil {
return errors.InternalServerError("notes.list", "Error decoding note: %v", err.Error())
}
}
return nil
}

27
notes/main.go Normal file
View File

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

1002
notes/proto/notes.pb.go Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,253 @@
// Code generated by protoc-gen-micro. DO NOT EDIT.
// source: 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)
Read(ctx context.Context, in *ReadRequest, opts ...client.CallOption) (*ReadResponse, error)
Delete(ctx context.Context, in *DeleteRequest, opts ...client.CallOption) (*DeleteResponse, error)
Update(ctx context.Context, in *UpdateRequest, opts ...client.CallOption) (*UpdateResponse, error)
Subscribe(ctx context.Context, in *SubscribeRequest, opts ...client.CallOption) (Notes_SubscribeService, 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) Read(ctx context.Context, in *ReadRequest, opts ...client.CallOption) (*ReadResponse, error) {
req := c.c.NewRequest(c.name, "Notes.Read", in)
out := new(ReadResponse)
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) Subscribe(ctx context.Context, in *SubscribeRequest, opts ...client.CallOption) (Notes_SubscribeService, error) {
req := c.c.NewRequest(c.name, "Notes.Subscribe", &SubscribeRequest{})
stream, err := c.c.Stream(ctx, req, opts...)
if err != nil {
return nil, err
}
if err := stream.Send(in); err != nil {
return nil, err
}
return &notesServiceSubscribe{stream}, nil
}
type Notes_SubscribeService interface {
Context() context.Context
SendMsg(interface{}) error
RecvMsg(interface{}) error
Close() error
Recv() (*SubscribeResponse, error)
}
type notesServiceSubscribe struct {
stream client.Stream
}
func (x *notesServiceSubscribe) Close() error {
return x.stream.Close()
}
func (x *notesServiceSubscribe) Context() context.Context {
return x.stream.Context()
}
func (x *notesServiceSubscribe) SendMsg(m interface{}) error {
return x.stream.Send(m)
}
func (x *notesServiceSubscribe) RecvMsg(m interface{}) error {
return x.stream.Recv(m)
}
func (x *notesServiceSubscribe) Recv() (*SubscribeResponse, error) {
m := new(SubscribeResponse)
err := x.stream.Recv(m)
if err != nil {
return nil, err
}
return m, nil
}
// Server API for Notes service
type NotesHandler interface {
List(context.Context, *ListRequest, *ListResponse) error
Create(context.Context, *CreateRequest, *CreateResponse) error
Read(context.Context, *ReadRequest, *ReadResponse) error
Delete(context.Context, *DeleteRequest, *DeleteResponse) error
Update(context.Context, *UpdateRequest, *UpdateResponse) error
Subscribe(context.Context, *SubscribeRequest, Notes_SubscribeStream) 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
Read(ctx context.Context, in *ReadRequest, out *ReadResponse) error
Delete(ctx context.Context, in *DeleteRequest, out *DeleteResponse) error
Update(ctx context.Context, in *UpdateRequest, out *UpdateResponse) error
Subscribe(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) Read(ctx context.Context, in *ReadRequest, out *ReadResponse) error {
return h.NotesHandler.Read(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) Subscribe(ctx context.Context, stream server.Stream) error {
m := new(SubscribeRequest)
if err := stream.Recv(m); err != nil {
return err
}
return h.NotesHandler.Subscribe(ctx, m, &notesSubscribeStream{stream})
}
type Notes_SubscribeStream interface {
Context() context.Context
SendMsg(interface{}) error
RecvMsg(interface{}) error
Close() error
Send(*SubscribeResponse) error
}
type notesSubscribeStream struct {
stream server.Stream
}
func (x *notesSubscribeStream) Close() error {
return x.stream.Close()
}
func (x *notesSubscribeStream) Context() context.Context {
return x.stream.Context()
}
func (x *notesSubscribeStream) SendMsg(m interface{}) error {
return x.stream.Send(m)
}
func (x *notesSubscribeStream) RecvMsg(m interface{}) error {
return x.stream.Recv(m)
}
func (x *notesSubscribeStream) Send(m *SubscribeResponse) error {
return x.stream.Send(m)
}

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

@@ -0,0 +1,91 @@
syntax = "proto3";
package notes;
option go_package = "./proto;notes";
service Notes {
rpc List(ListRequest) returns (ListResponse);
rpc Create(CreateRequest) returns (CreateResponse);
rpc Read(ReadRequest) returns (ReadResponse);
rpc Delete(DeleteRequest) returns (DeleteResponse);
rpc Update(UpdateRequest) returns (UpdateResponse);
rpc Subscribe(SubscribeRequest) returns (stream SubscribeResponse);
}
message Note {
// unique id for the note, generated if not specified
string id = 1;
// time at which the note was created
string created = 2;
// time at which the note was updated
string updated = 3;
// title of the note
string title = 4;
// text within the note
string text = 5;
}
// Create a new note
message CreateRequest {
// note title
string title = 1;
// note text
string text = 2;
}
message CreateResponse {
// The created note
Note note = 1;
}
// Read a note
message ReadRequest {
// the note id
string id = 1;
}
message ReadResponse {
// The note
Note note = 1;
}
// Update a note
message UpdateRequest {
Note note = 1;
}
message UpdateResponse {
Note note = 1;
}
// Delete a note
message DeleteRequest {
// specify the id of the note
string id = 1;
}
message DeleteResponse {
Note note = 1;
}
// List all the notes
message ListRequest {}
message ListResponse {
// the list of notes
repeated Note notes = 1;
}
// Specify the note to events
message SubscribeRequest {
// optionally specify a note id
string id = 1;
}
message SubscribeResponse {
// the event which occured; created, deleted, updated
string event = 1;
// the note which the operation occured on
Note note = 2;
}

5
notes/publicapi.json Normal file
View File

@@ -0,0 +1,5 @@
{
"name": "notes",
"icon": "📝",
"category": "storage"
}