Path: blob/main/components/common-go/baseserver/options.go
2498 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package baseserver56import (7"context"8"fmt"9"time"1011"github.com/gitpod-io/gitpod/common-go/log"12"github.com/heptiolabs/healthcheck"13"github.com/prometheus/client_golang/prometheus"14"github.com/sirupsen/logrus"15"google.golang.org/grpc/health/grpc_health_v1"16)1718type options struct {19logger *logrus.Entry2021// version is the version of this application22version string2324config *Configuration2526// closeTimeout is the amount we allow for the server to shut down cleanly27closeTimeout time.Duration2829underTest bool3031// metricsRegistry configures the metrics registry to use for exporting metrics. When not set, the default prometheus registry is used.32metricsRegistry *prometheus.Registry3334healthHandler healthcheck.Handler3536grpcHealthCheck grpc_health_v1.HealthServer37}3839func defaultOptions() *options {40return &options{41logger: log.New(),42version: "unknown",43config: &Configuration{44Services: ServicesConfiguration{45GRPC: nil, // disabled by default46HTTP: nil, // disabled by default47},48},4950closeTimeout: 5 * time.Second,51healthHandler: healthcheck.NewHandler(),52metricsRegistry: prometheus.NewRegistry(),53grpcHealthCheck: &GrpcHealthService{},54}55}5657type Option func(opts *options) error5859// WithConfig uses a config struct to initialise the services60func WithConfig(config *Configuration) Option {61return func(opts *options) error {62opts.config = config63return nil64}65}6667func WithVersion(version string) Option {68return func(opts *options) error {69opts.version = version70return nil71}72}7374func WithUnderTest() Option {75return func(opts *options) error {76opts.underTest = true77return nil78}79}8081// WithHTTP configures and enables the HTTP server.82func WithHTTP(cfg *ServerConfiguration) Option {83return func(opts *options) error {84opts.config.Services.HTTP = cfg85return nil86}87}8889// WithGRPC configures and enables the GRPC server.90func WithGRPC(cfg *ServerConfiguration) Option {91return func(opts *options) error {92opts.config.Services.GRPC = cfg93return nil94}95}9697func WithLogger(logger *logrus.Entry) Option {98return func(opts *options) error {99if logger == nil {100return fmt.Errorf("nil logger specified")101}102103opts.logger = logger104return nil105}106}107108func WithCloseTimeout(d time.Duration) Option {109return func(opts *options) error {110opts.closeTimeout = d111return nil112}113}114115func WithMetricsRegistry(r *prometheus.Registry) Option {116return func(opts *options) error {117if r == nil {118return fmt.Errorf("nil prometheus registry received")119}120121opts.metricsRegistry = r122return nil123}124}125126func WithHealthHandler(handler healthcheck.Handler) Option {127return func(opts *options) error {128if handler == nil {129return fmt.Errorf("nil healthcheck handler provided")130}131132opts.healthHandler = handler133return nil134}135}136137func WithGRPCHealthService(svc grpc_health_v1.HealthServer) Option {138return func(opts *options) error {139if svc == nil {140return fmt.Errorf("nil healthcheck handler provided")141}142143opts.grpcHealthCheck = svc144return nil145}146}147148func evaluateOptions(cfg *options, opts ...Option) (*options, error) {149for _, opt := range opts {150if err := opt(cfg); err != nil {151return nil, fmt.Errorf("failed to evaluate options: %w", err)152}153}154155return cfg, nil156}157158type GrpcHealthService struct {159grpc_health_v1.UnimplementedHealthServer160}161162func (g *GrpcHealthService) Check(ctx context.Context, request *grpc_health_v1.HealthCheckRequest) (*grpc_health_v1.HealthCheckResponse, error) {163return &grpc_health_v1.HealthCheckResponse{Status: grpc_health_v1.HealthCheckResponse_SERVING}, nil164}165166167