This commit is contained in:
Asim Aslam
2020-10-02 11:13:01 +01:00
commit 61fe9c169b
62 changed files with 4428 additions and 0 deletions

2
blog/posts/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
./posts

3
blog/posts/Dockerfile Normal file
View File

@@ -0,0 +1,3 @@
FROM alpine
ADD post-service /post-service
ENTRYPOINT [ "/post-service" ]

22
blog/posts/Makefile Normal file
View File

@@ -0,0 +1,22 @@
GOPATH:=$(shell go env GOPATH)
MODIFY=Mgithub.com/micro/go-micro/api/proto/api.proto=github.com/micro/go-micro/v3/api/proto
.PHONY: proto
proto:
protoc --proto_path=. --micro_out=${MODIFY}:. --go_out=${MODIFY}:. proto/post/post.proto
.PHONY: build
build: proto
go build -o post-service *.go
.PHONY: test
test:
go test -v ./... -cover
.PHONY: docker
docker:
docker build . -t post-service:latest

32
blog/posts/README.md Normal file
View File

@@ -0,0 +1,32 @@
# Post Service
The posts service stores posts
## Usage
### Create a post
```
micro call posts Posts.Save '{"post":{"id":"1","title":"How to Micro","content":"Simply put, Micro is awesome."}}'
micro call posts Posts.Save '{"post":{"id":"2","title":"Fresh posts are fresh","content":"This post is fresher than the How to Micro one"}}'
```
### Create a post with tags
```
micro call posts Posts.Save '{"post":{"id":"3","title":"How to do epic things with Micro","content":"Everything is awesome.","tagNames":["a","b"]}}'
```
### Query posts
```
micro call posts Posts.Query '{}'
micro call posts Posts.Query '{"slug":"how-to-micro"}'
micro call posts Posts.Query '{"offset": 10, "limit": 10}'
```
### Delete posts
```
micro call posts Posts.Delete '{"offset": 10, "limit": 10}'
```

3
blog/posts/generate.go Normal file
View File

@@ -0,0 +1,3 @@
package main
//go:generate make proto

258
blog/posts/handler/posts.go Normal file
View File

