1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-07-12 22:41:07 +02:00

Add examples

This commit is contained in:
Asim Aslam
2020-12-26 15:17:20 +00:00
parent 273bab5dd7
commit a34c70de0e
320 changed files with 20803 additions and 0 deletions

View File

@ -0,0 +1,33 @@
# RPC API
This example makes use of the micro API with the RPC handler.
The api in this mode lets you serve http while routing to standard go-micro services via RPC.
The micro api with rpc handler only supports POST method and expects content-type of application/json or application/protobuf.
## Usage
Run the micro API with the rpc handler
```
micro api --handler=rpc
```
Run this example
```
go run rpc.go
```
Make a POST request to /example/call which will call go.micro.api.example Example.Call
```
curl -H 'Content-Type: application/json' -d '{"name": "john"}' "http://localhost:8080/example/call"
```
Make a POST request to /example/foo/bar which will call go.micro.api.example Foo.Bar
```
curl -H 'Content-Type: application/json' -d '{}' http://localhost:8080/example/foo/bar
```

View File

@ -0,0 +1,160 @@
// Code generated by protoc-gen-micro. DO NOT EDIT.
// source: github.com/micro/examples/api/rpc/proto/api.proto
/*
Package api is a generated protocol buffer package.
It is generated from these files:
github.com/micro/examples/api/rpc/proto/api.proto
It has these top-level messages:
CallRequest
CallResponse
EmptyRequest
EmptyResponse
*/
package api
import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"
import (
context "context"
client "github.com/micro/go-micro/v2/client"
server "github.com/micro/go-micro/v2/server"
)
// 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.ProtoPackageIsVersion2 // please upgrade the proto package
// Reference imports to suppress errors if they are not otherwise used.
var _ context.Context
var _ client.Option
var _ server.Option
// Client API for Example service
type ExampleService interface {
Call(ctx context.Context, in *CallRequest, opts ...client.CallOption) (*CallResponse, error)
}
type exampleService struct {
c client.Client
serviceName string
}
func NewExampleService(serviceName string, c client.Client) ExampleService {
if c == nil {
c = client.NewClient()
}
if len(serviceName) == 0 {
serviceName = "example"
}
return &exampleService{
c: c,
serviceName: serviceName,
}
}
func (c *exampleService) Call(ctx context.Context, in *CallRequest, opts ...client.CallOption) (*CallResponse, error) {
req := c.c.NewRequest(c.serviceName, "Example.Call", in)
out := new(CallResponse)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// Server API for Example service
type ExampleHandler interface {
Call(context.Context, *CallRequest, *CallResponse) error
}
func RegisterExampleHandler(s server.Server, hdlr ExampleHandler, opts ...server.HandlerOption) {
type example interface {
Call(ctx context.Context, in *CallRequest, out *CallResponse) error
}
type Example struct {
example
}
h := &exampleHandler{hdlr}
s.Handle(s.NewHandler(&Example{h}, opts...))
}
type exampleHandler struct {
ExampleHandler
}
func (h *exampleHandler) Call(ctx context.Context, in *CallRequest, out *CallResponse) error {
return h.ExampleHandler.Call(ctx, in, out)
}
// Client API for Foo service
type FooService interface {
Bar(ctx context.Context, in *EmptyRequest, opts ...client.CallOption) (*EmptyResponse, error)
}
type fooService struct {
c client.Client
serviceName string
}
func NewFooService(serviceName string, c client.Client) FooService {
if c == nil {
c = client.NewClient()
}
if len(serviceName) == 0 {
serviceName = "foo"
}
return &fooService{
c: c,
serviceName: serviceName,
}
}
func (c *fooService) Bar(ctx context.Context, in *EmptyRequest, opts ...client.CallOption) (*EmptyResponse, error) {
req := c.c.NewRequest(c.serviceName, "Foo.Bar", in)
out := new(EmptyResponse)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// Server API for Foo service
type FooHandler interface {
Bar(context.Context, *EmptyRequest, *EmptyResponse) error
}
func RegisterFooHandler(s server.Server, hdlr FooHandler, opts ...server.HandlerOption) {
type foo interface {
Bar(ctx context.Context, in *EmptyRequest, out *EmptyResponse) error
}
type Foo struct {
foo
}
h := &fooHandler{hdlr}
s.Handle(s.NewHandler(&Foo{h}, opts...))
}
type fooHandler struct {
FooHandler
}
func (h *fooHandler) Bar(ctx context.Context, in *EmptyRequest, out *EmptyResponse) error {
return h.FooHandler.Bar(ctx, in, out)
}

View File

@ -0,0 +1,246 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// source: github.com/micro/examples/api/rpc/proto/api.proto
/*
Package api is a generated protocol buffer package.
It is generated from these files:
github.com/micro/examples/api/rpc/proto/api.proto
It has these top-level messages:
CallRequest
CallResponse
EmptyRequest
EmptyResponse
*/
package api
import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"
import (
context "golang.org/x/net/context"
grpc "google.golang.org/grpc"
)
// 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.ProtoPackageIsVersion2 // please upgrade the proto package
type CallRequest struct {
Name string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"`
}
func (m *CallRequest) Reset() { *m = CallRequest{} }
func (m *CallRequest) String() string { return proto.CompactTextString(m) }
func (*CallRequest) ProtoMessage() {}
func (*CallRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
func (m *CallRequest) GetName() string {
if m != nil {
return m.Name
}
return ""
}
type CallResponse struct {
Message string `protobuf:"bytes,2,opt,name=message" json:"message,omitempty"`
}
func (m *CallResponse) Reset() { *m = CallResponse{} }
func (m *CallResponse) String() string { return proto.CompactTextString(m) }
func (*CallResponse) ProtoMessage() {}
func (*CallResponse) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} }
func (m *CallResponse) GetMessage() string {
if m != nil {
return m.Message
}
return ""
}
type EmptyRequest struct {
}
func (m *EmptyRequest) Reset() { *m = EmptyRequest{} }
func (m *EmptyRequest) String() string { return proto.CompactTextString(m) }
func (*EmptyRequest) ProtoMessage() {}
func (*EmptyRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{2} }
type EmptyResponse struct {
}
func (m *EmptyResponse) Reset() { *m = EmptyResponse{} }
func (m *EmptyResponse) String() string { return proto.CompactTextString(m) }
func (*EmptyResponse) ProtoMessage() {}
func (*EmptyResponse) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{3} }
func init() {
proto.RegisterType((*CallRequest)(nil), "CallRequest")
proto.RegisterType((*CallResponse)(nil), "CallResponse")
proto.RegisterType((*EmptyRequest)(nil), "EmptyRequest")
proto.RegisterType((*EmptyResponse)(nil), "EmptyResponse")
}
// 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
// Client API for Example service
type ExampleClient interface {
Call(ctx context.Context, in *CallRequest, opts ...grpc.CallOption) (*CallResponse, error)
}
type exampleClient struct {
cc *grpc.ClientConn
}
func NewExampleClient(cc *grpc.ClientConn) ExampleClient {
return &exampleClient{cc}
}
func (c *exampleClient) Call(ctx context.Context, in *CallRequest, opts ...grpc.CallOption) (*CallResponse, error) {
out := new(CallResponse)
err := grpc.Invoke(ctx, "/Example/Call", in, out, c.cc, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// Server API for Example service
type ExampleServer interface {
Call(context.Context, *CallRequest) (*CallResponse, error)
}
func RegisterExampleServer(s *grpc.Server, srv ExampleServer) {
s.RegisterService(&_Example_serviceDesc, srv)
}
func _Example_Call_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(CallRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(ExampleServer).Call(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/Example/Call",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(ExampleServer).Call(ctx, req.(*CallRequest))
}
return interceptor(ctx, in, info, handler)
}
var _Example_serviceDesc = grpc.ServiceDesc{
ServiceName: "Example",
HandlerType: (*ExampleServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "Call",
Handler: _Example_Call_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "github.com/micro/examples/api/rpc/proto/api.proto",
}
// Client API for Foo service
type FooClient interface {
Bar(ctx context.Context, in *EmptyRequest, opts ...grpc.CallOption) (*EmptyResponse, error)
}
type fooClient struct {
cc *grpc.ClientConn
}
func NewFooClient(cc *grpc.ClientConn) FooClient {
return &fooClient{cc}
}
func (c *fooClient) Bar(ctx context.Context, in *EmptyRequest, opts ...grpc.CallOption) (*EmptyResponse, error) {
out := new(EmptyResponse)
err := grpc.Invoke(ctx, "/Foo/Bar", in, out, c.cc, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// Server API for Foo service
type FooServer interface {
Bar(context.Context, *EmptyRequest) (*EmptyResponse, error)
}
func RegisterFooServer(s *grpc.Server, srv FooServer) {
s.RegisterService(&_Foo_serviceDesc, srv)
}
func _Foo_Bar_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(EmptyRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(FooServer).Bar(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/Foo/Bar",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(FooServer).Bar(ctx, req.(*EmptyRequest))
}
return interceptor(ctx, in, info, handler)
}
var _Foo_serviceDesc = grpc.ServiceDesc{
ServiceName: "Foo",
HandlerType: (*FooServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "Bar",
Handler: _Foo_Bar_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "github.com/micro/examples/api/rpc/proto/api.proto",
}
func init() { proto.RegisterFile("github.com/micro/examples/api/rpc/proto/api.proto", fileDescriptor0) }
var fileDescriptor0 = []byte{
// 203 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0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,
0x00, 0xff, 0xff, 0x81, 0x2d, 0x74, 0x3d, 0x0a, 0x01, 0x00, 0x00,
}

View File

@ -0,0 +1,23 @@
syntax = "proto3";
service Example {
rpc Call(CallRequest) returns(CallResponse) {};
}
service Foo {
rpc Bar(EmptyRequest) returns(EmptyResponse) {};
}
message CallRequest {
string name = 1;
}
message CallResponse {
string message = 2;
}
message EmptyRequest {
}
message EmptyResponse {
}

57
examples/api/rpc/rpc.go Normal file
View File

@ -0,0 +1,57 @@
package main
import (
"log"
proto "github.com/micro/examples/api/rpc/proto"
"github.com/micro/go-micro/v2"
"github.com/micro/go-micro/v2/errors"
"context"
)
type Example struct{}
type Foo struct{}
// Example.Call is a method which will be served by http request /example/call
// In the event we see /[service]/[method] the [service] is used as part of the method
// E.g /example/call goes to go.micro.api.example Example.Call
func (e *Example) Call(ctx context.Context, req *proto.CallRequest, rsp *proto.CallResponse) error {
log.Print("Received Example.Call request")
if len(req.Name) == 0 {
return errors.BadRequest("go.micro.api.example", "no content")
}
rsp.Message = "got your request " + req.Name
return nil
}
// Foo.Bar is a method which will be served by http request /example/foo/bar
// Because Foo is not the same as the service name it is mapped beyond /example/
func (f *Foo) Bar(ctx context.Context, req *proto.EmptyRequest, rsp *proto.EmptyResponse) error {
log.Print("Received Foo.Bar request")
// noop
return nil
}
func main() {
service := micro.NewService(
micro.Name("go.micro.api.example"),
)
service.Init()
// register example handler
proto.RegisterExampleHandler(service.Server(), new(Example))
// register foo handler
proto.RegisterFooHandler(service.Server(), new(Foo))
if err := service.Run(); err != nil {
log.Fatal(err)
}
}