protoc-gen-go-micro/examples/go-kit/main.go

94 lines
3.1 KiB
Go
Raw Normal View History

2016-12-13 18:03:16 +03:00
package main
import (
"fmt"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/go-kit/kit/log"
"github.com/gorilla/handlers"
"google.golang.org/grpc"
session_svc "moul.io/protoc-gen-gotemplate/examples/go-kit/services/session"
session_endpoints "moul.io/protoc-gen-gotemplate/examples/go-kit/services/session/gen/endpoints"
session_pb "moul.io/protoc-gen-gotemplate/examples/go-kit/services/session/gen/pb"
session_grpctransport "moul.io/protoc-gen-gotemplate/examples/go-kit/services/session/gen/transports/grpc"
session_httptransport "moul.io/protoc-gen-gotemplate/examples/go-kit/services/session/gen/transports/http"
sprint_svc "moul.io/protoc-gen-gotemplate/examples/go-kit/services/sprint"
sprint_endpoints "moul.io/protoc-gen-gotemplate/examples/go-kit/services/sprint/gen/endpoints"
sprint_pb "moul.io/protoc-gen-gotemplate/examples/go-kit/services/sprint/gen/pb"
sprint_grpctransport "moul.io/protoc-gen-gotemplate/examples/go-kit/services/sprint/gen/transports/grpc"
sprint_httptransport "moul.io/protoc-gen-gotemplate/examples/go-kit/services/sprint/gen/transports/http"
2016-12-13 19:18:55 +03:00
user_svc "moul.io/protoc-gen-gotemplate/examples/go-kit/services/user"
user_endpoints "moul.io/protoc-gen-gotemplate/examples/go-kit/services/user/gen/endpoints"
user_pb "moul.io/protoc-gen-gotemplate/examples/go-kit/services/user/gen/pb"
user_grpctransport "moul.io/protoc-gen-gotemplate/examples/go-kit/services/user/gen/transports/grpc"
user_httptransport "moul.io/protoc-gen-gotemplate/examples/go-kit/services/user/gen/transports/http"
2016-12-13 18:03:16 +03:00
)
func main() {
mux := http.NewServeMux()
errc := make(chan error)
s := grpc.NewServer()
var logger log.Logger
{
logger = log.NewLogfmtLogger(os.Stdout)
2017-03-16 18:16:45 +03:00
logger = log.With(logger, "ts", log.DefaultTimestampUTC)
logger = log.With(logger, "caller", log.DefaultCaller)
2016-12-13 18:03:16 +03:00
}
// initialize services
{
svc := session_svc.New()
2016-12-13 18:03:16 +03:00
endpoints := session_endpoints.MakeEndpoints(svc)
2017-03-16 18:16:45 +03:00
srv := session_grpctransport.MakeGRPCServer(endpoints)
2016-12-13 18:03:16 +03:00
session_pb.RegisterSessionServiceServer(s, srv)
2017-03-16 18:16:45 +03:00
session_httptransport.RegisterHandlers(svc, mux, endpoints)
2016-12-13 18:03:16 +03:00
}
{
svc := sprint_svc.New()
endpoints := sprint_endpoints.MakeEndpoints(svc)
2017-03-16 18:16:45 +03:00
srv := sprint_grpctransport.MakeGRPCServer(endpoints)
sprint_pb.RegisterSprintServiceServer(s, srv)
2017-03-16 18:16:45 +03:00
sprint_httptransport.RegisterHandlers(svc, mux, endpoints)
2016-12-13 18:03:16 +03:00
}
2016-12-13 19:18:55 +03:00
{
svc := user_svc.New()
endpoints := user_endpoints.MakeEndpoints(svc)
2017-03-16 18:16:45 +03:00
srv := user_grpctransport.MakeGRPCServer(endpoints)
2016-12-13 19:18:55 +03:00
user_pb.RegisterUserServiceServer(s, srv)
2017-03-16 18:16:45 +03:00
user_httptransport.RegisterHandlers(svc, mux, endpoints)
2016-12-13 19:18:55 +03:00
}
2016-12-13 18:03:16 +03:00
// start servers
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
errc <- fmt.Errorf("%s", <-c)
}()
go func() {
2017-03-16 18:16:45 +03:00
logger := log.With(logger, "transport", "HTTP")
2016-12-13 18:03:16 +03:00
logger.Log("addr", ":8000")
errc <- http.ListenAndServe(":8000", handlers.LoggingHandler(os.Stderr, mux))
}()
go func() {
2017-03-16 18:16:45 +03:00
logger := log.With(logger, "transport", "gRPC")
2016-12-13 18:03:16 +03:00
ln, err := net.Listen("tcp", ":9000")
if err != nil {
errc <- err
return
}
logger.Log("addr", ":9000")
errc <- s.Serve(ln)
}()
logger.Log("exit", <-errc)
}