micro/examples/server/main.go

65 lines
1.2 KiB
Go
Raw Normal View History

2015-01-14 02:31:27 +03:00
package main
import (
2015-01-31 18:49:21 +03:00
log "github.com/golang/glog"
2015-11-20 19:17:33 +03: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 14:54:36 +03:00
"golang.org/x/net/context"
2015-01-14 02:31:27 +03:00
)
2015-12-02 14:54:36 +03:00
func logWrapper(fn server.HandlerFunc) server.HandlerFunc {
return func(ctx context.Context, req interface{}, rsp interface{}) error {
log.Infof("[Log Wrapper] Before serving request")
err := fn(ctx, req, rsp)
log.Infof("[Log Wrapper] After serving request")
return err
}
}
2015-01-14 02:31:27 +03:00
func main() {
// optionally setup command line usage
cmd.Init()
2015-12-02 14:54:36 +03:00
server.DefaultServer = server.NewServer(
server.WrapHandler(logWrapper),
)
2015-11-27 03:17:36 +03:00
2015-01-14 02:31:27 +03:00
// Initialise Server
2015-05-27 00:39:48 +03:00
server.Init(
server.Name("go.micro.srv.example"),
)
2015-01-14 02:31:27 +03:00
// Register Handlers
server.Handle(
server.NewHandler(
2015-01-14 02:31:27 +03:00
new(handler.Example),
),
)
// Register Subscribers
if err := server.Subscribe(
server.NewSubscriber(
"topic.go.micro.srv.example",
new(subscriber.Example),
),
); err != nil {
log.Fatal(err)
}
if err := server.Subscribe(
server.NewSubscriber(
"topic.go.micro.srv.example",
subscriber.Handler,
),
); err != nil {
log.Fatal(err)
}
2015-01-14 02:31:27 +03:00
// Run server
if err := server.Run(); err != nil {
log.Fatal(err)
}
}