update after protoc-gen-micro changes
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
c5fb53c0f1
commit
267b29b658
@ -19,6 +19,7 @@ import (
|
||||
protocodec "github.com/unistack-org/micro-codec-proto/v3"
|
||||
regRouter "github.com/unistack-org/micro-router-register/v3"
|
||||
gsrv "github.com/unistack-org/micro-server-grpc/v3"
|
||||
gpb "github.com/unistack-org/micro-tests/server/grpc/gproto"
|
||||
pb "github.com/unistack-org/micro-tests/server/grpc/proto"
|
||||
"github.com/unistack-org/micro/v3/api"
|
||||
"github.com/unistack-org/micro/v3/api/handler"
|
||||
@ -87,7 +88,7 @@ func initial(t *testing.T) (server.Server, client.Client) {
|
||||
)
|
||||
|
||||
h := &testServer{}
|
||||
pb.RegisterTestHandler(s, h)
|
||||
gpb.RegisterTestServer(s, h)
|
||||
|
||||
if err := s.Init(); err != nil {
|
||||
t.Fatalf("failed to init: %v", err)
|
||||
|
@ -1,3 +1,3 @@
|
||||
package grpc
|
||||
|
||||
//go:generate protoc -I./proto -I. --go-grpc_out=paths=source_relative:./proto --go_out=paths=source_relative:./proto --micro_out=paths=source_relative:./proto proto/test.proto
|
||||
//go:generate protoc -I./proto -I. --go-grpc_out=paths=source_relative:./proto --go_out=paths=source_relative:./proto --micro_out=standalone=true,paths=source_relative:./gproto proto/test.proto
|
||||
|
42
client/grpc/gproto/test_micro.pb.go
Normal file
42
client/grpc/gproto/test_micro.pb.go
Normal file
@ -0,0 +1,42 @@
|
||||
// Code generated by protoc-gen-micro
|
||||
// source: test.proto
|
||||
package helloworld
|
||||
|
||||
import (
|
||||
context "context"
|
||||
proto "github.com/unistack-org/micro-tests/client/grpc/proto"
|
||||
api "github.com/unistack-org/micro/v3/api"
|
||||
client "github.com/unistack-org/micro/v3/client"
|
||||
)
|
||||
|
||||
func NewTestEndpoints() []*api.Endpoint {
|
||||
return []*api.Endpoint{}
|
||||
}
|
||||
|
||||
type TestClient interface {
|
||||
Call(ctx context.Context, req *proto.Request, opts ...client.CallOption) (*proto.Response, error)
|
||||
Stream(ctx context.Context, opts ...client.CallOption) (Test_StreamClient, error)
|
||||
}
|
||||
|
||||
type Test_StreamClient interface {
|
||||
Context() context.Context
|
||||
SendMsg(msg interface{}) error
|
||||
RecvMsg(msg interface{}) error
|
||||
Close() error
|
||||
Send(msg *proto.Request) error
|
||||
Recv() (*proto.Response, error)
|
||||
}
|
||||
|
||||
type TestServer interface {
|
||||
Call(ctx context.Context, req *proto.Request, rsp *proto.Response) error
|
||||
Stream(ctx context.Context, stream Test_StreamStream) error
|
||||
}
|
||||
|
||||
type Test_StreamStream interface {
|
||||
Context() context.Context
|
||||
SendMsg(msg interface{}) error
|
||||
RecvMsg(msg interface{}) error
|
||||
Close() error
|
||||
Recv() (*proto.Request, error)
|
||||
Send(msg *proto.Response) error
|
||||
}
|
128
client/grpc/gproto/test_micro_rpc.pb.go
Normal file
128
client/grpc/gproto/test_micro_rpc.pb.go
Normal file
@ -0,0 +1,128 @@
|
||||
// Code generated by protoc-gen-micro
|
||||
// source: test.proto
|
||||
package helloworld
|
||||
|
||||
import (
|
||||
context "context"
|
||||
proto "github.com/unistack-org/micro-tests/client/grpc/proto"
|
||||
api "github.com/unistack-org/micro/v3/api"
|
||||
client "github.com/unistack-org/micro/v3/client"
|
||||
server "github.com/unistack-org/micro/v3/server"
|
||||
)
|
||||
|
||||
type testClient struct {
|
||||
c client.Client
|
||||
name string
|
||||
}
|
||||
|
||||
func NewTestClient(name string, c client.Client) TestClient {
|
||||
return &testClient{c: c, name: name}
|
||||
}
|
||||
|
||||
func (c *testClient) Call(ctx context.Context, req *proto.Request, opts ...client.CallOption) (*proto.Response, error) {
|
||||
rsp := &proto.Response{}
|
||||
err := c.c.Call(ctx, c.c.NewRequest(c.name, "Test.Call", req), rsp, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rsp, nil
|
||||
}
|
||||
|
||||
func (c *testClient) Stream(ctx context.Context, opts ...client.CallOption) (Test_StreamClient, error) {
|
||||
stream, err := c.c.Stream(ctx, c.c.NewRequest(c.name, "Test.Stream", &proto.Request{}), opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &testClientStream{stream}, nil
|
||||
}
|
||||
|
||||
type testClientStream struct {
|
||||
stream client.Stream
|
||||
}
|
||||
|
||||
func (s *testClientStream) Close() error {
|
||||
return s.stream.Close()
|
||||
}
|
||||
|
||||
func (s *testClientStream) Context() context.Context {
|
||||
return s.stream.Context()
|
||||
}
|
||||
|
||||
func (s *testClientStream) SendMsg(msg interface{}) error {
|
||||
return s.stream.Send(msg)
|
||||
}
|
||||
|
||||
func (s *testClientStream) RecvMsg(msg interface{}) error {
|
||||
return s.stream.Recv(msg)
|
||||
}
|
||||
|
||||
func (s *testClientStream) Send(msg *proto.Request) error {
|
||||
return s.stream.Send(msg)
|
||||
}
|
||||
|
||||
func (s *testClientStream) Recv() (*proto.Response, error) {
|
||||
msg := &proto.Response{}
|
||||
if err := s.stream.Recv(msg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return msg, nil
|
||||
}
|
||||
|
||||
type testServer struct {
|
||||
TestServer
|
||||
}
|
||||
|
||||
func (h *testServer) Call(ctx context.Context, req *proto.Request, rsp *proto.Response) error {
|
||||
return h.TestServer.Call(ctx, req, rsp)
|
||||
}
|
||||
|
||||
func (h *testServer) Stream(ctx context.Context, stream server.Stream) error {
|
||||
return h.TestServer.Stream(ctx, &testStreamStream{stream})
|
||||
}
|
||||
|
||||
type testStreamStream struct {
|
||||
stream server.Stream
|
||||
}
|
||||
|
||||
func (s *testStreamStream) Close() error {
|
||||
return s.stream.Close()
|
||||
}
|
||||
|
||||
func (s *testStreamStream) Context() context.Context {
|
||||
return s.stream.Context()
|
||||
}
|
||||
|
||||
func (s *testStreamStream) SendMsg(msg interface{}) error {
|
||||
return s.stream.Send(msg)
|
||||
}
|
||||
|
||||
func (s *testStreamStream) RecvMsg(msg interface{}) error {
|
||||
return s.stream.Recv(msg)
|
||||
}
|
||||
|
||||
func (s *testStreamStream) Send(msg *proto.Response) error {
|
||||
return s.stream.Send(msg)
|
||||
}
|
||||
|
||||
func (s *testStreamStream) Recv() (*proto.Request, error) {
|
||||
msg := &proto.Request{}
|
||||
if err := s.stream.Recv(msg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return msg, nil
|
||||
}
|
||||
|
||||
func RegisterTestServer(s server.Server, sh TestServer, opts ...server.HandlerOption) error {
|
||||
type test interface {
|
||||
Call(ctx context.Context, req *proto.Request, rsp *proto.Response) error
|
||||
Stream(ctx context.Context, stream server.Stream) error
|
||||
}
|
||||
type Test struct {
|
||||
test
|
||||
}
|
||||
h := &testServer{sh}
|
||||
for _, endpoint := range NewTestEndpoints() {
|
||||
opts = append(opts, api.WithEndpoint(endpoint))
|
||||
}
|
||||
return s.Handle(s.NewHandler(&Test{h}, opts...))
|
||||
}
|
@ -144,8 +144,11 @@ var file_test_proto_rawDesc = []byte{
|
||||
0x12, 0x13, 0x2e, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2e, 0x52, 0x65,
|
||||
0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x14, 0x2e, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77, 0x6f, 0x72,
|
||||
0x6c, 0x64, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x28, 0x01, 0x30,
|
||||
0x01, 0x42, 0x0e, 0x5a, 0x0c, 0x2e, 0x3b, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77, 0x6f, 0x72, 0x6c,
|
||||
0x64, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
0x01, 0x42, 0x42, 0x5a, 0x40, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f,
|
||||
0x75, 0x6e, 0x69, 0x73, 0x74, 0x61, 0x63, 0x6b, 0x2d, 0x6f, 0x72, 0x67, 0x2f, 0x6d, 0x69, 0x63,
|
||||
0x72, 0x6f, 0x2d, 0x74, 0x65, 0x73, 0x74, 0x73, 0x2f, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x2f,
|
||||
0x67, 0x72, 0x70, 0x63, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x3b, 0x68, 0x65, 0x6c, 0x6c, 0x6f,
|
||||
0x77, 0x6f, 0x72, 0x6c, 0x64, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
}
|
||||
|
||||
var (
|
||||
|
@ -1,7 +1,7 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package helloworld;
|
||||
option go_package = ".;helloworld";
|
||||
option go_package = "github.com/unistack-org/micro-tests/client/grpc/proto;helloworld";
|
||||
|
||||
service Test {
|
||||
rpc Call(Request) returns (Response) {};
|
||||
|
@ -1,3 +1,3 @@
|
||||
package http
|
||||
|
||||
//go:generate protoc -I./proto -I. -I/home/vtolstov/.cache/go-path/pkg/mod/github.com/grpc-ecosystem/grpc-gateway/v2@v2.1.0 -I/home/vtolstov/.cache/go-path/pkg/mod/github.com/grpc-ecosystem/grpc-gateway/v2@v2.1.0/third_party/googleapis --go-grpc_out=paths=source_relative:./proto --go_out=paths=source_relative:./proto --micro_out=components=micro|http,debug=true,paths=source_relative:./proto proto/github.proto
|
||||
//go:generate protoc -I./proto -I. -I/home/vtolstov/.cache/go-path/pkg/mod/github.com/grpc-ecosystem/grpc-gateway/v2@v2.1.0 -I/home/vtolstov/.cache/go-path/pkg/mod/github.com/grpc-ecosystem/grpc-gateway/v2@v2.1.0/third_party/googleapis --go_out=paths=source_relative:./proto --micro_out=components=micro|http,debug=true,paths=source_relative:./proto proto/github.proto
|
||||
|
@ -62,7 +62,7 @@ func TestError(t *testing.T) {
|
||||
|
||||
func TestNative(t *testing.T) {
|
||||
c := client.NewClientCallOptions(mhttp.NewClient(client.ContentType("application/json"), client.Codec("application/json", jsoncodec.NewCodec())), client.WithAddress("https://api.github.com"))
|
||||
gh := pb.NewGithubService("github", c)
|
||||
gh := pb.NewGithubClient("github", c)
|
||||
|
||||
rsp, err := gh.LookupUser(context.TODO(), &pb.LookupUserReq{Username: "vtolstov"})
|
||||
if err != nil {
|
||||
|
@ -1,90 +0,0 @@
|
||||
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
|
||||
|
||||
package pb
|
||||
|
||||
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.
|
||||
const _ = grpc.SupportPackageIsVersion6
|
||||
|
||||
// GithubClient is the client API for Github 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 GithubClient interface {
|
||||
LookupUser(ctx context.Context, in *LookupUserReq, opts ...grpc.CallOption) (*LookupUserRsp, error)
|
||||
}
|
||||
|
||||
type githubClient struct {
|
||||
cc grpc.ClientConnInterface
|
||||
}
|
||||
|
||||
func NewGithubClient(cc grpc.ClientConnInterface) GithubClient {
|
||||
return &githubClient{cc}
|
||||
}
|
||||
|
||||
func (c *githubClient) LookupUser(ctx context.Context, in *LookupUserReq, opts ...grpc.CallOption) (*LookupUserRsp, error) {
|
||||
out := new(LookupUserRsp)
|
||||
err := c.cc.Invoke(ctx, "/github.Github/LookupUser", in, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// GithubServer is the server API for Github service.
|
||||
// All implementations must embed UnimplementedGithubServer
|
||||
// for forward compatibility
|
||||
type GithubServer interface {
|
||||
LookupUser(context.Context, *LookupUserReq) (*LookupUserRsp, error)
|
||||
mustEmbedUnimplementedGithubServer()
|
||||
}
|
||||
|
||||
// UnimplementedGithubServer must be embedded to have forward compatible implementations.
|
||||
type UnimplementedGithubServer struct {
|
||||
}
|
||||
|
||||
func (*UnimplementedGithubServer) LookupUser(context.Context, *LookupUserReq) (*LookupUserRsp, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method LookupUser not implemented")
|
||||
}
|
||||
func (*UnimplementedGithubServer) mustEmbedUnimplementedGithubServer() {}
|
||||
|
||||
func RegisterGithubServer(s *grpc.Server, srv GithubServer) {
|
||||
s.RegisterService(&_Github_serviceDesc, srv)
|
||||
}
|
||||
|
||||
func _Github_LookupUser_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(LookupUserReq)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(GithubServer).LookupUser(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/github.Github/LookupUser",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(GithubServer).LookupUser(ctx, req.(*LookupUserReq))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
var _Github_serviceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "github.Github",
|
||||
HandlerType: (*GithubServer)(nil),
|
||||
Methods: []grpc.MethodDesc{
|
||||
{
|
||||
MethodName: "LookupUser",
|
||||
Handler: _Github_LookupUser_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{},
|
||||
Metadata: "github.proto",
|
||||
}
|
@ -3,49 +3,26 @@
|
||||
package pb
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
micro_api "github.com/unistack-org/micro/v3/api"
|
||||
micro_client "github.com/unistack-org/micro/v3/client"
|
||||
micro_server "github.com/unistack-org/micro/v3/server"
|
||||
context "context"
|
||||
api "github.com/unistack-org/micro/v3/api"
|
||||
client "github.com/unistack-org/micro/v3/client"
|
||||
)
|
||||
|
||||
// NewGithubEndpoints provides api endpoints metdata for Github service
|
||||
func NewGithubEndpoints() []*micro_api.Endpoint {
|
||||
var endpoints []*micro_api.Endpoint
|
||||
endpoint := µ_api.Endpoint{
|
||||
Name: "Github.LookupUser",
|
||||
Path: []string{"/users/{username}"},
|
||||
Method: []string{"GET"},
|
||||
Handler: "rpc",
|
||||
func NewGithubEndpoints() []*api.Endpoint {
|
||||
return []*api.Endpoint{
|
||||
&api.Endpoint{
|
||||
Name: "Github.LookupUser",
|
||||
Path: []string{"/users/{username}"},
|
||||
Method: []string{"GET"},
|
||||
Handler: "rpc",
|
||||
},
|
||||
}
|
||||
endpoints = append(endpoints, endpoint)
|
||||
return endpoints
|
||||
}
|
||||
|
||||
// GithubService interface
|
||||
type GithubService interface {
|
||||
LookupUser(context.Context, *LookupUserReq, ...micro_client.CallOption) (*LookupUserRsp, error)
|
||||
type GithubClient interface {
|
||||
LookupUser(ctx context.Context, req *LookupUserReq, opts ...client.CallOption) (*LookupUserRsp, error)
|
||||
}
|
||||
|
||||
// Micro server stuff
|
||||
|
||||
// GithubHandler server handler
|
||||
type GithubHandler interface {
|
||||
LookupUser(context.Context, *LookupUserReq, *LookupUserRsp) error
|
||||
}
|
||||
|
||||
// RegisterGithubHandler registers server handler
|
||||
func RegisterGithubHandler(s micro_server.Server, sh GithubHandler, opts ...micro_server.HandlerOption) error {
|
||||
type github interface {
|
||||
LookupUser(context.Context, *LookupUserReq, *LookupUserRsp) error
|
||||
}
|
||||
type Github struct {
|
||||
github
|
||||
}
|
||||
h := &githubHandler{sh}
|
||||
for _, endpoint := range NewGithubEndpoints() {
|
||||
opts = append(opts, micro_api.WithEndpoint(endpoint))
|
||||
}
|
||||
return s.Handle(s.NewHandler(&Github{h}, opts...))
|
||||
type GithubServer interface {
|
||||
LookupUser(ctx context.Context, req *LookupUserReq, rsp *LookupUserRsp) error
|
||||
}
|
||||
|
@ -3,58 +3,57 @@
|
||||
package pb
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
micro_client_http "github.com/unistack-org/micro-client-http/v3"
|
||||
micro_client "github.com/unistack-org/micro/v3/client"
|
||||
micro_server "github.com/unistack-org/micro/v3/server"
|
||||
context "context"
|
||||
v3 "github.com/unistack-org/micro-client-http/v3"
|
||||
api "github.com/unistack-org/micro/v3/api"
|
||||
client "github.com/unistack-org/micro/v3/client"
|
||||
server "github.com/unistack-org/micro/v3/server"
|
||||
)
|
||||
|
||||
var (
|
||||
_ micro_server.Option
|
||||
_ micro_client.Option
|
||||
)
|
||||
|
||||
type githubService struct {
|
||||
c micro_client.Client
|
||||
type githubClient struct {
|
||||
c client.Client
|
||||
name string
|
||||
}
|
||||
|
||||
// Micro client stuff
|
||||
|
||||
// NewGithubService create new service client
|
||||
func NewGithubService(name string, c micro_client.Client) GithubService {
|
||||
return &githubService{c: c, name: name}
|
||||
func NewGithubClient(name string, c client.Client) GithubClient {
|
||||
return &githubClient{c: c, name: name}
|
||||
}
|
||||
|
||||
func (c *githubService) LookupUser(ctx context.Context, req *LookupUserReq, opts ...micro_client.CallOption) (*LookupUserRsp, error) {
|
||||
func (c *githubClient) LookupUser(ctx context.Context, req *LookupUserReq, opts ...client.CallOption) (*LookupUserRsp, error) {
|
||||
errmap := make(map[string]interface{}, 1)
|
||||
errmap["default"] = &Error{}
|
||||
nopts := append(opts,
|
||||
micro_client_http.Method("GET"),
|
||||
micro_client_http.Path("/users/{username}"),
|
||||
micro_client_http.ErrorMap(errmap),
|
||||
opts = append(opts,
|
||||
v3.ErrorMap(errmap),
|
||||
v3.Method("GET"),
|
||||
v3.Path("/users/{username}"),
|
||||
v3.Body(""),
|
||||
)
|
||||
rsp := &LookupUserRsp{}
|
||||
err := c.c.Call(ctx, c.c.NewRequest(c.name, "Github.LookupUser", req), rsp, nopts...)
|
||||
err := c.c.Call(ctx, c.c.NewRequest(c.name, "Github.LookupUser", req), rsp, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rsp, nil
|
||||
}
|
||||
|
||||
// Error method to satisfy error interface
|
||||
func (e *Error) Error() string {
|
||||
return fmt.Sprintf("%#v", e)
|
||||
type githubServer struct {
|
||||
GithubServer
|
||||
}
|
||||
|
||||
// Micro server stuff
|
||||
|
||||
type githubHandler struct {
|
||||
GithubHandler
|
||||
func (h *githubServer) LookupUser(ctx context.Context, req *LookupUserReq, rsp *LookupUserRsp) error {
|
||||
return h.GithubServer.LookupUser(ctx, req, rsp)
|
||||
}
|
||||
|
||||
func (h *githubHandler) LookupUser(ctx context.Context, req *LookupUserReq, rsp *LookupUserRsp) error {
|
||||
return h.GithubHandler.LookupUser(ctx, req, rsp)
|
||||
func RegisterGithubServer(s server.Server, sh GithubServer, opts ...server.HandlerOption) error {
|
||||
type github interface {
|
||||
LookupUser(ctx context.Context, req *LookupUserReq, rsp *LookupUserRsp) error
|
||||
}
|
||||
type Github struct {
|
||||
github
|
||||
}
|
||||
h := &githubServer{sh}
|
||||
for _, endpoint := range NewGithubEndpoints() {
|
||||
opts = append(opts, api.WithEndpoint(endpoint))
|
||||
}
|
||||
return s.Handle(s.NewHandler(&Github{h}, opts...))
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ import (
|
||||
protocodec "github.com/unistack-org/micro-codec-segmentio/v3/proto"
|
||||
regRouter "github.com/unistack-org/micro-router-register/v3"
|
||||
gserver "github.com/unistack-org/micro-server-grpc/v3"
|
||||
gpb "github.com/unistack-org/micro-tests/server/grpc/gproto"
|
||||
pb "github.com/unistack-org/micro-tests/server/grpc/proto"
|
||||
"github.com/unistack-org/micro/v3/broker"
|
||||
"github.com/unistack-org/micro/v3/client"
|
||||
@ -40,7 +41,7 @@ func TestGRPCServer(t *testing.T) {
|
||||
rtr := regRouter.NewRouter(router.Register(r))
|
||||
|
||||
h := &testServer{}
|
||||
err = pb.RegisterTestHandler(s, h)
|
||||
err = gpb.RegisterTestServer(s, h)
|
||||
if err != nil {
|
||||
t.Fatalf("can't register handler: %v", err)
|
||||
}
|
||||
|
2
go.mod
2
go.mod
@ -26,7 +26,7 @@ require (
|
||||
github.com/unistack-org/micro-metrics-prometheus/v3 v3.1.1
|
||||
github.com/unistack-org/micro-router-register/v3 v3.2.2
|
||||
github.com/unistack-org/micro-server-grpc/v3 v3.2.3
|
||||
github.com/unistack-org/micro-server-http/v3 v3.2.5
|
||||
github.com/unistack-org/micro-server-http/v3 v3.2.6
|
||||
github.com/unistack-org/micro-server-tcp/v3 v3.2.2
|
||||
github.com/unistack-org/micro-wrapper-trace-opentracing/v3 v3.2.0
|
||||
github.com/unistack-org/micro/v3 v3.2.15-0.20210221215218-09973af099cd
|
||||
|
8
go.sum
8
go.sum
@ -523,8 +523,8 @@ github.com/unistack-org/micro-router-register/v3 v3.2.2 h1:lYCymDHkJfhZWYQ4+Sb7F
|
||||
github.com/unistack-org/micro-router-register/v3 v3.2.2/go.mod h1:Y9Qtlg4NHqq5rR6X6Jm+04LoSJMi7/OOCm2mRueZYTE=
|
||||
github.com/unistack-org/micro-server-grpc/v3 v3.2.3 h1:Q6zBB4ipgI6yptC0b7jyXULuRQ4s7sLfSmEbUWYOBUQ=
|
||||
github.com/unistack-org/micro-server-grpc/v3 v3.2.3/go.mod h1:rSb800jZW5x4sKV1mX6HdYtp7fXSrnCdqCJ6C5+Fb/Y=
|
||||
github.com/unistack-org/micro-server-http/v3 v3.2.5 h1:NFuddx39k8DP3nwV1rlRHQIS2/F+KBhG7KUWTwCGrTw=
|
||||
github.com/unistack-org/micro-server-http/v3 v3.2.5/go.mod h1:TOeb0O65cr+MYL1l+AAnaprhuSKqeqDoIEuEk5wepAg=
|
||||
github.com/unistack-org/micro-server-http/v3 v3.2.6 h1:Ykd4M6cqH5gVq06XgOuHb1Tc/RPHku7Kc+UsrjN90Jc=
|
||||
github.com/unistack-org/micro-server-http/v3 v3.2.6/go.mod h1:EEtAuq3F7eNQYdeANzQpz2Y9YYNrQxopKyMP+7xxb7s=
|
||||
github.com/unistack-org/micro-server-tcp/v3 v3.2.2 h1:2/Xn+4+dnzY/tpD3MgLO1wg3ect9Jx5CLSDfPBjdjT4=
|
||||
github.com/unistack-org/micro-server-tcp/v3 v3.2.2/go.mod h1:TQDIck2+RdEAGIRnwv+2a0OVBUTkL6OM7YUY4AjFFmY=
|
||||
github.com/unistack-org/micro-wrapper-trace-opentracing/v3 v3.2.0 h1:PvemkpeCVUWfCoKwt1XmJ8uGK9My/7T29qOVxtYJohw=
|
||||
@ -533,7 +533,6 @@ github.com/unistack-org/micro/v3 v3.1.1/go.mod h1:0DgOy4OdJxQCDER8YSKitZugd2+1bd
|
||||
github.com/unistack-org/micro/v3 v3.2.1/go.mod h1:J8XxJj4Pqa3Ee0a4biRRtut7UwTlfBq8QRe+s4PKGS0=
|
||||
github.com/unistack-org/micro/v3 v3.2.2/go.mod h1:J8XxJj4Pqa3Ee0a4biRRtut7UwTlfBq8QRe+s4PKGS0=
|
||||
github.com/unistack-org/micro/v3 v3.2.4/go.mod h1:J8XxJj4Pqa3Ee0a4biRRtut7UwTlfBq8QRe+s4PKGS0=
|
||||
github.com/unistack-org/micro/v3 v3.2.8/go.mod h1:J8XxJj4Pqa3Ee0a4biRRtut7UwTlfBq8QRe+s4PKGS0=
|
||||
github.com/unistack-org/micro/v3 v3.2.11/go.mod h1:uGPB8BhDWHj63tR3eaoCd3X+lPtAgFG630897yhB3Ag=
|
||||
github.com/unistack-org/micro/v3 v3.2.14/go.mod h1:3j13mSd/rILNjyP0tEVtDxyDkJBtnHUXShNCuPHkC5A=
|
||||
github.com/unistack-org/micro/v3 v3.2.15-0.20210221215218-09973af099cd h1:r5Ii2dGYhLn0JzyQntmm1Y6LZ1Snr1iAHjexvZx+ETg=
|
||||
@ -651,8 +650,9 @@ golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81R
|
||||
golang.org/x/net v0.0.0-20200904194848-62affa334b73/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
||||
golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20210220033124-5f55cee0dc0d h1:1aflnvSoWWLI2k/dMUAl5lvU1YO4Mb4hz0gh+1rjcxU=
|
||||
golang.org/x/net v0.0.0-20210220033124-5f55cee0dc0d/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20210224082022-3d97a244fca7 h1:OgUuv8lsRpBibGNbSizVwKWlysjaNzmC9gYMhPVfqFM=
|
||||
golang.org/x/net v0.0.0-20210224082022-3d97a244fca7/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
|
@ -1,3 +1,3 @@
|
||||
package grpc
|
||||
|
||||
//go:generate protoc -I./proto -I$GOPATH/pkg/mod/github.com/grpc-ecosystem/grpc-gateway@v1.9.5/third_party/googleapis -I. --go-grpc_out=paths=source_relative:./proto --go_out=paths=source_relative:./proto --micro_out=paths=source_relative:./proto proto/test.proto
|
||||
//go:generate protoc -I./proto -I$GOPATH/pkg/mod/github.com/grpc-ecosystem/grpc-gateway@v1.9.5/third_party/googleapis -I. --go-grpc_out=paths=source_relative:./proto --go_out=paths=source_relative:./proto --micro_out=components=micro|rpc,debug=true,standalone=true,paths=source_relative:./gproto proto/test.proto
|
||||
|
30
server/grpc/gproto/test_micro.pb.go
Normal file
30
server/grpc/gproto/test_micro.pb.go
Normal file
@ -0,0 +1,30 @@
|
||||
// Code generated by protoc-gen-micro
|
||||
// source: test.proto
|
||||
package helloworld
|
||||
|
||||
import (
|
||||
context "context"
|
||||
proto "github.com/unistack-org/micro-tests/server/grpc/proto"
|
||||
api "github.com/unistack-org/micro/v3/api"
|
||||
client "github.com/unistack-org/micro/v3/client"
|
||||
)
|
||||
|
||||
func NewTestEndpoints() []*api.Endpoint {
|
||||
return []*api.Endpoint{
|
||||
&api.Endpoint{
|
||||
Name: "Test.Call",
|
||||
Path: []string{"/api/v0/test/call/TEST"},
|
||||
Method: []string{"POST"},
|
||||
Body: "*",
|
||||
Handler: "rpc",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
type TestClient interface {
|
||||
Call(ctx context.Context, req *proto.Request, opts ...client.CallOption) (*proto.Response, error)
|
||||
}
|
||||
|
||||
type TestServer interface {
|
||||
Call(ctx context.Context, req *proto.Request, rsp *proto.Response) error
|
||||
}
|
51
server/grpc/gproto/test_micro_rpc.pb.go
Normal file
51
server/grpc/gproto/test_micro_rpc.pb.go
Normal file
@ -0,0 +1,51 @@
|
||||
// Code generated by protoc-gen-micro
|
||||
// source: test.proto
|
||||
package helloworld
|
||||
|
||||
import (
|
||||
context "context"
|
||||
proto "github.com/unistack-org/micro-tests/server/grpc/proto"
|
||||
api "github.com/unistack-org/micro/v3/api"
|
||||
client "github.com/unistack-org/micro/v3/client"
|
||||
server "github.com/unistack-org/micro/v3/server"
|
||||
)
|
||||
|
||||
type testClient struct {
|
||||
c client.Client
|
||||
name string
|
||||
}
|
||||
|
||||
func NewTestClient(name string, c client.Client) TestClient {
|
||||
return &testClient{c: c, name: name}
|
||||
}
|
||||
|
||||
func (c *testClient) Call(ctx context.Context, req *proto.Request, opts ...client.CallOption) (*proto.Response, error) {
|
||||
rsp := &proto.Response{}
|
||||
err := c.c.Call(ctx, c.c.NewRequest(c.name, "Test.Call", req), rsp, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rsp, nil
|
||||
}
|
||||
|
||||
type testServer struct {
|
||||
TestServer
|
||||
}
|
||||
|
||||
func (h *testServer) Call(ctx context.Context, req *proto.Request, rsp *proto.Response) error {
|
||||
return h.TestServer.Call(ctx, req, rsp)
|
||||
}
|
||||
|
||||
func RegisterTestServer(s server.Server, sh TestServer, opts ...server.HandlerOption) error {
|
||||
type test interface {
|
||||
Call(ctx context.Context, req *proto.Request, rsp *proto.Response) error
|
||||
}
|
||||
type Test struct {
|
||||
test
|
||||
}
|
||||
h := &testServer{sh}
|
||||
for _, endpoint := range NewTestEndpoints() {
|
||||
opts = append(opts, api.WithEndpoint(endpoint))
|
||||
}
|
||||
return s.Handle(s.NewHandler(&Test{h}, opts...))
|
||||
}
|
@ -9,6 +9,7 @@ import (
|
||||
protocodec "github.com/unistack-org/micro-codec-proto/v3"
|
||||
regRouter "github.com/unistack-org/micro-router-register/v3"
|
||||
gserver "github.com/unistack-org/micro-server-grpc/v3"
|
||||
gpb "github.com/unistack-org/micro-tests/server/grpc/gproto"
|
||||
pb "github.com/unistack-org/micro-tests/server/grpc/proto"
|
||||
"github.com/unistack-org/micro/v3/broker"
|
||||
"github.com/unistack-org/micro/v3/client"
|
||||
@ -54,7 +55,7 @@ func TestGRPCServer(t *testing.T) {
|
||||
rtr := regRouter.NewRouter(router.Register(r))
|
||||
|
||||
h := &testServer{}
|
||||
err = pb.RegisterTestHandler(s, h)
|
||||
err = gpb.RegisterTestServer(s, h)
|
||||
if err != nil {
|
||||
t.Fatalf("can't register handler: %v", err)
|
||||
}
|
||||
|
@ -215,9 +215,12 @@ var file_test_proto_rawDesc = []byte{
|
||||
0x75, 0x65, 0x73, 0x74, 0x1a, 0x14, 0x2e, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77, 0x6f, 0x72, 0x6c,
|
||||
0x64, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x21, 0x82, 0xd3, 0xe4, 0x93,
|
||||
0x02, 0x1b, 0x22, 0x16, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x76, 0x30, 0x2f, 0x74, 0x65, 0x73, 0x74,
|
||||
0x2f, 0x63, 0x61, 0x6c, 0x6c, 0x2f, 0x54, 0x45, 0x53, 0x54, 0x3a, 0x01, 0x2a, 0x42, 0x0e, 0x5a,
|
||||
0x0c, 0x2e, 0x3b, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x62, 0x06, 0x70,
|
||||
0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
0x2f, 0x63, 0x61, 0x6c, 0x6c, 0x2f, 0x54, 0x45, 0x53, 0x54, 0x3a, 0x01, 0x2a, 0x42, 0x42, 0x5a,
|
||||
0x40, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x75, 0x6e, 0x69, 0x73,
|
||||
0x74, 0x61, 0x63, 0x6b, 0x2d, 0x6f, 0x72, 0x67, 0x2f, 0x6d, 0x69, 0x63, 0x72, 0x6f, 0x2d, 0x74,
|
||||
0x65, 0x73, 0x74, 0x73, 0x2f, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x2f, 0x67, 0x72, 0x70, 0x63,
|
||||
0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x3b, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77, 0x6f, 0x72, 0x6c,
|
||||
0x64, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
}
|
||||
|
||||
var (
|
||||
|
@ -2,7 +2,7 @@ syntax = "proto3";
|
||||
|
||||
package helloworld;
|
||||
|
||||
option go_package = ".;helloworld";
|
||||
option go_package = "github.com/unistack-org/micro-tests/server/grpc/proto;helloworld";
|
||||
import "google/api/annotations.proto";
|
||||
|
||||
service Test {
|
||||
|
@ -1,52 +0,0 @@
|
||||
// Code generated by protoc-gen-micro
|
||||
// source: test.proto
|
||||
package helloworld
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
micro_api "github.com/unistack-org/micro/v3/api"
|
||||
micro_client "github.com/unistack-org/micro/v3/client"
|
||||
micro_server "github.com/unistack-org/micro/v3/server"
|
||||
)
|
||||
|
||||
// NewTestEndpoints provides api endpoints metdata for Test service
|
||||
func NewTestEndpoints() []*micro_api.Endpoint {
|
||||
var endpoints []*micro_api.Endpoint
|
||||
endpoint := µ_api.Endpoint{
|
||||
Name: "Test.Call",
|
||||
Path: []string{"/api/v0/test/call/TEST"},
|
||||
Method: []string{"POST"},
|
||||
Body: "*",
|
||||
Handler: "rpc",
|
||||
}
|
||||
endpoints = append(endpoints, endpoint)
|
||||
return endpoints
|
||||
}
|
||||
|
||||
// TestService interface
|
||||
type TestService interface {
|
||||
Call(context.Context, *Request, ...micro_client.CallOption) (*Response, error)
|
||||
}
|
||||
|
||||
// Micro server stuff
|
||||
|
||||
// TestHandler server handler
|
||||
type TestHandler interface {
|
||||
Call(context.Context, *Request, *Response) error
|
||||
}
|
||||
|
||||
// RegisterTestHandler registers server handler
|
||||
func RegisterTestHandler(s micro_server.Server, sh TestHandler, opts ...micro_server.HandlerOption) error {
|
||||
type test interface {
|
||||
Call(context.Context, *Request, *Response) error
|
||||
}
|
||||
type Test struct {
|
||||
test
|
||||
}
|
||||
h := &testHandler{sh}
|
||||
for _, endpoint := range NewTestEndpoints() {
|
||||
opts = append(opts, micro_api.WithEndpoint(endpoint))
|
||||
}
|
||||
return s.Handle(s.NewHandler(&Test{h}, opts...))
|
||||
}
|
@ -1,46 +0,0 @@
|
||||
// Code generated by protoc-gen-micro
|
||||
// source: test.proto
|
||||
package helloworld
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
micro_client "github.com/unistack-org/micro/v3/client"
|
||||
micro_server "github.com/unistack-org/micro/v3/server"
|
||||
)
|
||||
|
||||
var (
|
||||
_ micro_server.Option
|
||||
_ micro_client.Option
|
||||
)
|
||||
|
||||
type testService struct {
|
||||
c micro_client.Client
|
||||
name string
|
||||
}
|
||||
|
||||
// Micro client stuff
|
||||
|
||||
// NewTestService create new service client
|
||||
func NewTestService(name string, c micro_client.Client) TestService {
|
||||
return &testService{c: c, name: name}
|
||||
}
|
||||
|
||||
func (c *testService) Call(ctx context.Context, req *Request, opts ...micro_client.CallOption) (*Response, error) {
|
||||
rsp := &Response{}
|
||||
err := c.c.Call(ctx, c.c.NewRequest(c.name, "Test.Call", req), rsp, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rsp, nil
|
||||
}
|
||||
|
||||
// Micro server stuff
|
||||
|
||||
type testHandler struct {
|
||||
TestHandler
|
||||
}
|
||||
|
||||
func (h *testHandler) Call(ctx context.Context, req *Request, rsp *Response) error {
|
||||
return h.TestHandler.Call(ctx, req, rsp)
|
||||
}
|
@ -1,3 +1,3 @@
|
||||
package http
|
||||
|
||||
//go:generate protoc -I./proto -I. -I/home/vtolstov/.cache/go-path/pkg/mod/github.com/grpc-ecosystem/grpc-gateway/v2@v2.1.0 -I/home/vtolstov/.cache/go-path/pkg/mod/github.com/grpc-ecosystem/grpc-gateway/v2@v2.1.0/third_party/googleapis --go-grpc_out=paths=source_relative:./proto --go_out=paths=source_relative:./proto --micro_out=components=micro|http,debug=true,paths=source_relative:./proto proto/test.proto
|
||||
//go:generate protoc -I./proto -I. -I/home/vtolstov/.cache/go-path/pkg/mod/github.com/grpc-ecosystem/grpc-gateway/v2@v2.1.0 -I/home/vtolstov/.cache/go-path/pkg/mod/github.com/grpc-ecosystem/grpc-gateway/v2@v2.1.0/third_party/googleapis --go_out=paths=source_relative:./proto --micro_out=components=micro|http,debug=true,paths=source_relative:./proto proto/test.proto
|
||||
|
@ -2,9 +2,10 @@ package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
httpsrv "github.com/unistack-org/micro-server-http/v3"
|
||||
pb "github.com/unistack-org/micro-tests/client/http/proto"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type GithubHandler struct{}
|
||||
@ -16,7 +17,7 @@ func NewGithubHandler() *GithubHandler {
|
||||
func (h *GithubHandler) LookupUser(ctx context.Context, req *pb.LookupUserReq, rsp *pb.LookupUserRsp) error {
|
||||
if req.GetUsername() == "" || req.GetUsername() != "vtolstov" {
|
||||
httpsrv.SetRspCode(ctx, http.StatusBadRequest)
|
||||
return &pb.Error{Message: "name is not correct"}
|
||||
return httpsrv.SetError(&pb.Error{Message: "name is not correct"})
|
||||
}
|
||||
rsp.Name = "Vasiliy Tolstov"
|
||||
httpsrv.SetRspCode(ctx, http.StatusOK)
|
||||
|
@ -39,7 +39,7 @@ func (h *Handler) Call(ctx context.Context, req *pb.CallReq, rsp *pb.CallRsp) er
|
||||
if len(req.Nested.Uint64Args) != 3 || req.Nested.Uint64Args[2].Value != 3 {
|
||||
h.t.Fatalf("invalid reflect merging")
|
||||
}
|
||||
md, ok := metadata.FromContext(ctx)
|
||||
md, ok := metadata.FromIncomingContext(ctx)
|
||||
if !ok {
|
||||
h.t.Fatalf("context without metadata")
|
||||
}
|
||||
@ -56,8 +56,7 @@ func (h *Handler) Call(ctx context.Context, req *pb.CallReq, rsp *pb.CallRsp) er
|
||||
|
||||
func (h *Handler) CallError(ctx context.Context, req *pb.CallReq1, rsp *pb.CallRsp1) error {
|
||||
httpsrv.SetRspCode(ctx, http.StatusBadRequest)
|
||||
return &pb.Error{Msg: "my_error"}
|
||||
return nil
|
||||
return httpsrv.SetError(&pb.Error{Msg: "my_error"})
|
||||
}
|
||||
|
||||
func TestNativeClientServer(t *testing.T) {
|
||||
@ -82,7 +81,7 @@ func TestNativeClientServer(t *testing.T) {
|
||||
)
|
||||
|
||||
h := &Handler{t: t}
|
||||
pb.RegisterTestHandler(srv, h)
|
||||
pb.RegisterTestServer(srv, h)
|
||||
|
||||
// start server
|
||||
if err := srv.Start(); err != nil {
|
||||
@ -105,7 +104,7 @@ func TestNativeClientServer(t *testing.T) {
|
||||
|
||||
cli := client.NewClientCallOptions(httpcli.NewClient(client.ContentType("application/json"), client.Codec("application/json", jsonpbcodec.NewCodec())), client.WithAddress(fmt.Sprintf("http://%s", service[0].Nodes[0].Address)))
|
||||
|
||||
svc := pb.NewTestService("helloworld", cli)
|
||||
svc := pb.NewTestClient("helloworld", cli)
|
||||
rsp, err := svc.Call(ctx, &pb.CallReq{
|
||||
Name: "my_name",
|
||||
Nested: &pb.Nested{Uint64Args: []*wrapperpb.UInt64Value{
|
||||
@ -145,7 +144,7 @@ func TestNativeServer(t *testing.T) {
|
||||
)
|
||||
|
||||
h := &Handler{t: t}
|
||||
pb.RegisterTestHandler(srv, h)
|
||||
pb.RegisterTestServer(srv, h)
|
||||
|
||||
// start server
|
||||
if err := srv.Start(); err != nil {
|
||||
|
@ -1,126 +0,0 @@
|
||||
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
|
||||
|
||||
package pb
|
||||
|
||||
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.
|
||||
const _ = grpc.SupportPackageIsVersion6
|
||||
|
||||
// TestClient is the client API for Test 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 TestClient interface {
|
||||
Call(ctx context.Context, in *CallReq, opts ...grpc.CallOption) (*CallRsp, error)
|
||||
CallError(ctx context.Context, in *CallReq1, opts ...grpc.CallOption) (*CallRsp1, error)
|
||||
}
|
||||
|
||||
type testClient struct {
|
||||
cc grpc.ClientConnInterface
|
||||
}
|
||||
|
||||
func NewTestClient(cc grpc.ClientConnInterface) TestClient {
|
||||
return &testClient{cc}
|
||||
}
|
||||
|
||||
func (c *testClient) Call(ctx context.Context, in *CallReq, opts ...grpc.CallOption) (*CallRsp, error) {
|
||||
out := new(CallRsp)
|
||||
err := c.cc.Invoke(ctx, "/test.Test/Call", in, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *testClient) CallError(ctx context.Context, in *CallReq1, opts ...grpc.CallOption) (*CallRsp1, error) {
|
||||
out := new(CallRsp1)
|
||||
err := c.cc.Invoke(ctx, "/test.Test/CallError", in, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// TestServer is the server API for Test service.
|
||||
// All implementations must embed UnimplementedTestServer
|
||||
// for forward compatibility
|
||||
type TestServer interface {
|
||||
Call(context.Context, *CallReq) (*CallRsp, error)
|
||||
CallError(context.Context, *CallReq1) (*CallRsp1, error)
|
||||
mustEmbedUnimplementedTestServer()
|
||||
}
|
||||
|
||||
// UnimplementedTestServer must be embedded to have forward compatible implementations.
|
||||
type UnimplementedTestServer struct {
|
||||
}
|
||||
|
||||
func (*UnimplementedTestServer) Call(context.Context, *CallReq) (*CallRsp, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method Call not implemented")
|
||||
}
|
||||
func (*UnimplementedTestServer) CallError(context.Context, *CallReq1) (*CallRsp1, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method CallError not implemented")
|
||||
}
|
||||
func (*UnimplementedTestServer) mustEmbedUnimplementedTestServer() {}
|
||||
|
||||
func RegisterTestServer(s *grpc.Server, srv TestServer) {
|
||||
s.RegisterService(&_Test_serviceDesc, srv)
|
||||
}
|
||||
|
||||
func _Test_Call_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(CallReq)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(TestServer).Call(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/test.Test/Call",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(TestServer).Call(ctx, req.(*CallReq))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _Test_CallError_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(CallReq1)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(TestServer).CallError(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/test.Test/CallError",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(TestServer).CallError(ctx, req.(*CallReq1))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
var _Test_serviceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "test.Test",
|
||||
HandlerType: (*TestServer)(nil),
|
||||
Methods: []grpc.MethodDesc{
|
||||
{
|
||||
MethodName: "Call",
|
||||
Handler: _Test_Call_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "CallError",
|
||||
Handler: _Test_CallError_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{},
|
||||
Metadata: "test.proto",
|
||||
}
|
@ -3,45 +3,36 @@
|
||||
package pb
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
micro_api "github.com/unistack-org/micro/v3/api"
|
||||
micro_client "github.com/unistack-org/micro/v3/client"
|
||||
context "context"
|
||||
api "github.com/unistack-org/micro/v3/api"
|
||||
client "github.com/unistack-org/micro/v3/client"
|
||||
)
|
||||
|
||||
// NewTestEndpoints provides api endpoints metdata for Test service
|
||||
func NewTestEndpoints() []*micro_api.Endpoint {
|
||||
endpoints := make([]*micro_api.Endpoint, 0, 2)
|
||||
var endpoint *micro_api.Endpoint
|
||||
endpoint = µ_api.Endpoint{
|
||||
Name: "Test.Call",
|
||||
Path: []string{"/v1/test/call/{name}"},
|
||||
Method: []string{"POST"},
|
||||
Body: "*",
|
||||
Handler: "rpc",
|
||||
func NewTestEndpoints() []*api.Endpoint {
|
||||
return []*api.Endpoint{
|
||||
&api.Endpoint{
|
||||
Name: "Test.Call",
|
||||
Path: []string{"/v1/test/call/{name}"},
|
||||
Method: []string{"POST"},
|
||||
Body: "*",
|
||||
Handler: "rpc",
|
||||
},
|
||||
&api.Endpoint{
|
||||
Name: "Test.CallError",
|
||||
Path: []string{"/v1/test/callerror/{name}"},
|
||||
Method: []string{"POST"},
|
||||
Body: "*",
|
||||
Handler: "rpc",
|
||||
},
|
||||
}
|
||||
endpoints = append(endpoints, endpoint)
|
||||
endpoint = µ_api.Endpoint{
|
||||
Name: "Test.CallError",
|
||||
Path: []string{"/v1/test/callerror/{name}"},
|
||||
Method: []string{"POST"},
|
||||
Body: "*",
|
||||
Handler: "rpc",
|
||||
}
|
||||
endpoints = append(endpoints, endpoint)
|
||||
return endpoints
|
||||
}
|
||||
|
||||
// TestService interface
|
||||
type TestService interface {
|
||||
Call(context.Context, *CallReq, ...micro_client.CallOption) (*CallRsp, error)
|
||||
CallError(context.Context, *CallReq1, ...micro_client.CallOption) (*CallRsp1, error)
|
||||
type TestClient interface {
|
||||
Call(ctx context.Context, req *CallReq, opts ...client.CallOption) (*CallRsp, error)
|
||||
CallError(ctx context.Context, req *CallReq1, opts ...client.CallOption) (*CallRsp1, error)
|
||||
}
|
||||
|
||||
// Micro server stuff
|
||||
|
||||
// TestHandler server handler
|
||||
type TestHandler interface {
|
||||
Call(context.Context, *CallReq, *CallRsp) error
|
||||
CallError(context.Context, *CallReq1, *CallRsp1) error
|
||||
type TestServer interface {
|
||||
Call(ctx context.Context, req *CallReq, rsp *CallRsp) error
|
||||
CallError(ctx context.Context, req *CallReq1, rsp *CallRsp1) error
|
||||
}
|
||||
|
@ -3,115 +3,79 @@
|
||||
package pb
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
// "net/http"
|
||||
|
||||
micro_client_http "github.com/unistack-org/micro-client-http/v3"
|
||||
micro_api "github.com/unistack-org/micro/v3/api"
|
||||
micro_client "github.com/unistack-org/micro/v3/client"
|
||||
micro_server "github.com/unistack-org/micro/v3/server"
|
||||
context "context"
|
||||
v3 "github.com/unistack-org/micro-client-http/v3"
|
||||
api "github.com/unistack-org/micro/v3/api"
|
||||
client "github.com/unistack-org/micro/v3/client"
|
||||
server "github.com/unistack-org/micro/v3/server"
|
||||
)
|
||||
|
||||
var (
|
||||
_ micro_server.Option
|
||||
_ micro_client.Option
|
||||
)
|
||||
|
||||
type testService struct {
|
||||
c micro_client.Client
|
||||
type testClient struct {
|
||||
c client.Client
|
||||
name string
|
||||
}
|
||||
|
||||
// Micro client stuff
|
||||
|
||||
// NewTestService create new service client
|
||||
func NewTestService(name string, c micro_client.Client) TestService {
|
||||
return &testService{c: c, name: name}
|
||||
func NewTestClient(name string, c client.Client) TestClient {
|
||||
return &testClient{c: c, name: name}
|
||||
}
|
||||
|
||||
func (c *testService) Call(ctx context.Context, req *CallReq, opts ...micro_client.CallOption) (*CallRsp, error) {
|
||||
func (c *testClient) Call(ctx context.Context, req *CallReq, opts ...client.CallOption) (*CallRsp, error) {
|
||||
errmap := make(map[string]interface{}, 1)
|
||||
errmap["default"] = &Error{}
|
||||
|
||||
nopts := append(opts,
|
||||
micro_client_http.Method("POST"),
|
||||
micro_client_http.Path("/v1/test/call/{name}"),
|
||||
micro_client_http.Body("*"),
|
||||
micro_client_http.ErrorMap(errmap),
|
||||
opts = append(opts,
|
||||
v3.ErrorMap(errmap),
|
||||
v3.Method("POST"),
|
||||
v3.Path("/v1/test/call/{name}"),
|
||||
v3.Body("*"),
|
||||
)
|
||||
rsp := &CallRsp{}
|
||||
err := c.c.Call(ctx, c.c.NewRequest(c.name, "Test.Call", req), rsp, nopts...)
|
||||
err := c.c.Call(ctx, c.c.NewRequest(c.name, "Test.Call", req), rsp, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rsp, nil
|
||||
}
|
||||
|
||||
func (c *testService) CallError(ctx context.Context, req *CallReq1, opts ...micro_client.CallOption) (*CallRsp1, error) {
|
||||
func (c *testClient) CallError(ctx context.Context, req *CallReq1, opts ...client.CallOption) (*CallRsp1, error) {
|
||||
errmap := make(map[string]interface{}, 1)
|
||||
errmap["default"] = &Error{}
|
||||
|
||||
nopts := append(opts,
|
||||
micro_client_http.Method("POST"),
|
||||
micro_client_http.Path("/v1/test/callerror/{name}"),
|
||||
micro_client_http.Body("*"),
|
||||
micro_client_http.ErrorMap(errmap),
|
||||
opts = append(opts,
|
||||
v3.ErrorMap(errmap),
|
||||
v3.Method("POST"),
|
||||
v3.Path("/v1/test/callerror/{name}"),
|
||||
v3.Body("*"),
|
||||
)
|
||||
rsp := &CallRsp1{}
|
||||
err := c.c.Call(ctx, c.c.NewRequest(c.name, "Test.CallError", req), rsp, nopts...)
|
||||
err := c.c.Call(ctx, c.c.NewRequest(c.name, "Test.CallError", req), rsp, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rsp, nil
|
||||
}
|
||||
|
||||
// Micro server stuff
|
||||
|
||||
type testHandler struct {
|
||||
TestHandler
|
||||
type testServer struct {
|
||||
TestServer
|
||||
}
|
||||
|
||||
func (h *testHandler) Call(ctx context.Context, req *CallReq, rsp *CallRsp) error {
|
||||
return h.TestHandler.Call(ctx, req, rsp)
|
||||
func (h *testServer) Call(ctx context.Context, req *CallReq, rsp *CallRsp) error {
|
||||
return h.TestServer.Call(ctx, req, rsp)
|
||||
}
|
||||
|
||||
/*
|
||||
func (h *testHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Printf("new request: %#+v\n", r)
|
||||
// HANDLE ALL STUFF
|
||||
}
|
||||
*/
|
||||
|
||||
func (h *testHandler) CallError(ctx context.Context, req *CallReq1, rsp *CallRsp1) error {
|
||||
return h.TestHandler.CallError(ctx, req, rsp)
|
||||
func (h *testServer) CallError(ctx context.Context, req *CallReq1, rsp *CallRsp1) error {
|
||||
return h.TestServer.CallError(ctx, req, rsp)
|
||||
}
|
||||
|
||||
/*
|
||||
func (h *testHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Printf("new request: %#+v\n", r)
|
||||
// HANDLE ALL STUFF
|
||||
}
|
||||
*/
|
||||
|
||||
// Error method to satisfy error interface
|
||||
func (e *Error) Error() string {
|
||||
return fmt.Sprintf("%#v", e)
|
||||
}
|
||||
|
||||
// RegisterTestHandler registers server handler
|
||||
func RegisterTestHandler(s micro_server.Server, sh TestHandler, opts ...micro_server.HandlerOption) error {
|
||||
func RegisterTestServer(s server.Server, sh TestServer, opts ...server.HandlerOption) error {
|
||||
type test interface {
|
||||
Call(context.Context, *CallReq, *CallRsp) error
|
||||
CallError(context.Context, *CallReq1, *CallRsp1) error
|
||||
// ServeHTTP(http.ResponseWriter, *http.Request)
|
||||
Call(ctx context.Context, req *CallReq, rsp *CallRsp) error
|
||||
CallError(ctx context.Context, req *CallReq1, rsp *CallRsp1) error
|
||||
}
|
||||
type Test struct {
|
||||
test
|
||||
}
|
||||
h := &testHandler{sh}
|
||||
h := &testServer{sh}
|
||||
for _, endpoint := range NewTestEndpoints() {
|
||||
opts = append(opts, micro_api.WithEndpoint(endpoint))
|
||||
opts = append(opts, api.WithEndpoint(endpoint))
|
||||
}
|
||||
return s.Handle(s.NewHandler(&Test{h}, opts...))
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user