comment

package
v0.0.0-...-8e0b234 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 28, 2024 License: OSL-3.0 Imports: 8 Imported by: 0

README

评论模块

  • 对内提供RPC
  • 对外提供Restful 接口

业务定义

Comment

BlogId: 哪个博客
User: 谁发起的评论
Content: 评论内容
CreateAt: 评论时间
CreateComment(CreateCommentRequest) *Comment

通过编写Protobuf来定义业务接口

service Service {
    rpc CreateComment(CreateCommnetRequest) returns (Comment);
}

message Comment {
    // 评论Id
    string id = 1;
    // 10位时间戳
    int64 create_at = 2;
    // 评论具体定义
    CreateCommnetRequest spec = 3;
}

message CreateCommnetRequest {
    // 被评论文章Id
    string blog_id = 3;
    // 评论的用户
    string user_id = 4;
    // 评论内容
    string content = 5;
}

代码生成

protoc -I=. --go_out=. --go-grpc_out=. --go-grpc_opt=module="gitee.com/baicaijc/vblog" --go_opt=module="gitee.com/baicaijc/vblog" apps/*/pb/*.proto

无法嵌套, 导致数据多层

数据结构多层

{id: '', create_at: 0, spec: {}}
  1. 扁平化存储: 输入入库需要扁平化存储: GORM:
  • embedded 嵌套字段
  • embeddedPrefix 嵌入字段的列名前缀
  1. 扁平化展示: 使用匿名结构图, 重新调整数据结构, 数据在展示的时候重新组装:
// {"id":"","create_at":0,"content":"test"}
jd2, _ := json.Marshal(struct {
    Id       string `json:"id"`
    CreateAt int64  `json:"create_at"`
    *comment.CreateCommnetRequest
}{
    Id:                   ins.Id,
    CreateAt:             ins.CreateAt,
    CreateCommnetRequest: ins.Spec,
})

无法添加自定义Tag

github.com/favadi/protoc-go-inject-tag

  • gorm
  • mongo
  • validate

安装插件:

go install github.com/favadi/protoc-go-inject-tag@latest
  1. 补充Tag
protoc-go-inject-tag -input="*.pb.go"
  1. 执行命令
protoc-go-inject-tag -input="apps/*/*.pb.go" 
  1. 为了方便make gen
gen: ## Gen Code
	@protoc -I=. --go_out=. --go-grpc_out=. --go-grpc_opt=module="gitee.com/baicaijc/vblog" --go_opt=module="gitee.com/baicaijc/vblog" apps/*/pb/*.proto
	@protoc-go-inject-tag -input="apps/*/*.pb.go" 

接口最小权限设计

  • 进程内 调用: 权限最大, API Handler --> ServiceImpl
  • RPC 内部调用: 安全级别? 基本的安全, 数据修改类接口 适不适合 走RPC, 通过RPC删除文章? 是否允许通过RPC查询文章基本信息?
  • Restful API 调用: 调用的安全级别? 给 Web <-- 代表用户自己的行为

Documentation

Index

Constants

View Source
const (
	AppName = "comment"
)
View Source
const (
	Service_CreateComment_FullMethodName = "/go13.vblog.comment.Service/CreateComment"
)

Variables

View Source
var File_apps_comment_pb_interface_proto protoreflect.FileDescriptor
View Source
var File_apps_comment_pb_model_proto protoreflect.FileDescriptor
View Source
var Service_ServiceDesc = grpc.ServiceDesc{
	ServiceName: "go13.vblog.comment.Service",
	HandlerType: (*ServiceServer)(nil),
	Methods: []grpc.MethodDesc{
		{
			MethodName: "CreateComment",
			Handler:    _Service_CreateComment_Handler,
		},
	},
	Streams:  []grpc.StreamDesc{},
	Metadata: "apps/comment/pb/interface.proto",
}

Service_ServiceDesc is the grpc.ServiceDesc for Service service. It's only intended for direct use with grpc.RegisterService, and not to be introspected or modified (even as a copy)

Functions

func RegisterServiceServer