@@ -0,0 +1,258 @@
package handler
import (
"context"
"encoding/json"
"fmt"
"math"
"time"
"github.com/micro/go-micro/v3/errors"
gostore "github.com/micro/go-micro/v3/store"
"github.com/micro/micro/v3/service/logger"
"github.com/micro/micro/v3/service/store"
"github.com/gosimple/slug"
pb "github.com/micro/services/blog/posts/proto/posts"
posts "github.com/micro/services/blog/posts/proto/posts"
tags "github.com/micro/services/blog/tags/proto"
)
const (
tagType = "post-tag"
slugPrefix = "slug"
idPrefix = "id"
timeStampPrefix = "timestamp"
)
type Post struct {
ID string `json:"id"`
Title string `json:"title"`
Slug string `json:"slug"`
Content string `json:"content"`
CreateTimestamp int64 `json:"create_timestamp"`
UpdateTimestamp int64 `json:"update_timestamp"`
TagNames []string `json:"tagNames"`
}
type Posts struct {
Tags tags.TagsService
}
func (p *Posts) Save(ctx context.Context, req *posts.SaveRequest, rsp *posts.SaveResponse) error {
if len(req.Post.Id) == 0 || len(req.Post.Title) == 0 || len(req.Post.Content) == 0 {
return errors.BadRequest("posts.save.input-check", "Id, title or content is missing")
}
// read by post
records, err := store.Read(fmt.Sprintf("%v:%v", idPrefix, req.Post.Id))
if err != nil && err != gostore.ErrNotFound {
return errors.InternalServerError("posts.save.store-id-read", "Failed to read post by id: %v", err.Error())
}
postSlug := slug.Make(req.Post.Title)
// If no existing record is found, create a new one
if len(records) == 0 {
post := &Post{
ID: req.Post.Id,
Title: req.Post.Title,
Content: req.Post.Content,
TagNames: req.Post.TagNames,
Slug: postSlug,
CreateTimestamp: time.Now().Unix(),
}
err := p.savePost(ctx, nil, post)
if err != nil {
return errors.InternalServerError("posts.save.post-save", "Failed to save new post: %v", err.Error())
}
return nil
}
record := records[0]
oldPost := &Post{}
err = json.Unmarshal(record.Value, oldPost)
if err != nil {
return errors.InternalServerError("posts.save.unmarshal", "Failed to unmarshal old post: %v", err.Error())
}
post := &Post{
ID: req.Post.Id,
Title: req.Post.Title,
Content: req.Post.Content,
Slug: postSlug,
TagNames: req.Post.TagNames,
CreateTimestamp: oldPost.CreateTimestamp,
UpdateTimestamp: time.Now().Unix(),
}
// Check if slug exists
recordsBySlug, err := store.Read(fmt.Sprintf("%v:%v", slugPrefix, postSlug))
if err != nil && err != gostore.ErrNotFound {
return errors.InternalServerError("posts.save.store-read", "Failed to read post by slug: %v", err.Error())
}
otherSlugPost := &Post{}
err = json.Unmarshal(record.Value, otherSlugPost)
if err != nil {
return errors.InternalServerError("posts.save.slug-unmarshal", "Error unmarshaling other post with same slug: %v", err.Error())
}
if len(recordsBySlug) > 0 && oldPost.ID != otherSlugPost.ID {
return errors.BadRequest("posts.save.slug-check", "An other post with this slug already exists")
}
return p.savePost(ctx, oldPost, post)
}
func (p *Posts) savePost(ctx context.Context, oldPost, post *Post) error {
bytes, err := json.Marshal(post)
if err != nil {
return err
}
err = store.Write(&gostore.Record{
Key: fmt.Sprintf("%v:%v", idPrefix, post.ID),
Value: bytes,
})
if err != nil {
return err
}
// Delete old slug index if the slug has changed
if oldPost != nil && oldPost.Slug != post.Slug {
err = store.Delete(fmt.Sprintf("%v:%v", slugPrefix, post.Slug))
if err != nil {
return err
}
}
err = store.Write(&gostore.Record{
Key: fmt.Sprintf("%v:%v", slugPrefix, post.Slug),
Value: bytes,
})
if err != nil {
return err
}
err = store.Write(&gostore.Record{
Key: fmt.Sprintf("%v:%v", timeStampPrefix, math.MaxInt64-post.CreateTimestamp),
Value: bytes,
})
if err != nil {
return err
}
if oldPost == nil {
for _, tagName := range post.TagNames {
_, err := p.Tags.IncreaseCount(ctx, &tags.IncreaseCountRequest{
ParentID: post.ID,
Type: tagType,
Title: tagName,
})
if err != nil {
return err
}
}
return nil
}
return p.diffTags(ctx, post.ID, oldPost.TagNames, post.TagNames)
}
func (p *Posts) diffTags(ctx context.Context, parentID string, oldTagNames, newTagNames []string) error {
oldTags := map[string]struct{}{}
for _, v := range oldTagNames {
oldTags[v] = struct{}{}
}
newTags := map[string]struct{}{}
for _, v := range newTagNames {
newTags[v] = struct{}{}
}
for i := range oldTags {
_, stillThere := newTags[i]
if !stillThere {
_, err := p.Tags.DecreaseCount(ctx, &tags.DecreaseCountRequest{
ParentID: parentID,
Type: tagType,
Title: i,
})
if err != nil {
logger.Errorf("Error decreasing count for tag '%v' with type '%v' for parent '%v'", i, tagType, parentID)
}
}
}
for i := range newTags {
_, newlyAdded := oldTags[i]
if newlyAdded {
_, err := p.Tags.IncreaseCount(ctx, &tags.IncreaseCountRequest{
ParentID: parentID,
Type: tagType,
Title: i,
})
if err != nil {
logger.Errorf("Error increasing count for tag '%v' with type '%v' for parent '%v'", i, tagType, parentID)
}
}
}
return nil
}
func (p *Posts) Query(ctx context.Context, req *pb.QueryRequest, rsp *pb.QueryResponse) error {
var records []*gostore.Record
var err error
if len(req.Slug) > 0 {
key := fmt.Sprintf("%v:%v", slugPrefix, req.Slug)
logger.Infof("Reading post by slug: %v", req.Slug)
records, err = store.Read("", store.Prefix(key))
} else {
key := fmt.Sprintf("%v:", timeStampPrefix)
var limit uint
limit = 20
if req.Limit > 0 {
limit = uint(req.Limit)
}
logger.Infof("Listing posts, offset: %v, limit: %v", req.Offset, limit)
records, err = store.Read("", store.Prefix(key),
store.Offset(uint(req.Offset)),
store.Limit(limit))
}
if err != nil {
return errors.BadRequest("posts.query.store-read", "Failed to read from store: %v", err.Error())
}
rsp.Posts = make([]*pb.Post, len(records))
for i, record := range records {
postRecord := &Post{}
err := json.Unmarshal(record.Value, postRecord)
if err != nil {
return errors.InternalServerError("posts.save.unmarshal", "Failed to unmarshal old post: %v", err.Error())
}
rsp.Posts[i] = &pb.Post{
Id: postRecord.ID,
Title: postRecord.Title,
Slug: postRecord.Slug,
Content: postRecord.Content,
TagNames: postRecord.TagNames,
}
}
return nil
}
func (p *Posts) Delete(ctx context.Context, req *pb.DeleteRequest, rsp *pb.DeleteResponse) error {
logger.Info("Received Post.Delete request")
records, err := store.Read(fmt.Sprintf("%v:%v", idPrefix, req.Id))
if err != nil && err != gostore.ErrNotFound {
return err
}
if len(records) == 0 {
return fmt.Errorf("Post with ID %v not found", req.Id)
}
post := &Post{}
err = json.Unmarshal(records[0].Value, post)
if err != nil {
return err
}
// Delete by ID
err = store.Delete(fmt.Sprintf("%v:%v", idPrefix, post.ID))
if err != nil {
return err
}
// Delete by slug
err = store.Delete(fmt.Sprintf("%v:%v", slugPrefix, post.Slug))
if err != nil {
return err
}
// Delete by timeStamp
return store.Delete(fmt.Sprintf("%v:%v", timeStampPrefix, post.CreateTimestamp))
}

