2020-03-24 14:15:01 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-06-22 13:51:44 +02:00
|
|
|
"github.com/caos/zitadel/internal/config/systemdefaults"
|
2020-03-24 14:15:01 +01:00
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/caos/logging"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/api/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultGrpcPort = "80"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Server interface {
|
|
|
|
GRPCPort() string
|
2020-06-22 13:51:44 +02:00
|
|
|
GRPCServer(defaults systemdefaults.SystemDefaults) (*grpc.Server, error)
|
2020-03-24 14:15:01 +01:00
|
|
|
}
|
|
|
|
|
2020-06-22 13:51:44 +02:00
|
|
|
func StartServer(ctx context.Context, s Server, defaults systemdefaults.SystemDefaults) {
|
2020-03-24 14:15:01 +01:00
|
|
|
port := grpcPort(s.GRPCPort())
|
|
|
|
listener := http.CreateListener(port)
|
2020-06-22 13:51:44 +02:00
|
|
|
server := createGrpcServer(s, defaults)
|
2020-03-24 14:15:01 +01:00
|
|
|
serveServer(ctx, server, listener, port)
|
|
|
|
}
|
|
|
|
|
2020-06-22 13:51:44 +02:00
|
|
|
func createGrpcServer(s Server, defaults systemdefaults.SystemDefaults) *grpc.Server {
|
|
|
|
grpcServer, err := s.GRPCServer(defaults)
|
2020-03-24 14:15:01 +01:00
|
|
|
logging.Log("SERVE-k280HZ").OnError(err).Panic("failed to create grpc server")
|
|
|
|
return grpcServer
|
|
|
|
}
|
|
|
|
|
|
|
|
func serveServer(ctx context.Context, server *grpc.Server, listener net.Listener, port string) {
|
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
server.GracefulStop()
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
err := server.Serve(listener)
|
|
|
|
logging.Log("SERVE-Ga3e94").OnError(err).Panic("grpc server serve failed")
|
|
|
|
}()
|
|
|
|
logging.LogWithFields("SERVE-bZ44QM", "port", port).Info("grpc server is listening")
|
|
|
|
}
|
|
|
|
|
|
|
|
func grpcPort(port string) string {
|
|
|
|
if port == "" {
|
|
|
|
return defaultGrpcPort
|
|
|
|
}
|
|
|
|
return port
|
|
|
|
}
|