2015-01-13 23:31:27 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-01-31 16:49:21 +01:00
|
|
|
log "github.com/golang/glog"
|
2015-11-20 16:17:33 +00:00
|
|
|
"github.com/micro/go-micro/cmd"
|
|
|
|
"github.com/micro/go-micro/examples/server/handler"
|
|
|
|
"github.com/micro/go-micro/examples/server/subscriber"
|
|
|
|
"github.com/micro/go-micro/server"
|
2015-12-02 11:54:36 +00:00
|
|
|
"golang.org/x/net/context"
|
2015-01-13 23:31:27 +00:00
|
|
|
)
|
|
|
|
|
2015-12-02 11:54:36 +00:00
|
|
|
func logWrapper(fn server.HandlerFunc) server.HandlerFunc {
|
2015-12-02 20:56:50 +00:00
|
|
|
return func(ctx context.Context, req server.Request, rsp interface{}) error {
|
|
|
|
log.Infof("[Log Wrapper] Before serving request method: %v", req.Method())
|
2015-12-02 11:54:36 +00:00
|
|
|
err := fn(ctx, req, rsp)
|
|
|
|
log.Infof("[Log Wrapper] After serving request")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-02 20:00:06 +00:00
|
|
|
func logSubWrapper(fn server.SubscriberFunc) server.SubscriberFunc {
|
2015-12-02 20:56:50 +00:00
|
|
|
return func(ctx context.Context, req server.Publication) error {
|
|
|
|
log.Infof("[Log Sub Wrapper] Before serving publication topic: %v", req.Topic())
|
2015-12-02 20:00:06 +00:00
|
|
|
err := fn(ctx, req)
|
|
|
|
log.Infof("[Log Sub Wrapper] After serving publication")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-13 23:31:27 +00:00
|
|
|
func main() {
|
2015-02-14 23:33:21 +00:00
|
|
|
// optionally setup command line usage
|
|
|
|
cmd.Init()
|
|
|
|
|
2015-12-31 18:11:46 +00:00
|
|
|
md := server.DefaultOptions().Metadata
|
2015-12-09 15:06:31 +00:00
|
|
|
md["datacenter"] = "local"
|
|
|
|
|
2015-12-02 11:54:36 +00:00
|
|
|
server.DefaultServer = server.NewServer(
|
|
|
|
server.WrapHandler(logWrapper),
|
2015-12-02 20:00:06 +00:00
|
|
|
server.WrapSubscriber(logSubWrapper),
|
2015-12-09 15:06:31 +00:00
|
|
|
server.Metadata(md),
|
2015-12-02 11:54:36 +00:00
|
|
|
)
|
2015-11-27 00:17:36 +00:00
|
|
|
|
2015-01-13 23:31:27 +00:00
|
|
|
// Initialise Server
|
2015-05-26 22:39:48 +01:00
|
|
|
server.Init(
|
|
|
|
server.Name("go.micro.srv.example"),
|
|
|
|
)
|
2015-01-13 23:31:27 +00:00
|
|
|
|
|
|
|
// Register Handlers
|
2015-06-03 01:25:37 +01:00
|
|
|
server.Handle(
|
|
|
|
server.NewHandler(
|
2015-01-13 23:31:27 +00:00
|
|
|
new(handler.Example),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2015-06-12 19:52:27 +01:00
|
|
|
// Register Subscribers
|
2015-12-02 17:42:14 +00:00
|
|
|
if err := server.Subscribe(
|
2015-06-12 19:52:27 +01:00
|
|
|
server.NewSubscriber(
|
|
|
|
"topic.go.micro.srv.example",
|
|
|
|
new(subscriber.Example),
|
|
|
|
),
|
2015-12-02 17:42:14 +00:00
|
|
|
); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2015-06-12 19:52:27 +01:00
|
|
|
|
2015-12-02 17:42:14 +00:00
|
|
|
if err := server.Subscribe(
|
2015-06-12 19:52:27 +01:00
|
|
|
server.NewSubscriber(
|
|
|
|
"topic.go.micro.srv.example",
|
|
|
|
subscriber.Handler,
|
|
|
|
),
|
2015-12-02 17:42:14 +00:00
|
|
|
); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2015-06-12 19:52:27 +01:00
|
|
|
|
2015-01-13 23:31:27 +00:00
|
|
|
// Run server
|
|
|
|
if err := server.Run(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|