Example of how to use wrappers with top level service
This commit is contained in:
parent
c4dabe2652
commit
6bf0affc75
3
examples/service/wrapper/README.md
Normal file
3
examples/service/wrapper/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# Client/Server Wrapper
|
||||
|
||||
This is an example using wrappers or middleware
|
116
examples/service/wrapper/main.go
Normal file
116
examples/service/wrapper/main.go
Normal file
@ -0,0 +1,116 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/micro/cli"
|
||||
"github.com/micro/go-micro"
|
||||
"github.com/micro/go-micro/client"
|
||||
c "github.com/micro/go-micro/context"
|
||||
proto "github.com/micro/go-micro/examples/service/proto"
|
||||
"github.com/micro/go-micro/server"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
/*
|
||||
|
||||
Example usage of top level service initialisation including wrappers
|
||||
|
||||
*/
|
||||
|
||||
type logWrapper struct {
|
||||
client.Client
|
||||
}
|
||||
|
||||
func (l *logWrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
|
||||
md, _ := c.GetMetadata(ctx)
|
||||
fmt.Printf("[Log Wrapper] ctx: %v service: %s method: %s\n", md, req.Service(), req.Method())
|
||||
return l.Client.Call(ctx, req, rsp)
|
||||
}
|
||||
|
||||
// Implements client.Wrapper as logWrapper
|
||||
func logWrap(c client.Client) client.Client {
|
||||
return &logWrapper{c}
|
||||
}
|
||||
|
||||
// Implements the server.HandlerWrapper
|
||||
func logHandlerWrapper(fn server.HandlerFunc) server.HandlerFunc {
|
||||
return func(ctx context.Context, req server.Request, rsp interface{}) error {
|
||||
fmt.Printf("[Log Wrapper] Before serving request method: %v\n", req.Method())
|
||||
err := fn(ctx, req, rsp)
|
||||
fmt.Println("[Log Wrapper] After serving request")
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
type Greeter struct{}
|
||||
|
||||
func (g *Greeter) Hello(ctx context.Context, req *proto.HelloRequest, rsp *proto.HelloResponse) error {
|
||||
rsp.Greeting = "Hello " + req.Name
|
||||
return nil
|
||||
}
|
||||
|
||||
// Setup and the client
|
||||
func runClient(service micro.Service) {
|
||||
// Create new greeter client
|
||||
greeter := proto.NewGreeterClient("greeter", service.Client())
|
||||
|
||||
// Call the greeter
|
||||
rsp, err := greeter.Hello(context.TODO(), &proto.HelloRequest{Name: "John"})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
// Print response
|
||||
fmt.Println(rsp.Greeting)
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Create a new service. Optionally include some options here.
|
||||
service := micro.NewService(
|
||||
micro.Client(client.NewClient(client.Wrap(logWrap))),
|
||||
micro.Server(server.NewServer(server.WrapHandler(logHandlerWrapper))),
|
||||
micro.Name("greeter"),
|
||||
micro.Version("latest"),
|
||||
micro.Metadata(map[string]string{
|
||||
"type": "helloworld",
|
||||
}),
|
||||
|
||||
// Setup some flags. Specify --client to run the client
|
||||
|
||||
// Add runtime flags
|
||||
// We could do this below too
|
||||
micro.Flags(cli.BoolFlag{
|
||||
Name: "client",
|
||||
Usage: "Launch the client",
|
||||
}),
|
||||
)
|
||||
|
||||
// Init will parse the command line flags. Any flags set will
|
||||
// override the above settings. Options defined here will
|
||||
// override anything set on the command line.
|
||||
service.Init(
|
||||
// Add runtime action
|
||||
// We could actually do this above
|
||||
micro.Action(func(c *cli.Context) {
|
||||
if c.Bool("client") {
|
||||
runClient(service)
|
||||
os.Exit(0)
|
||||
}
|
||||
}),
|
||||
)
|
||||
|
||||
// By default we'll run the server unless the flags catch us
|
||||
|
||||
// Setup the server
|
||||
|
||||
// Register handler
|
||||
proto.RegisterGreeterHandler(service.Server(), new(Greeter))
|
||||
|
||||
// Run the server
|
||||
if err := service.Run(); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user