Commit 28bc9099 by Joneq

ready

parents
.idea
*.properties
*.iml
/logs
.DS_Store?
.DS_Store
/configs
/sh
go.sum
\ No newline at end of file
## Demo
### v1.0.0
1. 上线功能xxx
# Author
# Reviewer
# Demo
## 项目简介
1.
// Code generated by protoc-gen-bm v0.1, DO NOT EDIT.
// source: api.proto
/*
Package api is a generated blademaster stub package.
This code was generated with kratos/tool/protobuf/protoc-gen-bm v0.1.
package 命名使用 {appid}.{version} 的方式, version 形如 v1, v2 ..
It is generated from these files:
api.proto
*/
package api
import (
"context"
bm "github.com/go-kratos/kratos/pkg/net/http/blademaster"
"github.com/go-kratos/kratos/pkg/net/http/blademaster/binding"
)
import google_protobuf1 "github.com/golang/protobuf/ptypes/empty"
// to suppressed 'imported but not used warning'
var _ *bm.Context
var _ context.Context
var _ binding.StructValidator
var PathDemoPing = "/demo.service.v1.Demo/Ping"
var PathDemoSayHello = "/demo.service.v1.Demo/SayHello"
var PathDemoSayHelloURL = "/kratos-demo/say_hello"
// DemoBMServer is the server API for Demo service.
type DemoBMServer interface {
Ping(ctx context.Context, req *google_protobuf1.Empty) (resp *google_protobuf1.Empty, err error)
SayHello(ctx context.Context, req *HelloReq) (resp *google_protobuf1.Empty, err error)
SayHelloURL(ctx context.Context, req *HelloReq) (resp *HelloResp, err error)
}
var DemoSvc DemoBMServer
func demoPing(c *bm.Context) {
p := new(google_protobuf1.Empty)
if err := c.BindWith(p, binding.Default(c.Request.Method, c.Request.Header.Get("Content-Type"))); err != nil {
return
}
resp, err := DemoSvc.Ping(c, p)
c.JSON(resp, err)
}
func demoSayHello(c *bm.Context) {
p := new(HelloReq)
if err := c.BindWith(p, binding.Default(c.Request.Method, c.Request.Header.Get("Content-Type"))); err != nil {
return
}
resp, err := DemoSvc.SayHello(c, p)
c.JSON(resp, err)
}
func demoSayHelloURL(c *bm.Context) {
p := new(HelloReq)
if err := c.BindWith(p, binding.Default(c.Request.Method, c.Request.Header.Get("Content-Type"))); err != nil {
return
}
resp, err := DemoSvc.SayHelloURL(c, p)
c.JSON(resp, err)
}
// RegisterDemoBMServer Register the blademaster route
func RegisterDemoBMServer(e *bm.Engine, server DemoBMServer) {
DemoSvc = server
e.GET("/demo.service.v1.Demo/Ping", demoPing)
e.GET("/demo.service.v1.Demo/SayHello", demoSayHello)
e.GET("/kratos-demo/say_hello", demoSayHelloURL)
}
// Code generated by protoc-gen-gogo. DO NOT EDIT.
// source: api.proto
// package 命名使用 {appid}.{version} 的方式, version 形如 v1, v2 ..
package api
import (
context "context"
fmt "fmt"
_ "github.com/gogo/protobuf/gogoproto"
proto "github.com/golang/protobuf/proto"
empty "github.com/golang/protobuf/ptypes/empty"
_ "google.golang.org/genproto/googleapis/api/annotations"
grpc "google.golang.org/grpc"
codes "google.golang.org/grpc/codes"
status "google.golang.org/grpc/status"
io "io"
math "math"
math_bits "math/bits"
)
// 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 HelloReq struct {
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty" form:"name" validate:"required"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *HelloReq) Reset() { *m = HelloReq{} }
func (m *HelloReq) String() string { return proto.CompactTextString(m) }
func (*HelloReq) ProtoMessage() {}
func (*HelloReq) Descriptor() ([]byte, []int) {
return fileDescriptor_00212fb1f9d3bf1c, []int{0}
}
func (m *HelloReq) XXX_Unmarshal(b []byte) error {
return m.Unmarshal(b)
}
func (m *HelloReq) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
if deterministic {
return xxx_messageInfo_HelloReq.Marshal(b, m, deterministic)
} else {
b = b[:cap(b)]
n, err := m.MarshalToSizedBuffer(b)
if err != nil {
return nil, err
}
return b[:n], nil
}
}
func (m *HelloReq) XXX_Merge(src proto.Message) {
xxx_messageInfo_HelloReq.Merge(m, src)
}
func (m *HelloReq) XXX_Size() int {
return m.Size()
}
func (m *HelloReq) XXX_DiscardUnknown() {
xxx_messageInfo_HelloReq.DiscardUnknown(m)
}
var xxx_messageInfo_HelloReq proto.InternalMessageInfo
type HelloResp struct {
Content string `protobuf:"bytes,1,opt,name=Content,proto3" json:"content"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *HelloResp) Reset() { *m = HelloResp{} }
func (m *HelloResp) String() string { return proto.CompactTextString(m) }
func (*HelloResp) ProtoMessage() {}
func (*HelloResp) Descriptor() ([]byte, []int) {
return fileDescriptor_00212fb1f9d3bf1c, []int{1}
}
func (m *HelloResp) XXX_Unmarshal(b []byte) error {
return m.Unmarshal(b)
}
func (m *HelloResp) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
if deterministic {
return xxx_messageInfo_HelloResp.Marshal(b, m, deterministic)
} else {
b = b[:cap(b)]
n, err := m.MarshalToSizedBuffer(b)
if err != nil {
return nil, err
}
return b[:n], nil
}
}
func (m *HelloResp) XXX_Merge(src proto.Message) {
xxx_messageInfo_HelloResp.Merge(m, src)
}
func (m *HelloResp) XXX_Size() int {
return m.Size()
}
func (m *HelloResp) XXX_DiscardUnknown() {
xxx_messageInfo_HelloResp.DiscardUnknown(m)
}
var xxx_messageInfo_HelloResp proto.InternalMessageInfo
func init() {
proto.RegisterType((*HelloReq)(nil), "demo.service.v1.HelloReq")
proto.RegisterType((*HelloResp)(nil), "demo.service.v1.HelloResp")
}
func init() { proto.RegisterFile("api.proto", fileDescriptor_00212fb1f9d3bf1c) }
var fileDescriptor_00212fb1f9d3bf1c = []byte{
// 341 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x50, 0x3f, 0x4b, 0xc3, 0x40,
0x14, 0x6f, 0xb4, 0xd8, 0xf6, 0x3a, 0x08, 0x37, 0x94, 0x36, 0x4a, 0x5a, 0x22, 0x82, 0x4b, 0x2f,
0x58, 0x41, 0xa1, 0xe0, 0xd2, 0x2a, 0x38, 0x38, 0x48, 0xc4, 0xc5, 0x45, 0xae, 0xcd, 0x35, 0x3d,
0x4c, 0xee, 0x5d, 0x93, 0x6b, 0xa1, 0xab, 0x5f, 0xc1, 0xc5, 0x8f, 0xd4, 0x51, 0x70, 0x2f, 0xda,
0x3a, 0x39, 0xfa, 0x09, 0x24, 0x97, 0x04, 0x41, 0xa8, 0xdb, 0xbd, 0xdf, 0xbf, 0x7b, 0xbf, 0x87,
0x2a, 0x54, 0x72, 0x22, 0x23, 0x50, 0x80, 0x77, 0x3d, 0x16, 0x02, 0x89, 0x59, 0x34, 0xe3, 0x43,
0x46, 0x66, 0xc7, 0x66, 0xdb, 0xe7, 0x6a, 0x3c, 0x1d, 0x90, 0x21, 0x84, 0x8e, 0x0f, 0x3e, 0x38,
0x5a, 0x37, 0x98, 0x8e, 0xf4, 0xa4, 0x07, 0xfd, 0x4a, 0xfd, 0xe6, 0x9e, 0x0f, 0xe0, 0x07, 0xec,
0x57, 0xc5, 0x42, 0xa9, 0xe6, 0x19, 0xb9, 0x9f, 0x91, 0x54, 0x72, 0x87, 0x0a, 0x01, 0x8a, 0x2a,
0x0e, 0x22, 0x4e, 0x59, 0xbb, 0x8f, 0xca, 0x57, 0x2c, 0x08, 0xc0, 0x65, 0x13, 0x7c, 0x86, 0x8a,
0x82, 0x86, 0xac, 0x6e, 0xb4, 0x8c, 0xa3, 0x4a, 0xef, 0xe0, 0x7b, 0xd9, 0x6c, 0x8e, 0x20, 0x0a,
0xbb, 0x76, 0x82, 0xda, 0xad, 0x19, 0x0d, 0xb8, 0x47, 0x15, 0xeb, 0xda, 0x11, 0x9b, 0x4c, 0x79,
0xc4, 0x3c, 0xdb, 0xd5, 0x06, 0xbb, 0x83, 0x2a, 0x59, 0x48, 0x2c, 0xf1, 0x21, 0x2a, 0xf5, 0x41,
0x28, 0x26, 0x54, 0x16, 0x54, 0xfd, 0x5a, 0x36, 0x4b, 0xc3, 0x14, 0x72, 0x73, 0xae, 0xb3, 0x36,
0x50, 0xf1, 0x82, 0x85, 0x80, 0x4f, 0x51, 0xf1, 0x86, 0x0b, 0x1f, 0xd7, 0x48, 0xba, 0x28, 0xc9,
0x5b, 0x90, 0xcb, 0xa4, 0x85, 0xb9, 0x01, 0xc7, 0xe7, 0xa8, 0x7c, 0x4b, 0xe7, 0xfa, 0x5f, 0xdc,
0x20, 0x7f, 0x2e, 0x48, 0xf2, 0x52, 0x1b, 0xed, 0x1e, 0xaa, 0xe6, 0xf6, 0x3b, 0xf7, 0xfa, 0xbf,
0x04, 0x73, 0x13, 0x15, 0x4b, 0xdb, 0x7a, 0x7a, 0xfb, 0x7c, 0xde, 0xaa, 0xe3, 0x9a, 0xf3, 0x18,
0x51, 0x05, 0x71, 0x3b, 0x91, 0x3a, 0x31, 0x9d, 0x3f, 0x8c, 0x13, 0x4d, 0xaf, 0xb1, 0xf8, 0xb0,
0x0a, 0x8b, 0x95, 0x65, 0xbc, 0xae, 0x2c, 0xe3, 0x7d, 0x65, 0x19, 0x2f, 0x6b, 0xab, 0x70, 0xbf,
0x4d, 0x25, 0x1f, 0xec, 0xe8, 0x85, 0x4e, 0x7e, 0x02, 0x00, 0x00, 0xff, 0xff, 0x59, 0x9e, 0x74,
0x3f, 0x08, 0x02, 0x00, 0x00,
}
// Reference imports to suppress errors if they are not otherwise used.
var _ context.Context
var _ grpc.ClientConn
// This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against.
const _ = grpc.SupportPackageIsVersion4
// DemoClient is the client API for Demo service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.
type DemoClient interface {
Ping(ctx context.Context, in *empty.Empty, opts ...grpc.CallOption) (*empty.Empty, error)
SayHello(ctx context.Context, in *HelloReq, opts ...grpc.CallOption) (*empty.Empty, error)
SayHelloURL(ctx context.Context, in *HelloReq, opts ...grpc.CallOption) (*HelloResp, error)
}
type demoClient struct {
cc *grpc.ClientConn
}
func NewDemoClient(cc *grpc.ClientConn) DemoClient {
return &demoClient{cc}
}
func (c *demoClient) Ping(ctx context.Context, in *empty.Empty, opts ...grpc.CallOption) (*empty.Empty, error) {
out := new(empty.Empty)
err := c.cc.Invoke(ctx, "/demo.service.v1.Demo/Ping", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *demoClient) SayHello(ctx context.Context, in *HelloReq, opts ...grpc.CallOption) (*empty.Empty, error) {
out := new(empty.Empty)
err := c.cc.Invoke(ctx, "/demo.service.v1.Demo/SayHello", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *demoClient) SayHelloURL(ctx context.Context, in *HelloReq, opts ...grpc.CallOption) (*HelloResp, error) {
out := new(HelloResp)
err := c.cc.Invoke(ctx, "/demo.service.v1.Demo/SayHelloURL", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// DemoServer is the server API for Demo service.
type DemoServer interface {
Ping(context.Context, *empty.Empty) (*empty.Empty, error)
SayHello(context.Context, *HelloReq) (*empty.Empty, error)
SayHelloURL(context.Context, *HelloReq) (*HelloResp, error)
}
// UnimplementedDemoServer can be embedded to have forward compatible implementations.
type UnimplementedDemoServer struct {
}
func (*UnimplementedDemoServer) Ping(ctx context.Context, req *empty.Empty) (*empty.Empty, error) {
return nil, status.Errorf(codes.Unimplemented, "method Ping not implemented")
}
func (*UnimplementedDemoServer) SayHello(ctx context.Context, req *HelloReq) (*empty.Empty, error) {
return nil, status.Errorf(codes.Unimplemented, "method SayHello not implemented")
}
func (*UnimplementedDemoServer) SayHelloURL(ctx context.Context, req *HelloReq) (*HelloResp, error) {
return nil, status.Errorf(codes.Unimplemented, "method SayHelloURL not implemented")
}
func RegisterDemoServer(s *grpc.Server, srv DemoServer) {
s.RegisterService(&_Demo_serviceDesc, srv)
}
func _Demo_Ping_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(empty.Empty)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(DemoServer).Ping(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/demo.service.v1.Demo/Ping",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(DemoServer).Ping(ctx, req.(*empty.Empty))
}
return interceptor(ctx, in, info, handler)
}
func _Demo_SayHello_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(HelloReq)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(DemoServer).SayHello(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/demo.service.v1.Demo/SayHello",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(DemoServer).SayHello(ctx, req.(*HelloReq))
}
return interceptor(ctx, in, info, handler)
}
func _Demo_SayHelloURL_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(HelloReq)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(DemoServer).SayHelloURL(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/demo.service.v1.Demo/SayHelloURL",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(DemoServer).SayHelloURL(ctx, req.(*HelloReq))
}
return interceptor(ctx, in, info, handler)
}
var _Demo_serviceDesc = grpc.ServiceDesc{
ServiceName: "demo.service.v1.Demo",
HandlerType: (*DemoServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "Ping",
Handler: _Demo_Ping_Handler,
},
{
MethodName: "SayHello",
Handler: _Demo_SayHello_Handler,
},
{
MethodName: "SayHelloURL",
Handler: _Demo_SayHelloURL_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "api.proto",
}
func (m *HelloReq) Marshal() (dAtA []byte, err error) {
size := m.Size()
dAtA = make([]byte, size)
n, err := m.MarshalToSizedBuffer(dAtA[:size])
if err != nil {
return nil, err
}
return dAtA[:n], nil
}
func (m *HelloReq) MarshalTo(dAtA []byte) (int, error) {
size := m.Size()
return m.MarshalToSizedBuffer(dAtA[:size])
}
func (m *HelloReq) MarshalToSizedBuffer(dAtA []byte) (int, error) {
i := len(dAtA)
_ = i
var l int
_ = l
if m.XXX_unrecognized != nil {
i -= len(m.XXX_unrecognized)
copy(dAtA[i:], m.XXX_unrecognized)
}
if len(m.Name) > 0 {
i -= len(m.Name)
copy(dAtA[i:], m.Name)
i = encodeVarintApi(dAtA, i, uint64(len(m.Name)))
i--
dAtA[i] = 0xa
}
return len(dAtA) - i, nil
}
func (m *HelloResp) Marshal() (dAtA []byte, err error) {
size := m.Size()
dAtA = make([]byte, size)
n, err := m.MarshalToSizedBuffer(dAtA[:size])
if err != nil {
return nil, err
}
return dAtA[:n], nil
}
func (m *HelloResp) MarshalTo(dAtA []byte) (int, error) {
size := m.Size()
return m.MarshalToSizedBuffer(dAtA[:size])
}
func (m *HelloResp) MarshalToSizedBuffer(dAtA []byte) (int, error) {
i := len(dAtA)
_ = i
var l int
_ = l
if m.XXX_unrecognized != nil {
i -= len(m.XXX_unrecognized)
copy(dAtA[i:], m.XXX_unrecognized)
}
if len(m.Content) > 0 {
i -= len(m.Content)
copy(dAtA[i:], m.Content)
i = encodeVarintApi(dAtA, i, uint64(len(m.Content)))
i--
dAtA[i] = 0xa
}
return len(dAtA) - i, nil
}
func encodeVarintApi(dAtA []byte, offset int, v uint64) int {
offset -= sovApi(v)
base := offset
for v >= 1<<7 {
dAtA[offset] = uint8(v&0x7f | 0x80)
v >>= 7
offset++
}
dAtA[offset] = uint8(v)
return base
}
func (m *HelloReq) Size() (n int) {
if m == nil {
return 0
}
var l int
_ = l
l = len(m.Name)
if l > 0 {
n += 1 + l + sovApi(uint64(l))
}
if m.XXX_unrecognized != nil {
n += len(m.XXX_unrecognized)
}
return n
}
func (m *HelloResp) Size() (n int) {
if m == nil {
return 0
}
var l int
_ = l
l = len(m.Content)
if l > 0 {
n += 1 + l + sovApi(uint64(l))
}
if m.XXX_unrecognized != nil {
n += len(m.XXX_unrecognized)
}
return n
}
func sovApi(x uint64) (n int) {
return (math_bits.Len64(x|1) + 6) / 7
}
func sozApi(x uint64) (n int) {
return sovApi(uint64((x << 1) ^ uint64((int64(x) >> 63))))
}
func (m *HelloReq) Unmarshal(dAtA []byte) error {
l := len(dAtA)
iNdEx := 0
for iNdEx < l {
preIndex := iNdEx
var wire uint64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowApi
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
wire |= uint64(b&0x7F) << shift
if b < 0x80 {
break
}
}
fieldNum := int32(wire >> 3)
wireType := int(wire & 0x7)
if wireType == 4 {
return fmt.Errorf("proto: HelloReq: wiretype end group for non-group")
}
if fieldNum <= 0 {
return fmt.Errorf("proto: HelloReq: illegal tag %d (wire type %d)", fieldNum, wire)
}
switch fieldNum {
case 1:
if wireType != 2 {
return fmt.Errorf("proto: wrong wireType = %d for field Name", wireType)
}
var stringLen uint64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowApi
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
stringLen |= uint64(b&0x7F) << shift
if b < 0x80 {
break
}
}
intStringLen := int(stringLen)
if intStringLen < 0 {
return ErrInvalidLengthApi
}
postIndex := iNdEx + intStringLen
if postIndex < 0 {
return ErrInvalidLengthApi
}
if postIndex > l {
return io.ErrUnexpectedEOF
}
m.Name = string(dAtA[iNdEx:postIndex])
iNdEx = postIndex
default:
iNdEx = preIndex
skippy, err := skipApi(dAtA[iNdEx:])
if err != nil {
return err
}
if skippy < 0 {
return ErrInvalidLengthApi
}
if (iNdEx + skippy) < 0 {
return ErrInvalidLengthApi
}
if (iNdEx + skippy) > l {
return io.ErrUnexpectedEOF
}
m.XXX_unrecognized = append(m.XXX_unrecognized, dAtA[iNdEx:iNdEx+skippy]...)
iNdEx += skippy
}
}
if iNdEx > l {
return io.ErrUnexpectedEOF
}
return nil
}
func (m *HelloResp) Unmarshal(dAtA []byte) error {
l := len(dAtA)
iNdEx := 0
for iNdEx < l {
preIndex := iNdEx
var wire uint64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowApi
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
wire |= uint64(b&0x7F) << shift
if b < 0x80 {
break
}
}
fieldNum := int32(wire >> 3)
wireType := int(wire & 0x7)
if wireType == 4 {
return fmt.Errorf("proto: HelloResp: wiretype end group for non-group")
}
if fieldNum <= 0 {
return fmt.Errorf("proto: HelloResp: illegal tag %d (wire type %d)", fieldNum, wire)
}
switch fieldNum {
case 1:
if wireType != 2 {
return fmt.Errorf("proto: wrong wireType = %d for field Content", wireType)
}
var stringLen uint64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowApi
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
stringLen |= uint64(b&0x7F) << shift
if b < 0x80 {
break
}
}
intStringLen := int(stringLen)
if intStringLen < 0 {
return ErrInvalidLengthApi
}
postIndex := iNdEx + intStringLen
if postIndex < 0 {
return ErrInvalidLengthApi
}
if postIndex > l {
return io.ErrUnexpectedEOF
}
m.Content = string(dAtA[iNdEx:postIndex])
iNdEx = postIndex
default:
iNdEx = preIndex
skippy, err := skipApi(dAtA[iNdEx:])
if err != nil {
return err
}
if skippy < 0 {
return ErrInvalidLengthApi
}
if (iNdEx + skippy) < 0 {
return ErrInvalidLengthApi
}
if (iNdEx + skippy) > l {
return io.ErrUnexpectedEOF
}
m.XXX_unrecognized = append(m.XXX_unrecognized, dAtA[iNdEx:iNdEx+skippy]...)
iNdEx += skippy
}
}
if iNdEx > l {
return io.ErrUnexpectedEOF
}
return nil
}
func skipApi(dAtA []byte) (n int, err error) {
l := len(dAtA)
iNdEx := 0
depth := 0
for iNdEx < l {
var wire uint64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return 0, ErrIntOverflowApi
}
if iNdEx >= l {
return 0, io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
wire |= (uint64(b) & 0x7F) << shift
if b < 0x80 {
break
}
}
wireType := int(wire & 0x7)
switch wireType {
case 0:
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return 0, ErrIntOverflowApi
}
if iNdEx >= l {
return 0, io.ErrUnexpectedEOF
}
iNdEx++
if dAtA[iNdEx-1] < 0x80 {
break
}
}
case 1:
iNdEx += 8
case 2:
var length int
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return 0, ErrIntOverflowApi
}
if iNdEx >= l {
return 0, io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
length |= (int(b) & 0x7F) << shift
if b < 0x80 {
break
}
}
if length < 0 {
return 0, ErrInvalidLengthApi
}
iNdEx += length
case 3:
depth++
case 4:
if depth == 0 {
return 0, ErrUnexpectedEndOfGroupApi
}
depth--
case 5:
iNdEx += 4
default:
return 0, fmt.Errorf("proto: illegal wireType %d", wireType)
}
if iNdEx < 0 {
return 0, ErrInvalidLengthApi
}
if depth == 0 {
return iNdEx, nil
}
}
return 0, io.ErrUnexpectedEOF
}
var (
ErrInvalidLengthApi = fmt.Errorf("proto: negative length found during unmarshaling")
ErrIntOverflowApi = fmt.Errorf("proto: integer overflow")
ErrUnexpectedEndOfGroupApi = fmt.Errorf("proto: unexpected end of group")
)
// 定义项目 API 的 proto 文件 可以同时描述 gRPC 和 HTTP API
// protobuf 文件参考:
// - https://developers.google.com/protocol-buffers/
syntax = "proto3";
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
import "google/protobuf/empty.proto";
import "google/api/annotations.proto";
// package 命名使用 {appid}.{version} 的方式, version 形如 v1, v2 ..
package demo.service.v1;
// NOTE: 最后请删除这些无用的注释 (゜-゜)つロ
option go_package = "api";
option (gogoproto.goproto_getters_all) = false;
service Demo {
rpc Ping(.google.protobuf.Empty) returns (.google.protobuf.Empty);
rpc SayHello(HelloReq) returns (.google.protobuf.Empty);
rpc SayHelloURL(HelloReq) returns (HelloResp) {
option (google.api.http) = {
get: "/kratos-demo/say_hello"
};
};
}
message HelloReq {
string name = 1 [(gogoproto.moretags) = 'form:"name" validate:"required"'];
}
message HelloResp {
string Content = 1 [(gogoproto.jsontag) = 'content'];
}
package api
import (
"context"
"fmt"
"github.com/go-kratos/kratos/pkg/net/rpc/warden"
"google.golang.org/grpc"
)
// AppID .
const AppID = "TODO: ADD APP ID"
// NewClient new grpc client
func NewClient(cfg *warden.ClientConfig, opts ...grpc.DialOption) (DemoClient, error) {
client := warden.NewClient(cfg, opts...)
cc, err := client.Dial(context.Background(), fmt.Sprintf("discovery://default/%s", AppID))
if err != nil {
return nil, err
}
return NewDemoClient(cc), nil
}
// 生成 gRPC 代码
//go:generate kratos tool protoc --grpc --bm api.proto
package main
import (
"flag"
"os"
"os/signal"
"syscall"
"time"
"photo_taking/internal/di"
"github.com/go-kratos/kratos/pkg/conf/paladin"
"github.com/go-kratos/kratos/pkg/log"
)
func main() {
flag.Parse()
log.Init(nil) // debug flag: log.dir={path}
defer log.Close()
log.Info("photo_taking start")
paladin.Init()
_, closeFunc, err := di.InitApp()
if err != nil {
panic(err)
}
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
for {
s := <-c
log.Info("get a signal %s", s.String())
switch s {
case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
closeFunc()
log.Info("photo_taking exit")
time.Sleep(time.Second)
return
case syscall.SIGHUP:
default:
return
}
}
}
module photo_taking
go 1.13
require (
github.com/go-kratos/kratos v0.5.1-0.20200526160825-521d240568d0
github.com/gogo/protobuf v1.3.1
github.com/golang/protobuf v1.3.5
github.com/google/wire v0.4.0
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e
google.golang.org/genproto v0.0.0-20200402124713-8ff61da6d932
google.golang.org/grpc v1.28.1
)
// Code generated by kratos tool genbts. DO NOT EDIT.
/*
Package dao is a generated cache proxy package.
It is generated from:
type Dao interface {
Close()
Ping(ctx context.Context) (err error)
// bts: -nullcache=&model.Article{ID:-1} -check_null_code=$!=nil&&$.ID==-1
Article(c context.Context, id int64) (*model.Article, error)
}
*/
package dao
import (
"context"
"github.com/go-kratos/kratos/pkg/cache"
"photo_taking/internal/model"
)
// Article get data from cache if miss will call source method, then add to cache.
func (d *dao) Article(c context.Context, id int64) (res *model.Article, err error) {
addCache := true
res, err = d.CacheArticle(c, id)
if err != nil {
addCache = false
err = nil
}
defer func() {
if res != nil && res.ID == -1 {
res = nil
}
}()
if res != nil {
cache.MetricHits.Inc("bts:Article")
return
}
cache.MetricMisses.Inc("bts:Article")
res, err = d.RawArticle(c, id)
if err != nil {
return
}
miss := res
if miss == nil {
miss = &model.Article{ID: -1}
}
if !addCache {
return
}
d.cache.Do(c, func(c context.Context) {
d.AddCacheArticle(c, id, miss)
})
return
}
package dao
import (
"context"
"time"
"photo_taking/internal/model"
"github.com/go-kratos/kratos/pkg/cache/memcache"
"github.com/go-kratos/kratos/pkg/cache/redis"
"github.com/go-kratos/kratos/pkg/conf/paladin"
"github.com/go-kratos/kratos/pkg/database/sql"
"github.com/go-kratos/kratos/pkg/sync/pipeline/fanout"
xtime "github.com/go-kratos/kratos/pkg/time"
"github.com/google/wire"
)
var Provider = wire.NewSet(New, NewDB, NewRedis, NewMC)
//go:generate kratos tool genbts
// Dao dao interface
type Dao interface {
Close()
Ping(ctx context.Context) (err error)
// bts: -nullcache=&model.Article{ID:-1} -check_null_code=$!=nil&&$.ID==-1
Article(c context.Context, id int64) (*model.Article, error)
}
// dao dao.
type dao struct {
db *sql.DB
redis *redis.Redis
mc *memcache.Memcache
cache *fanout.Fanout
demoExpire int32
}
// New new a dao and return.
func New(r *redis.Redis, mc *memcache.Memcache, db *sql.DB) (d Dao, cf func(), err error) {
return newDao(r, mc, db)
}
func newDao(r *redis.Redis, mc *memcache.Memcache, db *sql.DB) (d *dao, cf func(), err error) {
var cfg struct{
DemoExpire xtime.Duration
}
if err = paladin.Get("application.toml").UnmarshalTOML(&cfg); err != nil {
return
}
d = &dao{
db: db,
redis: r,
mc: mc,
cache: fanout.New("cache"),
demoExpire: int32(time.Duration(cfg.DemoExpire) / time.Second),
}
cf = d.Close
return
}
// Close close the resource.
func (d *dao) Close() {
d.cache.Close()
}
// Ping ping the resource.
func (d *dao) Ping(ctx context.Context) (err error) {
return nil
}
package dao
import (
"context"
"flag"
"os"
"testing"
"github.com/go-kratos/kratos/pkg/conf/paladin"
"github.com/go-kratos/kratos/pkg/testing/lich"
)
var d *dao
var ctx = context.Background()
func TestMain(m *testing.M) {
flag.Set("conf", "../../test")
flag.Set("f", "../../test/docker-compose.yaml")
flag.Parse()
disableLich := os.Getenv("DISABLE_LICH") != ""
if !disableLich {
if err := lich.Setup(); err != nil {
panic(err)
}
}
var err error
if err = paladin.Init(); err != nil {
panic(err)
}
var cf func()
if d, cf, err = newTestDao();err != nil {
panic(err)
}
ret := m.Run()
cf()
if !disableLich {
_ = lich.Teardown()
}
os.Exit(ret)
}
package dao
import (
"context"
"photo_taking/internal/model"
"github.com/go-kratos/kratos/pkg/conf/paladin"
"github.com/go-kratos/kratos/pkg/database/sql"
)
func NewDB() (db *sql.DB, cf func(), err error) {
var (
cfg sql.Config
ct paladin.TOML
)
if err = paladin.Get("db.toml").Unmarshal(&ct); err != nil {
return
}
if err = ct.Get("Client").UnmarshalTOML(&cfg); err != nil {
return
}
db = sql.NewMySQL(&cfg)
cf = func() {db.Close()}
return
}
func (d *dao) RawArticle(ctx context.Context, id int64) (art *model.Article, err error) {
// get data from db
return
}
// Code generated by kratos tool genmc. DO NOT EDIT.
/*
Package dao is a generated mc cache package.
It is generated from:
type _mc interface {
// mc: -key=keyArt -type=get
CacheArticle(c context.Context, id int64) (*model.Article, error)
// mc: -key=keyArt -expire=d.demoExpire
AddCacheArticle(c context.Context, id int64, art *model.Article) (err error)
// mc: -key=keyArt
DeleteArticleCache(c context.Context, id int64) (err error)
}
*/
package dao
import (
"context"
"fmt"
"github.com/go-kratos/kratos/pkg/cache/memcache"
"github.com/go-kratos/kratos/pkg/log"
"photo_taking/internal/model"
)
var _ _mc
// CacheArticle get data from mc
func (d *dao) CacheArticle(c context.Context, id int64) (res *model.Article, err error) {
key := keyArt(id)
res = &model.Article{}
if err = d.mc.Get(c, key).Scan(res); err != nil {
res = nil
if err == memcache.ErrNotFound {
err = nil
}
}
if err != nil {
log.Errorv(c, log.KV("CacheArticle", fmt.Sprintf("%+v", err)), log.KV("key", key))
return
}
return
}
// AddCacheArticle Set data to mc
func (d *dao) AddCacheArticle(c context.Context, id int64, val *model.Article) (err error) {
if val == nil {
return
}
key := keyArt(id)
item := &memcache.Item{Key: key, Object: val, Expiration: d.demoExpire, Flags: memcache.FlagJSON}
if err = d.mc.Set(c, item); err != nil {
log.Errorv(c, log.KV("AddCacheArticle", fmt.Sprintf("%+v", err)), log.KV("key", key))
return
}
return
}
// DeleteArticleCache delete data from mc
func (d *dao) DeleteArticleCache(c context.Context, id int64) (err error) {
key := keyArt(id)
if err = d.mc.Delete(c, key); err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
log.Errorv(c, log.KV("DeleteArticleCache", fmt.Sprintf("%+v", err)), log.KV("key", key))
return
}
return
}
package dao
import (
"context"
"fmt"
"photo_taking/internal/model"
"github.com/go-kratos/kratos/pkg/cache/memcache"
"github.com/go-kratos/kratos/pkg/conf/paladin"
"github.com/go-kratos/kratos/pkg/log"
)
//go:generate kratos tool genmc
type _mc interface {
// mc: -key=keyArt -type=get
CacheArticle(c context.Context, id int64) (*model.Article, error)
// mc: -key=keyArt -expire=d.demoExpire
AddCacheArticle(c context.Context, id int64, art *model.Article) (err error)
// mc: -key=keyArt
DeleteArticleCache(c context.Context, id int64) (err error)
}
func NewMC() (mc *memcache.Memcache, cf func(), err error) {
var (
cfg memcache.Config
ct paladin.TOML
)
if err = paladin.Get("memcache.toml").Unmarshal(&ct); err != nil {
return
}
if err = ct.Get("Client").UnmarshalTOML(&cfg); err != nil {
return
}
mc = memcache.New(&cfg)
cf = func() {mc.Close()}
return
}
func (d *dao) PingMC(ctx context.Context) (err error) {
if err = d.mc.Set(ctx, &memcache.Item{Key: "ping", Value: []byte("pong"), Expiration: 0}); err != nil {
log.Error("conn.Set(PING) error(%v)", err)
}
return
}
func keyArt(id int64) string {
return fmt.Sprintf("art_%d", id)
}
package dao
import (
"context"
"github.com/go-kratos/kratos/pkg/cache/redis"
"github.com/go-kratos/kratos/pkg/conf/paladin"
"github.com/go-kratos/kratos/pkg/log"
)
func NewRedis() (r *redis.Redis, cf func(), err error) {
var (
cfg redis.Config
ct paladin.Map
)
if err = paladin.Get("redis.toml").Unmarshal(&ct); err != nil {
return
}
if err = ct.Get("Client").UnmarshalTOML(&cfg); err != nil {
return
}
r = redis.NewRedis(&cfg)
cf = func(){r.Close()}
return
}
func (d *dao) PingRedis(ctx context.Context) (err error) {
if _, err = d.redis.Do(ctx, "SET", "ping", "pong"); err != nil {
log.Error("conn.Set(PING) error(%v)", err)
}
return
}
\ No newline at end of file
// +build wireinject
// The build tag makes sure the stub is not built in the final build.
package dao
import (
"github.com/google/wire"
)
//go:generate kratos tool wire
func newTestDao() (*dao, func(), error) {
panic(wire.Build(newDao, NewDB, NewRedis, NewMC))
}
// Code generated by Wire. DO NOT EDIT.
//go:generate wire
//+build !wireinject
package dao
// Injectors from wire.go:
func newTestDao() (*dao, func(), error) {
redis, cleanup, err := NewRedis()
if err != nil {
return nil, nil, err
}
memcache, cleanup2, err := NewMC()
if err != nil {
cleanup()
return nil, nil, err
}
db, cleanup3, err := NewDB()
if err != nil {
cleanup2()
cleanup()
return nil, nil, err
}
daoDao, cleanup4, err := newDao(redis, memcache, db)
if err != nil {
cleanup3()
cleanup2()
cleanup()
return nil, nil, err
}
return daoDao, func() {
cleanup4()
cleanup3()
cleanup2()
cleanup()
}, nil
}
package di
import (
"context"
"time"
"photo_taking/internal/service"
"github.com/go-kratos/kratos/pkg/log"
bm "github.com/go-kratos/kratos/pkg/net/http/blademaster"
"github.com/go-kratos/kratos/pkg/net/rpc/warden"
)
//go:generate kratos tool wire
type App struct {
svc *service.Service
http *bm.Engine
grpc *warden.Server
}
func NewApp(svc *service.Service, h *bm.Engine, g *warden.Server) (app *App, closeFunc func(), err error){
app = &App{
svc: svc,
http: h,
grpc: g,
}
closeFunc = func() {
ctx, cancel := context.WithTimeout(context.Background(), 35*time.Second)
if err := g.Shutdown(ctx); err != nil {
log.Error("grpcSrv.Shutdown error(%v)", err)
}
if err := h.Shutdown(ctx); err != nil {
log.Error("httpSrv.Shutdown error(%v)", err)
}
cancel()
}
return
}
// +build wireinject
// The build tag makes sure the stub is not built in the final build.
package di
import (
"photo_taking/internal/dao"
"photo_taking/internal/service"
"photo_taking/internal/server/grpc"
"photo_taking/internal/server/http"
"github.com/google/wire"
)
//go:generate kratos t wire
func InitApp() (*App, func(), error) {
panic(wire.Build(dao.Provider, service.Provider, http.New, grpc.New, NewApp))
}
// Code generated by Wire. DO NOT EDIT.
//go:generate wire
//+build !wireinject
package di
import (
"photo_taking/internal/dao"
"photo_taking/internal/server/grpc"
"photo_taking/internal/server/http"
"photo_taking/internal/service"
)
// Injectors from wire.go:
func InitApp() (*App, func(), error) {
redis, cleanup, err := dao.NewRedis()
if err != nil {
return nil, nil, err
}
memcache, cleanup2, err := dao.NewMC()
if err != nil {
cleanup()
return nil, nil, err
}
db, cleanup3, err := dao.NewDB()
if err != nil {
cleanup2()
cleanup()
return nil, nil, err
}
daoDao, cleanup4, err := dao.New(redis, memcache, db)
if err != nil {
cleanup3()
cleanup2()
cleanup()
return nil, nil, err
}
serviceService, cleanup5, err := service.New(daoDao)
if err != nil {
cleanup4()
cleanup3()
cleanup2()
cleanup()
return nil, nil, err
}
engine, err := http.New(serviceService)
if err != nil {
cleanup5()
cleanup4()
cleanup3()
cleanup2()
cleanup()
return nil, nil, err
}
server, err := grpc.New(serviceService)
if err != nil {
cleanup5()
cleanup4()
cleanup3()
cleanup2()
cleanup()
return nil, nil, err
}
app, cleanup6, err := NewApp(serviceService, engine, server)
if err != nil {
cleanup5()
cleanup4()
cleanup3()
cleanup2()
cleanup()
return nil, nil, err
}
return app, func() {
cleanup6()
cleanup5()
cleanup4()
cleanup3()
cleanup2()
cleanup()
}, nil
}
package model
// Kratos hello kratos.
type Kratos struct {
Hello string
}
type Article struct {
ID int64
Content string
Author string
}
\ No newline at end of file
package grpc
import (
pb "photo_taking/api"
"github.com/go-kratos/kratos/pkg/conf/paladin"
"github.com/go-kratos/kratos/pkg/net/rpc/warden"
)
// New new a grpc server.
func New(svc pb.DemoServer) (ws *warden.Server, err error) {
var (
cfg warden.ServerConfig
ct paladin.TOML
)
if err = paladin.Get("grpc.toml").Unmarshal(&ct); err != nil {
return
}
if err = ct.Get("Server").UnmarshalTOML(&cfg); err != nil {
return
}
ws = warden.NewServer(&cfg)
pb.RegisterDemoServer(ws.Server(), svc)
ws, err = ws.Start()
return
}
package http
import (
"net/http"
pb "photo_taking/api"
"photo_taking/internal/model"
"github.com/go-kratos/kratos/pkg/conf/paladin"
"github.com/go-kratos/kratos/pkg/log"
bm "github.com/go-kratos/kratos/pkg/net/http/blademaster"
)
var svc pb.DemoServer
// New new a bm server.
func New(s pb.DemoServer) (engine *bm.Engine, err error) {
var (
cfg bm.ServerConfig
ct paladin.TOML
)
if err = paladin.Get("http.toml").Unmarshal(&ct); err != nil {
return
}
if err = ct.Get("Server").UnmarshalTOML(&cfg); err != nil {
return
}
svc = s
engine = bm.DefaultServer(&cfg)
pb.RegisterDemoBMServer(engine, s)
initRouter(engine)
err = engine.Start()
return
}
func initRouter(e *bm.Engine) {
e.Ping(ping)
g := e.Group("/photo_taking")
{
g.GET("/start", howToStart)
}
}
func ping(ctx *bm.Context) {
if _, err := svc.Ping(ctx, nil); err != nil {
log.Error("ping error(%v)", err)
ctx.AbortWithStatus(http.StatusServiceUnavailable)
}
}
// example for http request handler.
func howToStart(c *bm.Context) {
k := &model.Kratos{
Hello: "Golang 大法好 !!!",
}
c.JSON(k, nil)
}
\ No newline at end of file
package service
import (
"context"
"fmt"
pb "photo_taking/api"
"photo_taking/internal/dao"
"github.com/go-kratos/kratos/pkg/conf/paladin"
"github.com/golang/protobuf/ptypes/empty"
"github.com/google/wire"
)
var Provider = wire.NewSet(New, wire.Bind(new(pb.DemoServer), new(*Service)))
// Service service.
type Service struct {
ac *paladin.Map
dao dao.Dao
}
// New new a service and return.
func New(d dao.Dao) (s *Service, cf func(), err error) {
s = &Service{
ac: &paladin.TOML{},
dao: d,
}
cf = s.Close
err = paladin.Watch("application.toml", s.ac)
return
}
// SayHello grpc demo func.
func (s *Service) SayHello(ctx context.Context, req *pb.HelloReq) (reply *empty.Empty, err error) {
reply = new(empty.Empty)
fmt.Printf("hello %s", req.Name)
return
}
// SayHelloURL bm demo func.
func (s *Service) SayHelloURL(ctx context.Context, req *pb.HelloReq) (reply *pb.HelloResp, err error) {
reply = &pb.HelloResp{
Content: "hello " + req.Name,
}
fmt.Printf("hello url %s", req.Name)
return
}
// Ping ping the resource.
func (s *Service) Ping(ctx context.Context, e *empty.Empty) (*empty.Empty, error) {
return &empty.Empty{}, s.dao.Ping(ctx)
}
// Close close the resource.
func (s *Service) Close() {
}
create database kratos_demo;
use kratos_demo;
CREATE TABLE `articles` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`title` varchar(64) NOT NULL COMMENT '名称',
`mtime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最后修改时间',
`ctime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
PRIMARY KEY (`id`),
KEY `ix_mtime` (`mtime`)
) COMMENT='文章表';
use kratos_demo;
INSERT INTO articles(`id`, `title`) VALUES (1, 'title');
# This is a TOML document. Boom~
demoExpire = "24h"
[Client]
dsn = "root:root@tcp(127.0.0.1:13306)/kratos_demo?timeout=1s&readTimeout=1s&writeTimeout=1s&parseTime=true&loc=Local&charset=utf8mb4,utf8"
active = 20
idle = 10
idleTimeout ="4h"
queryTimeout = "200ms"
execTimeout = "300ms"
tranTimeout = "400ms"
version: "3.7"
services:
db:
image: mysql:5.6
ports:
- 13306:3306
environment:
- MYSQL_ROOT_PASSWORD=root
- TZ=Asia/Shanghai
volumes:
- .:/docker-entrypoint-initdb.d
command: [
'--character-set-server=utf8',
'--collation-server=utf8_unicode_ci'
]
healthcheck:
test: ["CMD", "mysqladmin" ,"ping", "--protocol=tcp"]
timeout: 20s
interval: 1s
retries: 20
redis:
image: redis
ports:
- 16379:6379
healthcheck:
test: ["CMD", "redis-cli","ping"]
interval: 20s
timeout: 1s
retries: 20
memcached:
image: memcached
ports:
- 21211:11211
healthcheck:
test: ["CMD", "echo", "stats", "|", "nc", "127.0.0.1", "11211"]
interval: 20s
timeout: 1s
retries: 20
[Server]
addr = "0.0.0.0:9000"
timeout = "1s"
[Server]
addr = "0.0.0.0:8000"
timeout = "1s"
[Client]
name = "demo"
proto = "tcp"
addr = "127.0.0.1:21211"
active = 50
idle = 10
dialTimeout = "100ms"
readTimeout = "200ms"
writeTimeout = "300ms"
idleTimeout = "80s"
[Client]
name = "demo"
proto = "tcp"
addr = "127.0.0.1:16379"
idle = 10
active = 10
dialTimeout = "1s"
readTimeout = "1s"
writeTimeout = "1s"
idleTimeout = "10s"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment