github.com/micro/go-micro/examples@v0.0.0-20210105173217-bf4ab679e18b/api/rpc/rpc.go (about) 1 package main 2 3 import ( 4 "log" 5 6 proto "github.com/micro/go-micro/examples/api/rpc/proto" 7 "github.com/micro/go-micro/v2" 8 "github.com/micro/go-micro/v2/errors" 9 10 "context" 11 ) 12 13 type Example struct{} 14 15 type Foo struct{} 16 17 // Example.Call is a method which will be served by http request /example/call 18 // In the event we see /[service]/[method] the [service] is used as part of the method 19 // E.g /example/call goes to go.micro.api.example Example.Call 20 func (e *Example) Call(ctx context.Context, req *proto.CallRequest, rsp *proto.CallResponse) error { 21 log.Print("Received Example.Call request") 22 23 if len(req.Name) == 0 { 24 return errors.BadRequest("go.micro.api.example", "no content") 25 } 26 27 rsp.Message = "got your request " + req.Name 28 return nil 29 } 30 31 // Foo.Bar is a method which will be served by http request /example/foo/bar 32 // Because Foo is not the same as the service name it is mapped beyond /example/ 33 func (f *Foo) Bar(ctx context.Context, req *proto.EmptyRequest, rsp *proto.EmptyResponse) error { 34 log.Print("Received Foo.Bar request") 35 36 // noop 37 38 return nil 39 } 40 41 func main() { 42 service := micro.NewService( 43 micro.Name("go.micro.api.example"), 44 ) 45 46 service.Init() 47 48 // register example handler 49 proto.RegisterExampleHandler(service.Server(), new(Example)) 50 51 // register foo handler 52 proto.RegisterFooHandler(service.Server(), new(Foo)) 53 54 if err := service.Run(); err != nil { 55 log.Fatal(err) 56 } 57 }