update after protoc-gen-micro changes
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
@@ -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...))
|
||||
}
|
||||
|
Reference in New Issue
Block a user