fleet/server/launcher/server.go
Victor Vrantchan 5ad562a952 refactor gRPC endpoints with launcher packages in mind (#1582)
The launcher service implementation is an adapter around the TLS service.
All launcher methods that have an equivalent in TLS pass the business logic to the
TLS API.

Closes #1565
2017-10-24 19:55:32 -04:00

55 lines
1.5 KiB
Go

// Package launcher provides a gRPC server to handle launcher requests.
package launcher
import (
"net/http"
"strings"
"github.com/go-kit/kit/log"
launcher "github.com/kolide/launcher/service"
grpc "google.golang.org/grpc"
"github.com/kolide/fleet/server/health"
"github.com/kolide/fleet/server/kolide"
)
// Handler extends the grpc.Server, providing Handler that allows us to serve
// both gRPC and http traffic.
type Handler struct {
*grpc.Server
}
// New creates a gRPC server to handle remote requests from launcher.
func New(
tls kolide.OsqueryService,
logger log.Logger,
grpcServer *grpc.Server,
healthCheckers map[string]health.Checker,
) *Handler {
var svc launcher.KolideService
{
svc = &launcherWrapper{
tls: tls,
logger: logger,
healthCheckers: healthCheckers,
}
svc = launcher.LoggingMiddleware(logger)(svc)
}
endpoints := launcher.MakeServerEndpoints(svc)
server := launcher.NewGRPCServer(endpoints, logger)
launcher.RegisterGRPCServer(grpcServer, server)
return &Handler{grpcServer}
}
// Handler will route gRPC traffic to the gRPC server, other http traffic
// will be routed to normal http handler functions.
func (hgprc *Handler) Handler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.ProtoMajor == 2 && strings.Contains(r.Header.Get("Content-Type"), "application/grpc") {
hgprc.ServeHTTP(w, r)
} else {
next.ServeHTTP(w, r)
}
})
}