1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-05-19 21:23:04 +02:00
2020-12-26 15:21:29 +00:00

58 lines
1.4 KiB
Go

package main
import (
"log"
proto "github.com/micro/go-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)
}
}