25
blog/posts/main.go Normal file
View File

@@ -0,0 +1,25 @@
package main
import (
"github.com/micro/micro/v3/service"
"github.com/micro/micro/v3/service/logger"
"github.com/micro/services/blog/posts/handler"
tags "github.com/micro/services/blog/tags/proto"
)
func main() {
// Create the service
srv := service.New(
service.Name("posts"),
)
// Register Handler
srv.Handle(&handler.Posts{
Tags: tags.NewTagsService("tags", srv.Client()),
})
// Run service
if err := srv.Run(); err != nil {
logger.Fatal(err)
}
}

View File

@@ -0,0 +1,380 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// source: blog/posts/proto/posts/post.proto
package posts
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 Post 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"`
Slug string `protobuf:"bytes,3,opt,name=slug,proto3" json:"slug,omitempty"`
Content string `protobuf:"bytes,4,opt,name=content,proto3" json:"content,omitempty"`
Timestamp int64 `protobuf:"varint,5,opt,name=timestamp,proto3" json:"timestamp,omitempty"`
TagNames []string `protobuf:"bytes,6,rep,name=tagNames,proto3" json:"tagNames,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *Post) Reset() { *m = Post{} }
func (m *Post) String() string { return proto.CompactTextString(m) }
func (*Post) ProtoMessage() {}
func (*Post) Descriptor() ([]byte, []int) {
return fileDescriptor_2d32cca1c2f74735, []int{0}
}
func (m *Post) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_Post.Unmarshal(m, b)
}
func (m *Post) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_Post.Marshal(b, m, deterministic)
}
func (m *Post) XXX_Merge(src proto.Message) {
xxx_messageInfo_Post.Merge(m, src)
}
func (m *Post) XXX_Size() int {
return xxx_messageInfo_Post.Size(m)
}
func (m *Post) XXX_DiscardUnknown() {
xxx_messageInfo_Post.DiscardUnknown(m)
}
var xxx_messageInfo_Post proto.InternalMessageInfo
func (m *Post) GetId() string {
if m != nil {
return m.Id
}
return ""
}
func (m *Post) GetTitle() string {
if m != nil {
return m.Title
}
return ""
}
func (m *Post) GetSlug() string {
if m != nil {
return m.Slug
}
return ""
}
func (m *Post) GetContent() string {
if m != nil {
return m.Content
}
return ""
}
func (m *Post) GetTimestamp() int64 {
if m != nil {
return m.Timestamp
}
return 0
}
func (m *Post) GetTagNames() []string {
if m != nil {
return m.TagNames
}
return nil
}
type QueryRequest struct {
Slug string `protobuf:"bytes,1,opt,name=slug,proto3" json:"slug,omitempty"`
Offset int64 `protobuf:"varint,2,opt,name=offset,proto3" json:"offset,omitempty"`
Limit int64 `protobuf:"varint,3,opt,name=limit,proto3" json:"limit,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *QueryRequest) Reset() { *m = QueryRequest{} }
func (m *QueryRequest) String() string { return proto.CompactTextString(m) }
func (*QueryRequest) ProtoMessage() {}
func (*QueryRequest) Descriptor() ([]byte, []int) {
return fileDescriptor_2d32cca1c2f74735, []int{1}
}
func (m *QueryRequest) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_QueryRequest.Unmarshal(m, b)
}
func (m *QueryRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_QueryRequest.Marshal(b, m, deterministic)
}
func (m *QueryRequest) XXX_Merge(src proto.Message) {
xxx_messageInfo_QueryRequest.Merge(m, src)
}
func (m *QueryRequest) XXX_Size() int {
return xxx_messageInfo_QueryRequest.Size(m)
}
func (m *QueryRequest) XXX_DiscardUnknown() {
xxx_messageInfo_QueryRequest.DiscardUnknown(m)
}
var xxx_messageInfo_QueryRequest proto.InternalMessageInfo
func (m *QueryRequest) GetSlug() string {
if m != nil {
return m.Slug
}
return ""
}
func (m *QueryRequest) GetOffset() int64 {
if m != nil {
return m.Offset
}
return 0
}
func (m *QueryRequest) GetLimit() int64 {
if m != nil {
return m.Limit
}
return 0
}
type QueryResponse struct {
Posts []*Post `protobuf:"bytes,1,rep,name=posts,proto3" json:"posts,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *QueryResponse) Reset() { *m = QueryResponse{} }
func (m *QueryResponse) String() string { return proto.CompactTextString(m) }
func (*QueryResponse) ProtoMessage() {}
func (*QueryResponse) Descriptor() ([]byte, []int) {
return fileDescriptor_2d32cca1c2f74735, []int{2}
}
func (m *QueryResponse) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_QueryResponse.Unmarshal(m, b)
}
func (m *QueryResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_QueryResponse.Marshal(b, m, deterministic)
}
func (m *QueryResponse) XXX_Merge(src proto.Message) {
xxx_messageInfo_QueryResponse.Merge(m, src)
}
func (m *QueryResponse) XXX_Size() int {
return xxx_messageInfo_QueryResponse.Size(m)
}
func (m *QueryResponse) XXX_DiscardUnknown() {
xxx_messageInfo_QueryResponse.DiscardUnknown(m)
}
var xxx_messageInfo_QueryResponse proto.InternalMessageInfo
func (m *QueryResponse) GetPosts() []*Post {
if m != nil {
return m.Posts
}
return nil
}
type SaveRequest struct {
Post *Post `protobuf:"bytes,1,opt,name=post,proto3" json:"post,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *SaveRequest) Reset() { *m = SaveRequest{} }
func (m *SaveRequest) String() string { return proto.CompactTextString(m) }
func (*SaveRequest) ProtoMessage() {}
func (*SaveRequest) Descriptor() ([]byte, []int) {
return fileDescriptor_2d32cca1c2f74735, []int{3}
}
func (m *SaveRequest) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_SaveRequest.Unmarshal(m, b)
}
func (m *SaveRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_SaveRequest.Marshal(b, m, deterministic)
}
func (m *SaveRequest) XXX_Merge(src proto.Message) {
xxx_messageInfo_SaveRequest.Merge(m, src)
}
func (m *SaveRequest) XXX_Size() int {
return xxx_messageInfo_SaveRequest.Size(m)
}
func (m *SaveRequest) XXX_DiscardUnknown() {
xxx_messageInfo_SaveRequest.DiscardUnknown(m)
}
var xxx_messageInfo_SaveRequest proto.InternalMessageInfo
func (m *SaveRequest) GetPost() *Post {
if m != nil {
return m.Post
}
return nil
}
type SaveResponse struct {
Post *Post `protobuf:"bytes,1,opt,name=post,proto3" json:"post,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *SaveResponse) Reset() { *m = SaveResponse{} }
func (m *SaveResponse) String() string { return proto.CompactTextString(m) }
func (*SaveResponse) ProtoMessage() {}
func (*SaveResponse) Descriptor() ([]byte, []int) {
return fileDescriptor_2d32cca1c2f74735, []int{4}
}
func (m *SaveResponse) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_SaveResponse.Unmarshal(m, b)
}
func (m *SaveResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_SaveResponse.Marshal(b, m, deterministic)
}
func (m *SaveResponse) XXX_Merge(src proto.Message) {
xxx_messageInfo_SaveResponse.Merge(m, src)
}
func (m *SaveResponse) XXX_Size() int {
return xxx_messageInfo_SaveResponse.Size(m)
}
func (m *SaveResponse) XXX_DiscardUnknown() {
xxx_messageInfo_SaveResponse.DiscardUnknown(m)
}
var xxx_messageInfo_SaveResponse proto.InternalMessageInfo
func (m *SaveResponse) GetPost() *Post {
if m != nil {
return m.Post
}
return nil
}
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_2d32cca1c2f74735, []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_2d32cca1c2f74735, []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
func init() {
proto.RegisterType((*Post)(nil), "posts.Post")
proto.RegisterType((*QueryRequest)(nil), "posts.QueryRequest")
proto.RegisterType((*QueryResponse)(nil), "posts.QueryResponse")
proto.RegisterType((*SaveRequest)(nil), "posts.SaveRequest")
proto.RegisterType((*SaveResponse)(nil), "posts.SaveResponse")
proto.RegisterType((*DeleteRequest)(nil), "posts.DeleteRequest")
proto.RegisterType((*DeleteResponse)(nil), "posts.DeleteResponse")
}
func init() { proto.RegisterFile("blog/posts/proto/posts/post.proto", fileDescriptor_2d32cca1c2f74735) }
var fileDescriptor_2d32cca1c2f74735 = []byte{
// 343 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x84, 0x92, 0x4d, 0x4e, 0xc3, 0x30,
0x10, 0x85, 0x71, 0xf3, 0x03, 0x9d, 0xfe, 0x08, 0x4d, 0x0b, 0xb2, 0x22, 0xa4, 0xa6, 0x59, 0x65,
0x95, 0x8a, 0x82, 0xc4, 0x05, 0x58, 0xa3, 0x62, 0x4e, 0x90, 0x52, 0xb7, 0x8a, 0x94, 0xd4, 0xa1,
0x9e, 0x22, 0x71, 0x0f, 0x2e, 0xc1, 0x2d, 0x51, 0xec, 0x24, 0xb4, 0xd9, 0xb0, 0xf3, 0xfb, 0xec,
0x79, 0xf3, 0x66, 0x12, 0x98, 0xaf, 0x73, 0xb5, 0x5b, 0x94, 0x4a, 0x93, 0x5e, 0x94, 0x07, 0x45,
0xaa, 0x39, 0x2b, 0x4d, 0x89, 0x01, 0xe8, 0x19, 0x12, 0x7d, 0x33, 0x70, 0x57, 0x4a, 0x13, 0x8e,
0xa1, 0x97, 0x6d, 0x38, 0x0b, 0x59, 0xdc, 0x17, 0xbd, 0x6c, 0x83, 0x53, 0xf0, 0x28, 0xa3, 0x5c,
0xf2, 0x9e, 0x41, 0x56, 0x20, 0x82, 0xab, 0xf3, 0xe3, 0x8e, 0x3b, 0x06, 0x9a, 0x33, 0x72, 0xb8,
0x7c, 0x57, 0x7b, 0x92, 0x7b, 0xe2, 0xae, 0xc1, 0x8d, 0xc4, 0x3b, 0xe8, 0x53, 0x56, 0x48, 0x4d,
0x69, 0x51, 0x72, 0x2f, 0x64, 0xb1, 0x23, 0xfe, 0x00, 0x06, 0x70, 0x45, 0xe9, 0xee, 0x25, 0x2d,
0xa4, 0xe6, 0x7e, 0xe8, 0xc4, 0x7d, 0xd1, 0xea, 0x68, 0x05, 0xc3, 0xd7, 0xa3, 0x3c, 0x7c, 0x09,
0xf9, 0x71, 0x94, 0x9a, 0xda, 0xbe, 0xec, 0xa4, 0xef, 0x2d, 0xf8, 0x6a, 0xbb, 0xd5, 0x92, 0x4c,
0x44, 0x47, 0xd4, 0xaa, 0x4a, 0x9e, 0x67, 0x45, 0x46, 0x26, 0xa4, 0x23, 0xac, 0x88, 0x96, 0x30,
0xaa, 0x1d, 0x75, 0xa9, 0xf6, 0x5a, 0xe2, 0x1c, 0xec, 0x0a, 0x38, 0x0b, 0x9d, 0x78, 0xb0, 0x1c,
0x24, 0x46, 0x25, 0xd5, 0x32, 0x44, 0xbd, 0x9c, 0x04, 0x06, 0x6f, 0xe9, 0xa7, 0x6c, 0x42, 0xcc,
0xc0, 0xad, 0xb8, 0x09, 0xd1, 0x29, 0x30, 0x17, 0xd1, 0x02, 0x86, 0xf6, 0x7d, 0xdd, 0xe2, 0xdf,
0x82, 0x19, 0x8c, 0x9e, 0x65, 0x2e, 0xa9, 0x6d, 0xd1, 0xf9, 0x0a, 0xd1, 0x35, 0x8c, 0x9b, 0x07,
0xd6, 0x73, 0xf9, 0xc3, 0xc0, 0xab, 0x1c, 0x34, 0x3e, 0x82, 0x67, 0x26, 0xc2, 0x49, 0x6d, 0x7c,
0xba, 0xb1, 0x60, 0x7a, 0x0e, 0x6d, 0x75, 0x74, 0x81, 0xf7, 0xe0, 0x56, 0x19, 0x11, 0xeb, 0xfb,
0x93, 0x01, 0x83, 0xc9, 0x19, 0x6b, 0x4b, 0x9e, 0xc0, 0xb7, 0x21, 0xb0, 0x31, 0x3d, 0x0b, 0x1d,
0xdc, 0x74, 0x68, 0x53, 0xb8, 0xf6, 0xcd, 0xaf, 0xf6, 0xf0, 0x1b, 0x00, 0x00, 0xff, 0xff, 0xd5,
0xd2, 0x06, 0xac, 0x8f, 0x02, 0x00, 0x00,
}

View File

@@ -0,0 +1,129 @@
// Code generated by protoc-gen-micro. DO NOT EDIT.
// source: blog/posts/proto/posts/post.proto
package posts
import (
fmt "fmt"
proto "github.com/golang/protobuf/proto"
math "math"
)
import (
context "context"
api "github.com/micro/go-micro/v3/api"
client "github.com/micro/go-micro/v3/client"
server "github.com/micro/go-micro/v3/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 Posts service
func NewPostsEndpoints() []*api.Endpoint {
return []*api.Endpoint{}
}
// Client API for Posts service
type PostsService interface {
// Query currently only supports read by slug or timestamp, no listing.
Query(ctx context.Context, in *QueryRequest, opts ...client.CallOption) (*QueryResponse, error)
Save(ctx context.Context, in *SaveRequest, opts ...client.CallOption) (*SaveResponse, error)
Delete(ctx context.Context, in *DeleteRequest, opts ...client.CallOption) (*DeleteResponse, error)
}
type postsService struct {
c client.Client
name string
}
func NewPostsService(name string, c client.Client) PostsService {
return &postsService{
c: c,
name: name,
}
}
func (c *postsService) Query(ctx context.Context, in *QueryRequest, opts ...client.CallOption) (*QueryResponse, error) {
req := c.c.NewRequest(c.name, "Posts.Query", in)
out := new(QueryResponse)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *postsService) Save(ctx context.Context, in *SaveRequest, opts ...client.CallOption) (*SaveResponse, error) {
req := c.c.NewRequest(c.name, "Posts.Save", in)
out := new(SaveResponse)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *postsService) Delete(ctx context.Context, in *DeleteRequest, opts ...client.CallOption) (*DeleteResponse, error) {
req := c.c.NewRequest(c.name, "Posts.Delete", in)
out := new(DeleteResponse)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// Server API for Posts service
type PostsHandler interface {
// Query currently only supports read by slug or timestamp, no listing.
Query(context.Context, *QueryRequest, *QueryResponse) error
Save(context.Context, *SaveRequest, *SaveResponse) error
Delete(context.Context, *DeleteRequest, *DeleteResponse) error
}
func RegisterPostsHandler(s server.Server, hdlr PostsHandler, opts ...server.HandlerOption) error {
type posts interface {
Query(ctx context.Context, in *QueryRequest, out *QueryResponse) error
Save(ctx context.Context, in *SaveRequest, out *SaveResponse) error
Delete(ctx context.Context, in *DeleteRequest, out *DeleteResponse) error
}
type Posts struct {
posts
}
h := &postsHandler{hdlr}
return s.Handle(s.NewHandler(&Posts{h}, opts...))
}
type postsHandler struct {
PostsHandler
}
func (h *postsHandler) Query(ctx context.Context, in *QueryRequest, out *QueryResponse) error {
return h.PostsHandler.Query(ctx, in, out)
}
func (h *postsHandler) Save(ctx context.Context, in *SaveRequest, out *SaveResponse) error {
return h.PostsHandler.Save(ctx, in, out)
}
func (h *postsHandler) Delete(ctx context.Context, in *DeleteRequest, out *DeleteResponse) error {
return h.PostsHandler.Delete(ctx, in, out)
}

View File

@@ -0,0 +1,45 @@
syntax = "proto3";
package posts;
service Posts {
// Query currently only supports read by slug or timestamp, no listing.
rpc Query(QueryRequest) returns (QueryResponse) {}
rpc Save(SaveRequest) returns (SaveResponse) {}
rpc Delete(DeleteRequest) returns (DeleteResponse) {}
}
message Post {
string id = 1;
string title = 2;
string slug = 3;
string content = 4;
int64 timestamp = 5;
repeated string tagNames = 6;
}
message QueryRequest {
string slug = 1;
int64 offset = 2;
int64 limit = 3;
}
message QueryResponse {
repeated Post posts = 1;
}
message SaveRequest {
Post post = 1;
}
message SaveResponse {
Post post = 1;
}
message DeleteRequest {
string id = 1;
}
message DeleteResponse {
}