func RegisterServiceServer(s grpc.ServiceRegistrar, srv ServiceServer)

Types

type Comment

type Comment struct {

	// 评论Id
	// @gotags: json:"id"
	Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id"`
	// 10位时间戳
	// @gotags: json:"create_at"
	CreateAt int64 `protobuf:"varint,2,opt,name=create_at,json=createAt,proto3" json:"create_at"`
	// 评论具体定义
	// @gotags: json:"spec" gorm:"embedded"
	Spec *CreateCommnetRequest `protobuf:"bytes,3,opt,name=spec,proto3" json:"spec" gorm:"embedded"`
	// contains filtered or unexported fields
}

func (*Comment) Descriptor deprecated

func (*Comment) Descriptor() ([]byte, []int)

Deprecated: Use Comment.ProtoReflect.Descriptor instead.

func (*Comment) GetCreateAt

func (x *Comment) GetCreateAt() int64

func (*Comment) GetId

func (x *Comment) GetId() string

func (*Comment) GetSpec

func (x *Comment) GetSpec() *CreateCommnetRequest

func (*Comment) ProtoMessage

func (*Comment) ProtoMessage()

func (*Comment) ProtoReflect

func (x *Comment) ProtoReflect() protoreflect.Message

func (*Comment) Reset

func (x *Comment) Reset()

func (*Comment) String

func (x *Comment) String() string

type CreateCommnetRequest

type CreateCommnetRequest struct {

	// 被评论文章Id
	// @gotags: json:"blog_id"
	BlogId string `protobuf:"bytes,3,opt,name=blog_id,json=blogId,proto3" json:"blog_id"`
	// 评论的用户
	// @gotags: json:"user_id"
	UserId string `protobuf:"bytes,4,opt,name=user_id,json=userId,proto3" json:"user_id"`
	// 评论内容
	// @gotags: json:"content"
	Content string `protobuf:"bytes,5,opt,name=content,proto3" json:"content"`
	// contains filtered or unexported fields
}

func (*CreateCommnetRequest) Descriptor deprecated

func (*CreateCommnetRequest) Descriptor() ([]byte, []int)

Deprecated: Use CreateCommnetRequest.ProtoReflect.Descriptor instead.

func (*CreateCommnetRequest) GetBlogId

func (x *CreateCommnetRequest) GetBlogId() string

func (*CreateCommnetRequest) GetContent

func (x *CreateCommnetRequest) GetContent() string

func (*CreateCommnetRequest) GetUserId

func (x *CreateCommnetRequest) GetUserId() string

func (*CreateCommnetRequest) ProtoMessage

func (*CreateCommnetRequest) ProtoMessage()

func (*CreateCommnetRequest) ProtoReflect

func (x *CreateCommnetRequest) ProtoReflect() protoreflect.Message

func (*CreateCommnetRequest) Reset

func (x *CreateCommnetRequest) Reset()

func (*CreateCommnetRequest) String

func (x *CreateCommnetRequest) String() string

type ServiceClient

type ServiceClient interface {
	CreateComment(ctx context.Context, in *CreateCommnetRequest, opts ...grpc.CallOption) (*Comment, error)
}

ServiceClient is the client API for Service service.

For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.

func NewServiceClient

func NewServiceClient(cc grpc.ClientConnInterface) ServiceClient

type ServiceServer

type ServiceServer interface {
	CreateComment(context.Context, *CreateCommnetRequest) (*Comment, error)
	// contains filtered or unexported methods
}

ServiceServer is the server API for Service service. All implementations must embed UnimplementedServiceServer for forward compatibility

type UnimplementedServiceServer

type UnimplementedServiceServer struct {
}

UnimplementedServiceServer must be embedded to have forward compatible implementations.

func (UnimplementedServiceServer) CreateComment

type UnsafeServiceServer

type UnsafeServiceServer interface {
	// contains filtered or unexported methods
}

UnsafeServiceServer may be embedded to opt out of forward compatibility for this service. Use of this interface is not recommended, as added methods to ServiceServer will result in compilation errors.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL