2020-03-24 13:15:01 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/caos/logging"
|
2020-07-08 11:56:37 +00:00
|
|
|
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
|
|
|
|
"golang.org/x/text/language"
|
2020-03-24 13:15:01 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
"github.com/caos/zitadel/internal/api/authz"
|
|
|
|
"github.com/caos/zitadel/internal/api/grpc/server/middleware"
|
2020-03-24 13:15:01 +00:00
|
|
|
"github.com/caos/zitadel/internal/api/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultGrpcPort = "80"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Server interface {
|
2020-07-08 11:56:37 +00:00
|
|
|
Gateway
|
|
|
|
RegisterServer(*grpc.Server)
|
|
|
|
AppName() string
|
|
|
|
MethodPrefix() string
|
|
|
|
AuthMethods() authz.MethodMapping
|
2020-03-24 13:15:01 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func CreateServer(verifier *authz.TokenVerifier, authConfig authz.Config, lang language.Tag) *grpc.Server {
|
|
|
|
return grpc.NewServer(
|
|
|
|
middleware.TracingStatsServer(http.Healthz, http.Readiness, http.Validation),
|
|
|
|
grpc.UnaryInterceptor(
|
|
|
|
grpc_middleware.ChainUnaryServer(
|
|
|
|
middleware.ErrorHandler(lang),
|
|
|
|
middleware.TranslationHandler(lang),
|
|
|
|
grpc_middleware.ChainUnaryServer(
|
|
|
|
middleware.AuthorizationInterceptor(verifier, authConfig),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
2020-03-24 13:15:01 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func Serve(ctx context.Context, server *grpc.Server, port string) {
|
2020-03-24 13:15:01 +00:00
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
server.GracefulStop()
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
2020-07-08 11:56:37 +00:00
|
|
|
listener := http.CreateListener(port)
|
2020-03-24 13:15:01 +00:00
|
|
|
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
|
|
|
|
}
|