Merge pull request #1 from viletyy/dev_grpc

add grpc to staging
This commit is contained in:
来自村里的小螃蟹 2021-06-20 22:33:02 +08:00 committed by GitHub
commit 914e93086a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 1032 additions and 43 deletions

View File

@ -12,7 +12,7 @@ app:
upload_image_max_size: 5 # MB
upload_image_allow_exts: ['.jpg','.jpeg','.png']
server:
http_port: 8000
port: 8000
read_timeout: 60
write_timeout: 60
tracer_host_port: '127.0.0.1:6831'

View File

@ -1,13 +1,13 @@
/*
* @Date: 2021-03-22 09:54:07
* @LastEditors: viletyy
* @LastEditTime: 2021-06-14 20:40:49
* @LastEditTime: 2021-06-20 19:45:31
* @FilePath: /potato/config/server.go
*/
package config
type Server struct {
HttpPort int64 `mapstructure:"http_port" json:"http_port" yaml:"http_port"`
Port int64 `mapstructure:"port" json:"port" yaml:"port"`
ReadTimeout int64 `mapstructure:"read_timeout" json:"read_timeout" yaml:"read_timeout"`
WriteTimeout int64 `mapstructure:"write_timeout" json:"write_timeout" yaml:"write_timeout"`
TracerHostPort string `mapstructure:"tracer_host_port" json:"tracer_host_port" yaml:"tracer_host_port"`

5
go.mod
View File

@ -20,6 +20,8 @@ require (
github.com/onsi/ginkgo v1.16.4 // indirect
github.com/onsi/gomega v1.13.0 // indirect
github.com/opentracing/opentracing-go v1.2.0
github.com/smacker/opentracing-gorm v0.0.0-20181207094635-cd4974441042 // indirect
github.com/soheilhy/cmux v0.1.5
github.com/spf13/viper v1.7.1
github.com/swaggo/gin-swagger v1.3.0
github.com/swaggo/swag v1.7.0
@ -28,6 +30,9 @@ require (
github.com/viletyy/yolk v1.0.1
go.uber.org/atomic v1.8.0 // indirect
go.uber.org/zap v1.17.0
golang.org/x/net v0.0.0-20210614182718-04defd469f4e // indirect
google.golang.org/grpc v1.38.0
google.golang.org/protobuf v1.26.0
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
)

22
initialize/grpc_server.go Normal file
View File

@ -0,0 +1,22 @@
/*
* @Date: 2021-06-17 00:19:32
* @LastEditors: viletyy
* @LastEditTime: 2021-06-20 19:46:17
* @FilePath: /potato/initialize/grpc_server.go
*/
package initialize
import (
pb "github.com/viletyy/potato/proto/basic"
"github.com/viletyy/potato/server/basic"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
func RunGrpcServer() *grpc.Server {
server := grpc.NewServer()
pb.RegisterVendorServiceServer(server, basic.NewVendorServer())
reflection.Register(server)
return server
}

28
initialize/http_server.go Normal file
View File

@ -0,0 +1,28 @@
/*
* @Date: 2021-03-22 17:03:27
* @LastEditors: viletyy
* @LastEditTime: 2021-06-20 22:22:10
* @FilePath: /potato/initialize/http_server.go
*/
package initialize
import (
"net/http"
"time"
"github.com/viletyy/potato/global"
"github.com/viletyy/potato/internal/routers"
)
func RunHttpServer(port string) *http.Server {
router := routers.InitRouter()
server := &http.Server{
Addr: port,
Handler: router,
ReadTimeout: time.Duration(global.GO_CONFIG.Server.ReadTimeout) * time.Second,
WriteTimeout: time.Duration(global.GO_CONFIG.Server.ReadTimeout) * time.Second,
MaxHeaderBytes: 1 << 20,
}
return server
}

View File

@ -1,50 +1,28 @@
/*
* @Date: 2021-03-22 17:03:27
* @LastEditors: viletyy
* @LastEditTime: 2021-06-10 22:24:39
* @LastEditTime: 2021-06-20 19:18:10
* @FilePath: /potato/initialize/server.go
*/
package initialize
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"time"
"github.com/viletyy/potato/global"
"github.com/viletyy/potato/internal/routers"
)
func RunServer() {
func RunServer(port string) *http.Server {
router := routers.InitRouter()
server := &http.Server{
Addr: fmt.Sprintf(":%d", global.GO_CONFIG.Server.HttpPort),
Addr: port,
Handler: router,
ReadTimeout: time.Duration(global.GO_CONFIG.Server.ReadTimeout) * time.Second,
WriteTimeout: time.Duration(global.GO_CONFIG.Server.ReadTimeout) * time.Second,
MaxHeaderBytes: 1 << 20,
}
go func() {
if err := server.ListenAndServe(); err != nil {
global.GO_LOG.Info(fmt.Sprintf("Listen: %s\n", err))
}
}()
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt)
<-quit
global.GO_LOG.Info("Shutdown Server")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
global.GO_LOG.Fatal(fmt.Sprintf("Server Shutdown: %v", err))
}
global.GO_LOG.Info("Server exiting")
return server
}

View File

@ -14,5 +14,5 @@ type AuthRequest struct {
}
func (svc *Service) CheckAuth(param *AuthRequest) (model.Auth, error) {
return svc.dao.GetAuth(param.AppKey, param.AppSecret)
return svc.Dao.GetAuth(param.AppKey, param.AppSecret)
}

View File

@ -1,7 +1,7 @@
/*
* @Date: 2021-06-10 18:51:48
* @LastEditors: viletyy
* @LastEditTime: 2021-06-14 21:13:01
* @LastEditTime: 2021-06-16 23:58:59
* @FilePath: /potato/internal/service/service.go
*/
package service
@ -16,12 +16,12 @@ import (
type Service struct {
Ctx context.Context
dao *dao.Dao
Dao *dao.Dao
}
func New(ctx context.Context) Service {
svc := Service{Ctx: ctx}
svc.dao = dao.New(otgorm.WithContext(svc.Ctx, global.GO_DB))
svc.Dao = dao.New(otgorm.WithContext(svc.Ctx, global.GO_DB))
return svc
}

View File

@ -1,7 +1,7 @@
/*
* @Date: 2021-06-10 17:57:48
* @LastEditors: viletyy
* @LastEditTime: 2021-06-14 23:19:35
* @LastEditTime: 2021-06-16 23:59:12
* @FilePath: /potato/internal/service/vendor.go
*/
package service
@ -41,25 +41,25 @@ type DeleteVendorRequest struct {
}
func (svc *Service) CountVendor(param *CountVendorRequest) (int, error) {
return svc.dao.CountVendor(param.Name, param.Uuid)
return svc.Dao.CountVendor(param.Name, param.Uuid)
}
func (svc *Service) GetVendorList(param *VendorListRequest, pager *app.Pager) ([]basic.Vendor, error) {
return svc.dao.GetVendorList(param.Name, param.Uuid, pager.Page, pager.PageSize)
return svc.Dao.GetVendorList(param.Name, param.Uuid, pager.Page, pager.PageSize)
}
func (svc *Service) GetVendor(param *VendorRequest) (basic.Vendor, error) {
return svc.dao.GetVendor(param.ID)
return svc.Dao.GetVendor(param.ID)
}
func (svc *Service) CreateVendor(param *CreateVendorRequest) (basic.Vendor, error) {
return svc.dao.CreateVendor(param.Name, param.Uuid)
return svc.Dao.CreateVendor(param.Name, param.Uuid)
}
func (svc *Service) UpdateVendor(param *UpdateVendorRequest) (basic.Vendor, error) {
return svc.dao.UpdateVendor(param.ID, param.Name, param.Uuid)
return svc.Dao.UpdateVendor(param.ID, param.Name, param.Uuid)
}
func (svc *Service) DeleteVendor(param *DeleteVendorRequest) (basic.Vendor, error) {
return svc.dao.DeleteVendor(param.ID)
return svc.Dao.DeleteVendor(param.ID)
}

39
main.go
View File

@ -1,14 +1,19 @@
/*
* @Date: 2021-03-21 19:54:57
* @LastEditors: viletyy
* @LastEditTime: 2021-06-13 23:32:06
* @LastEditTime: 2021-06-20 19:45:38
* @FilePath: /potato/main.go
*/
package main
import (
"flag"
"net"
"github.com/soheilhy/cmux"
"github.com/viletyy/potato/global"
"github.com/viletyy/potato/initialize"
"github.com/viletyy/yolk/convert"
)
// @title Potato Api
@ -16,6 +21,12 @@ import (
// @description This is a potato use golang
// @BasePath /api
var port string
func runTcpServer(port string) (net.Listener, error) {
return net.Listen("tcp", ":"+port)
}
func main() {
global.GO_VP = initialize.Viper()
global.GO_LOG = initialize.Zap()
@ -26,6 +37,30 @@ func main() {
defer global.GO_DB.Close()
defer global.GO_REDIS.Close()
initialize.RunServer()
flag.StringVar(&port, "port", convert.ToString(global.GO_CONFIG.Server.Port), "启动端口号")
flag.Parse()
l, err := runTcpServer(port)
if err != nil {
global.GO_LOG.Sugar().Fatalf("Run Tcp Server err: %v", err)
}
m := cmux.New(l)
grpcL := m.MatchWithWriters(
cmux.HTTP2MatchHeaderFieldSendSettings(
"content-type",
"application/grpc",
),
)
httpL := m.Match(cmux.HTTP1Fast())
grpcS := initialize.RunGrpcServer()
httpS := initialize.RunServer(port)
go grpcS.Serve(grpcL)
go httpS.Serve(httpL)
err = m.Serve()
if err != nil {
global.GO_LOG.Sugar().Fatalf("Run Server err: %v", err)
}
}

View File

@ -1,7 +1,7 @@
/*
* @Date: 2021-06-10 16:30:10
* @LastEditors: viletyy
* @LastEditTime: 2021-06-11 15:36:11
* @LastEditTime: 2021-06-18 22:22:55
* @FilePath: /potato/pkg/errcode/errcode.go
*/
package errcode

View File

@ -0,0 +1,20 @@
/*
* @Date: 2021-06-18 21:37:14
* @LastEditors: viletyy
* @LastEditTime: 2021-06-18 22:21:54
* @FilePath: /potato/pkg/errcode/rpc/rpc_common_code.go
*/
package rpc
var (
RpcSuccess = NewRpcError(0, "成功")
RpcFail = NewRpcError(10000, "内部错误")
RpcInvalidParams = NewRpcError(10001, "无效参数")
RpcUnauthorized = NewRpcError(10002, "认证错误")
RpcNotFound = NewRpcError(10003, "没有找到")
RpcUnknown = NewRpcError(10004, "未知")
RpcDeadlineExceeded = NewRpcError(10005, "超出最后截止期限")
RpcAccessDenied = NewRpcError(10006, "访问被拒绝")
RpcLimitExceed = NewRpcError(10007, "访问限制")
RpcMethodNotAllowed = NewRpcError(10008, "不支持该方法")
)

View File

@ -0,0 +1,81 @@
/*
* @Date: 2021-06-18 21:42:53
* @LastEditors: viletyy
* @LastEditTime: 2021-06-18 22:31:20
* @FilePath: /potato/pkg/errcode/rpc/rpc_errcode.go
*/
package rpc
import (
"fmt"
pb "github.com/viletyy/potato/proto"
codes "google.golang.org/grpc/codes"
status "google.golang.org/grpc/status"
)
type RpcError struct {
Code int
Msg string
}
type Status struct {
*status.Status
}
var rpcCodes = map[int]string{}
func FromError(err error) *Status {
s, _ := status.FromError(err)
return &Status{s}
}
func NewRpcError(code int, msg string) *RpcError {
if _, ok := rpcCodes[code]; ok {
panic(fmt.Sprintf("错误码 %d 已经存在,请更换一个", code))
}
rpcCodes[code] = msg
return &RpcError{Code: code, Msg: msg}
}
func (e *RpcError) Error() string {
return fmt.Sprintf("错误码:%d错误信息%s", e.Code, e.Msg)
}
func ToRpcError(err *RpcError) error {
pbErr := &pb.Error{Code: int32(err.Code), Message: err.Msg}
s, _ := status.New(ToRpcCode(err.Code), err.Msg).WithDetails(pbErr)
return s.Err()
}
func ToRpcStatus(code int, msg string) *Status {
pbErr := &pb.Error{Code: int32(code), Message: msg}
s, _ := status.New(ToRpcCode(code), msg).WithDetails(pbErr)
return &Status{s}
}
func ToRpcCode(code int) codes.Code {
var statusCode codes.Code
switch code {
case RpcFail.Code:
statusCode = codes.Internal
case RpcInvalidParams.Code:
statusCode = codes.InvalidArgument
case RpcUnauthorized.Code:
statusCode = codes.Unauthenticated
case RpcAccessDenied.Code:
statusCode = codes.PermissionDenied
case RpcDeadlineExceeded.Code:
statusCode = codes.DeadlineExceeded
case RpcNotFound.Code:
statusCode = codes.NotFound
case RpcLimitExceed.Code:
statusCode = codes.ResourceExhausted
case RpcMethodNotAllowed.Code:
statusCode = codes.Unimplemented
default:
statusCode = codes.Unknown
}
return statusCode
}

View File

@ -0,0 +1,16 @@
/*
* @Date: 2021-06-18 22:32:02
* @LastEditors: viletyy
* @LastEditTime: 2021-06-18 22:33:40
* @FilePath: /potato/pkg/errcode/rpc/rpc_module_error.go
*/
package rpc
var (
RpcErrorGetVendorListFail = NewRpcError(20101, "获取系统厂商列表失败")
RpcErrorGetVendorFail = NewRpcError(20102, "获取系统厂商失败")
RpcErrorCreateVendorFail = NewRpcError(20103, "创建系统厂商失败")
RpcErrorUpdateVendorFail = NewRpcError(20104, "更新系统厂商失败")
RpcErrorDeleteVendorFail = NewRpcError(20105, "删除系统厂商失败")
RpcErrorCountVendorFail = NewRpcError(20106, "统计系统厂商失败")
)

338
proto/basic/vendor.pb.go Normal file
View File

@ -0,0 +1,338 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.26.0
// protoc v3.17.1
// source: proto/basic/vendor.proto
package basic
import (
proto "github.com/viletyy/potato/proto"
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
reflect "reflect"
sync "sync"
)
const (
// Verify that this generated code is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
// Verify that runtime/protoimpl is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
type GetVendorListRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
Uuid int64 `protobuf:"varint,2,opt,name=uuid,proto3" json:"uuid,omitempty"`
Page int64 `protobuf:"varint,3,opt,name=page,proto3" json:"page,omitempty"`
PageSize int64 `protobuf:"varint,4,opt,name=page_size,json=pageSize,proto3" json:"page_size,omitempty"`
}
func (x *GetVendorListRequest) Reset() {
*x = GetVendorListRequest{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_basic_vendor_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *GetVendorListRequest) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*GetVendorListRequest) ProtoMessage() {}
func (x *GetVendorListRequest) ProtoReflect() protoreflect.Message {
mi := &file_proto_basic_vendor_proto_msgTypes[0]
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 GetVendorListRequest.ProtoReflect.Descriptor instead.
func (*GetVendorListRequest) Descriptor() ([]byte, []int) {
return file_proto_basic_vendor_proto_rawDescGZIP(), []int{0}
}
func (x *GetVendorListRequest) GetName() string {
if x != nil {
return x.Name
}
return ""
}
func (x *GetVendorListRequest) GetUuid() int64 {
if x != nil {
return x.Uuid
}
return 0
}
func (x *GetVendorListRequest) GetPage() int64 {
if x != nil {
return x.Page
}
return 0
}
func (x *GetVendorListRequest) GetPageSize() int64 {
if x != nil {
return x.PageSize
}
return 0
}
type Vendor struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Id int64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"`
Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`
Uuid int64 `protobuf:"varint,3,opt,name=uuid,proto3" json:"uuid,omitempty"`
}
func (x *Vendor) Reset() {
*x = Vendor{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_basic_vendor_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *Vendor) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*Vendor) ProtoMessage() {}
func (x *Vendor) ProtoReflect() protoreflect.Message {
mi := &file_proto_basic_vendor_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 Vendor.ProtoReflect.Descriptor instead.
func (*Vendor) Descriptor() ([]byte, []int) {
return file_proto_basic_vendor_proto_rawDescGZIP(), []int{1}
}
func (x *Vendor) GetId() int64 {
if x != nil {
return x.Id
}
return 0
}
func (x *Vendor) GetName() string {
if x != nil {
return x.Name
}
return ""
}
func (x *Vendor) GetUuid() int64 {
if x != nil {
return x.Uuid
}
return 0
}
type GetVendorListReply struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
List []*Vendor `protobuf:"bytes,1,rep,name=list,proto3" json:"list,omitempty"`
Pager *proto.Pager `protobuf:"bytes,2,opt,name=pager,proto3" json:"pager,omitempty"`
}
func (x *GetVendorListReply) Reset() {
*x = GetVendorListReply{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_basic_vendor_proto_msgTypes[2]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *GetVendorListReply) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*GetVendorListReply) ProtoMessage() {}
func (x *GetVendorListReply) ProtoReflect() protoreflect.Message {
mi := &file_proto_basic_vendor_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 GetVendorListReply.ProtoReflect.Descriptor instead.
func (*GetVendorListReply) Descriptor() ([]byte, []int) {
return file_proto_basic_vendor_proto_rawDescGZIP(), []int{2}
}
func (x *GetVendorListReply) GetList() []*Vendor {
if x != nil {
return x.List
}
return nil
}
func (x *GetVendorListReply) GetPager() *proto.Pager {
if x != nil {
return x.Pager
}
return nil
}
var File_proto_basic_vendor_proto protoreflect.FileDescriptor
var file_proto_basic_vendor_proto_rawDesc = []byte{
0x0a, 0x18, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x62, 0x61, 0x73, 0x69, 0x63, 0x2f, 0x76, 0x65,
0x6e, 0x64, 0x6f, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x12, 0x70, 0x72, 0x6f, 0x74,
0x6f, 0x2f, 0x63, 0x6f, 0x6d, 0x6d, 0x6f, 0x6e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x6f,
0x0a, 0x14, 0x47, 0x65, 0x74, 0x56, 0x65, 0x6e, 0x64, 0x6f, 0x72, 0x4c, 0x69, 0x73, 0x74, 0x52,
0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01,
0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x75, 0x75,
0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x04, 0x75, 0x75, 0x69, 0x64, 0x12, 0x12,
0x0a, 0x04, 0x70, 0x61, 0x67, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x04, 0x70, 0x61,
0x67, 0x65, 0x12, 0x1b, 0x0a, 0x09, 0x70, 0x61, 0x67, 0x65, 0x5f, 0x73, 0x69, 0x7a, 0x65, 0x18,
0x04, 0x20, 0x01, 0x28, 0x03, 0x52, 0x08, 0x70, 0x61, 0x67, 0x65, 0x53, 0x69, 0x7a, 0x65, 0x22,
0x40, 0x0a, 0x06, 0x56, 0x65, 0x6e, 0x64, 0x6f, 0x72, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18,
0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x02, 0x69, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d,
0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x12, 0x0a,
0x04, 0x75, 0x75, 0x69, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x04, 0x75, 0x75, 0x69,
0x64, 0x22, 0x4f, 0x0a, 0x12, 0x47, 0x65, 0x74, 0x56, 0x65, 0x6e, 0x64, 0x6f, 0x72, 0x4c, 0x69,
0x73, 0x74, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, 0x1b, 0x0a, 0x04, 0x6c, 0x69, 0x73, 0x74, 0x18,
0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x07, 0x2e, 0x56, 0x65, 0x6e, 0x64, 0x6f, 0x72, 0x52, 0x04,
0x6c, 0x69, 0x73, 0x74, 0x12, 0x1c, 0x0a, 0x05, 0x70, 0x61, 0x67, 0x65, 0x72, 0x18, 0x02, 0x20,
0x01, 0x28, 0x0b, 0x32, 0x06, 0x2e, 0x50, 0x61, 0x67, 0x65, 0x72, 0x52, 0x05, 0x70, 0x61, 0x67,
0x65, 0x72, 0x32, 0x4e, 0x0a, 0x0d, 0x56, 0x65, 0x6e, 0x64, 0x6f, 0x72, 0x53, 0x65, 0x72, 0x76,
0x69, 0x63, 0x65, 0x12, 0x3d, 0x0a, 0x0d, 0x47, 0x65, 0x74, 0x56, 0x65, 0x6e, 0x64, 0x6f, 0x72,
0x4c, 0x69, 0x73, 0x74, 0x12, 0x15, 0x2e, 0x47, 0x65, 0x74, 0x56, 0x65, 0x6e, 0x64, 0x6f, 0x72,
0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x13, 0x2e, 0x47, 0x65,
0x74, 0x56, 0x65, 0x6e, 0x64, 0x6f, 0x72, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x70, 0x6c, 0x79,
0x22, 0x00, 0x42, 0x27, 0x5a, 0x25, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d,
0x2f, 0x76, 0x69, 0x6c, 0x65, 0x74, 0x79, 0x79, 0x2f, 0x70, 0x6f, 0x74, 0x61, 0x74, 0x6f, 0x2f,
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x62, 0x61, 0x73, 0x69, 0x63, 0x62, 0x06, 0x70, 0x72, 0x6f,
0x74, 0x6f, 0x33,
}
var (
file_proto_basic_vendor_proto_rawDescOnce sync.Once
file_proto_basic_vendor_proto_rawDescData = file_proto_basic_vendor_proto_rawDesc
)
func file_proto_basic_vendor_proto_rawDescGZIP() []byte {
file_proto_basic_vendor_proto_rawDescOnce.Do(func() {
file_proto_basic_vendor_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_basic_vendor_proto_rawDescData)
})
return file_proto_basic_vendor_proto_rawDescData
}
var file_proto_basic_vendor_proto_msgTypes = make([]protoimpl.MessageInfo, 3)
var file_proto_basic_vendor_proto_goTypes = []interface{}{
(*GetVendorListRequest)(nil), // 0: GetVendorListRequest
(*Vendor)(nil), // 1: Vendor
(*GetVendorListReply)(nil), // 2: GetVendorListReply
(*proto.Pager)(nil), // 3: Pager
}
var file_proto_basic_vendor_proto_depIdxs = []int32{
1, // 0: GetVendorListReply.list:type_name -> Vendor
3, // 1: GetVendorListReply.pager:type_name -> Pager
0, // 2: VendorService.GetVendorList:input_type -> GetVendorListRequest
2, // 3: VendorService.GetVendorList:output_type -> GetVendorListReply
3, // [3:4] is the sub-list for method output_type
2, // [2:3] 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
}
func init() { file_proto_basic_vendor_proto_init() }
func file_proto_basic_vendor_proto_init() {
if File_proto_basic_vendor_proto != nil {
return
}
if !protoimpl.UnsafeEnabled {
file_proto_basic_vendor_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*GetVendorListRequest); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_proto_basic_vendor_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*Vendor); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_proto_basic_vendor_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*GetVendorListReply); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_proto_basic_vendor_proto_rawDesc,
NumEnums: 0,
NumMessages: 3,
NumExtensions: 0,
NumServices: 1,
},
GoTypes: file_proto_basic_vendor_proto_goTypes,
DependencyIndexes: file_proto_basic_vendor_proto_depIdxs,
MessageInfos: file_proto_basic_vendor_proto_msgTypes,
}.Build()
File_proto_basic_vendor_proto = out.File
file_proto_basic_vendor_proto_rawDesc = nil
file_proto_basic_vendor_proto_goTypes = nil
file_proto_basic_vendor_proto_depIdxs = nil
}

27
proto/basic/vendor.proto Normal file
View File

@ -0,0 +1,27 @@
syntax = "proto3";
option go_package = "github.com/viletyy/potato/proto/basic";
import "proto/common.proto";
service VendorService {
rpc GetVendorList(GetVendorListRequest) returns (GetVendorListReply) {}
}
message GetVendorListRequest {
string name = 1;
int64 uuid = 2;
int64 page = 3;
int64 page_size = 4;
}
message Vendor {
int64 id = 1;
string name = 2;
int64 uuid = 3;
}
message GetVendorListReply {
repeated Vendor list = 1;
Pager pager = 2;
}

View File

@ -0,0 +1,101 @@
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
package basic
import (
context "context"
grpc "google.golang.org/grpc"
codes "google.golang.org/grpc/codes"
status "google.golang.org/grpc/status"
)
// This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against.
// Requires gRPC-Go v1.32.0 or later.
const _ = grpc.SupportPackageIsVersion7
// VendorServiceClient is the client API for VendorService 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.
type VendorServiceClient interface {
GetVendorList(ctx context.Context, in *GetVendorListRequest, opts ...grpc.CallOption) (*GetVendorListReply, error)
}
type vendorServiceClient struct {
cc grpc.ClientConnInterface
}
func NewVendorServiceClient(cc grpc.ClientConnInterface) VendorServiceClient {
return &vendorServiceClient{cc}
}
func (c *vendorServiceClient) GetVendorList(ctx context.Context, in *GetVendorListRequest, opts ...grpc.CallOption) (*GetVendorListReply, error) {
out := new(GetVendorListReply)
err := c.cc.Invoke(ctx, "/VendorService/GetVendorList", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// VendorServiceServer is the server API for VendorService service.
// All implementations must embed UnimplementedVendorServiceServer
// for forward compatibility
type VendorServiceServer interface {
GetVendorList(context.Context, *GetVendorListRequest) (*GetVendorListReply, error)
mustEmbedUnimplementedVendorServiceServer()
}
// UnimplementedVendorServiceServer must be embedded to have forward compatible implementations.
type UnimplementedVendorServiceServer struct {
}
func (UnimplementedVendorServiceServer) GetVendorList(context.Context, *GetVendorListRequest) (*GetVendorListReply, error) {
return nil, status.Errorf(codes.Unimplemented, "method GetVendorList not implemented")
}
func (UnimplementedVendorServiceServer) mustEmbedUnimplementedVendorServiceServer() {}
// UnsafeVendorServiceServer may be embedded to opt out of forward compatibility for this service.
// Use of this interface is not recommended, as added methods to VendorServiceServer will
// result in compilation errors.
type UnsafeVendorServiceServer interface {
mustEmbedUnimplementedVendorServiceServer()
}
func RegisterVendorServiceServer(s grpc.ServiceRegistrar, srv VendorServiceServer) {
s.RegisterService(&VendorService_ServiceDesc, srv)
}
func _VendorService_GetVendorList_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(GetVendorListRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(VendorServiceServer).GetVendorList(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/VendorService/GetVendorList",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(VendorServiceServer).GetVendorList(ctx, req.(*GetVendorListRequest))
}
return interceptor(ctx, in, info, handler)
}
// VendorService_ServiceDesc is the grpc.ServiceDesc for VendorService service.
// It's only intended for direct use with grpc.RegisterService,
// and not to be introspected or modified (even as a copy)
var VendorService_ServiceDesc = grpc.ServiceDesc{
ServiceName: "VendorService",
HandlerType: (*VendorServiceServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "GetVendorList",
Handler: _VendorService_GetVendorList_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "proto/basic/vendor.proto",
}

248
proto/common.pb.go Normal file
View File

@ -0,0 +1,248 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.26.0
// protoc v3.17.1
// source: proto/common.proto
package proto
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
anypb "google.golang.org/protobuf/types/known/anypb"
reflect "reflect"
sync "sync"
)
const (
// Verify that this generated code is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
// Verify that runtime/protoimpl is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
type Pager struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Page int64 `protobuf:"varint,1,opt,name=page,proto3" json:"page,omitempty"`
PageSize int64 `protobuf:"varint,2,opt,name=page_size,json=pageSize,proto3" json:"page_size,omitempty"`
Total int64 `protobuf:"varint,3,opt,name=total,proto3" json:"total,omitempty"`
}
func (x *Pager) Reset() {
*x = Pager{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_common_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *Pager) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*Pager) ProtoMessage() {}
func (x *Pager) ProtoReflect() protoreflect.Message {
mi := &file_proto_common_proto_msgTypes[0]
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 Pager.ProtoReflect.Descriptor instead.
func (*Pager) Descriptor() ([]byte, []int) {
return file_proto_common_proto_rawDescGZIP(), []int{0}
}
func (x *Pager) GetPage() int64 {
if x != nil {
return x.Page
}
return 0
}
func (x *Pager) GetPageSize() int64 {
if x != nil {
return x.PageSize
}
return 0
}
func (x *Pager) GetTotal() int64 {
if x != nil {
return x.Total
}
return 0
}
type Error struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Code int32 `protobuf:"varint,1,opt,name=code,proto3" json:"code,omitempty"`
Message string `protobuf:"bytes,2,opt,name=message,proto3" json:"message,omitempty"`
Data *anypb.Any `protobuf:"bytes,3,opt,name=data,proto3" json:"data,omitempty"`
}
func (x *Error) Reset() {
*x = Error{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_common_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *Error) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*Error) ProtoMessage() {}
func (x *Error) ProtoReflect() protoreflect.Message {
mi := &file_proto_common_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 Error.ProtoReflect.Descriptor instead.
func (*Error) Descriptor() ([]byte, []int) {
return file_proto_common_proto_rawDescGZIP(), []int{1}
}
func (x *Error) GetCode() int32 {
if x != nil {
return x.Code
}
return 0
}
func (x *Error) GetMessage() string {
if x != nil {
return x.Message
}
return ""
}
func (x *Error) GetData() *anypb.Any {
if x != nil {
return x.Data
}
return nil
}
var File_proto_common_proto protoreflect.FileDescriptor
var file_proto_common_proto_rawDesc = []byte{
0x0a, 0x12, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x63, 0x6f, 0x6d, 0x6d, 0x6f, 0x6e, 0x2e, 0x70,
0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x19, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f,
0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x61, 0x6e, 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22,
0x4e, 0x0a, 0x05, 0x50, 0x61, 0x67, 0x65, 0x72, 0x12, 0x12, 0x0a, 0x04, 0x70, 0x61, 0x67, 0x65,
0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x04, 0x70, 0x61, 0x67, 0x65, 0x12, 0x1b, 0x0a, 0x09,
0x70, 0x61, 0x67, 0x65, 0x5f, 0x73, 0x69, 0x7a, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52,
0x08, 0x70, 0x61, 0x67, 0x65, 0x53, 0x69, 0x7a, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x74,
0x61, 0x6c, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x74, 0x6f, 0x74, 0x61, 0x6c, 0x22,
0x5f, 0x0a, 0x05, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65,
0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x12, 0x18, 0x0a, 0x07,
0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x6d,
0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x28, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x03,
0x20, 0x01, 0x28, 0x0b, 0x32, 0x14, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72,
0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x41, 0x6e, 0x79, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61,
0x42, 0x21, 0x5a, 0x1f, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x76,
0x69, 0x6c, 0x65, 0x74, 0x79, 0x79, 0x2f, 0x70, 0x6f, 0x74, 0x61, 0x74, 0x6f, 0x2f, 0x70, 0x72,
0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
file_proto_common_proto_rawDescOnce sync.Once
file_proto_common_proto_rawDescData = file_proto_common_proto_rawDesc
)
func file_proto_common_proto_rawDescGZIP() []byte {
file_proto_common_proto_rawDescOnce.Do(func() {
file_proto_common_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_common_proto_rawDescData)
})
return file_proto_common_proto_rawDescData
}
var file_proto_common_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
var file_proto_common_proto_goTypes = []interface{}{
(*Pager)(nil), // 0: Pager
(*Error)(nil), // 1: Error
(*anypb.Any)(nil), // 2: google.protobuf.Any
}
var file_proto_common_proto_depIdxs = []int32{
2, // 0: Error.data:type_name -> google.protobuf.Any
1, // [1:1] is the sub-list for method output_type
1, // [1:1] is the sub-list for method input_type
1, // [1:1] is the sub-list for extension type_name
1, // [1:1] is the sub-list for extension extendee
0, // [0:1] is the sub-list for field type_name
}
func init() { file_proto_common_proto_init() }
func file_proto_common_proto_init() {
if File_proto_common_proto != nil {
return
}
if !protoimpl.UnsafeEnabled {
file_proto_common_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*Pager); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_proto_common_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*Error); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_proto_common_proto_rawDesc,
NumEnums: 0,
NumMessages: 2,
NumExtensions: 0,
NumServices: 0,
},
GoTypes: file_proto_common_proto_goTypes,
DependencyIndexes: file_proto_common_proto_depIdxs,
MessageInfos: file_proto_common_proto_msgTypes,
}.Build()
File_proto_common_proto = out.File
file_proto_common_proto_rawDesc = nil
file_proto_common_proto_goTypes = nil
file_proto_common_proto_depIdxs = nil
}

17
proto/common.proto Normal file
View File

@ -0,0 +1,17 @@
syntax = "proto3";
import "google/protobuf/any.proto";
option go_package= "github.com/viletyy/potato/proto";
message Pager {
int64 page = 1;
int64 page_size = 2;
int64 total = 3;
}
message Error {
int32 code = 1;
string message = 2;
google.protobuf.Any data = 3;
}

View File

@ -0,0 +1,9 @@
###
# @Date: 2021-06-20 21:43:20
# @LastEditors: viletyy
# @LastEditTime: 2021-06-20 22:24:50
# @FilePath: /potato/scripts/gen_basic_proto.sh
###
protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
./proto/basic/*.proto

9
scripts/gen_proto.sh Normal file
View File

@ -0,0 +1,9 @@
###
# @Date: 2021-06-20 21:43:20
# @LastEditors: viletyy
# @LastEditTime: 2021-06-20 22:23:27
# @FilePath: /potato/scripts/gen_proto.sh
###
protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
./proto/*.proto

55
server/basic/vendor.go Normal file
View File

@ -0,0 +1,55 @@
/*
* @Date: 2021-06-16 23:49:02
* @LastEditors: viletyy
* @LastEditTime: 2021-06-18 22:39:40
* @FilePath: /potato/server/basic/vendor.go
*/
package basic
import (
"context"
"encoding/json"
"github.com/viletyy/potato/internal/service"
"github.com/viletyy/potato/pkg/app"
"github.com/viletyy/potato/pkg/errcode/rpc"
pb "github.com/viletyy/potato/proto/basic"
)
type VendorServer struct {
pb.UnimplementedVendorServiceServer
}
func NewVendorServer() *VendorServer {
return &VendorServer{}
}
func (t *VendorServer) GetVendorList(ctx context.Context, r *pb.GetVendorListRequest) (*pb.GetVendorListReply, error) {
svc := service.New(ctx)
dbVendorList, err := svc.Dao.GetVendorList(r.GetName(), int(r.GetUuid()), int(r.GetPage()), int(r.GetPageSize()))
if err != nil {
return nil, rpc.ToRpcError(rpc.RpcErrorGetVendorListFail)
}
total, err := svc.Dao.CountVendor(r.GetName(), int(r.GetUuid()))
if err != nil {
return nil, rpc.ToRpcError(rpc.RpcErrorCountVendorFail)
}
data := map[string]interface{}{
"list": dbVendorList,
"pager": app.Pager{
Page: int(r.GetPage()),
PageSize: int(r.GetPageSize()),
Total: total,
},
}
byteData, err := json.Marshal(data)
if err != nil {
return nil, rpc.ToRpcError(rpc.RpcInvalidParams)
}
vendorList := pb.GetVendorListReply{}
err = json.Unmarshal(byteData, &vendorList)
if err != nil {
return nil, rpc.ToRpcError(rpc.RpcInvalidParams)
}
return &vendorList, nil
}