mirror of
https://github.com/gitpod-io/gitpod.git
synced 2025-12-08 17:36:30 +00:00
Construct and pass a billingservice client to the Stripe webhook so that it can invoke the `FinalizeInvoice` RPC when the webhook is triggered.
76 lines
2.4 KiB
Go
76 lines
2.4 KiB
Go
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
|
|
// Licensed under the GNU Affero General Public License (AGPL).
|
|
// See License-AGPL.txt in the project root for license information.
|
|
|
|
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
|
|
"github.com/gitpod-io/gitpod/public-api/config"
|
|
"github.com/gorilla/handlers"
|
|
|
|
"github.com/gitpod-io/gitpod/common-go/baseserver"
|
|
"github.com/gitpod-io/gitpod/public-api-server/pkg/apiv1"
|
|
"github.com/gitpod-io/gitpod/public-api-server/pkg/billingservice"
|
|
"github.com/gitpod-io/gitpod/public-api-server/pkg/proxy"
|
|
"github.com/gitpod-io/gitpod/public-api-server/pkg/webhooks"
|
|
v1 "github.com/gitpod-io/gitpod/public-api/v1"
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func Start(logger *logrus.Entry, cfg *config.Configuration) error {
|
|
logger.WithField("config", cfg).Info("Starting public-api.")
|
|
|
|
gitpodAPI, err := url.Parse(cfg.GitpodServiceURL)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to parse Gitpod API URL: %w", err)
|
|
}
|
|
|
|
registry := prometheus.NewRegistry()
|
|
|
|
srv, err := baseserver.New("public_api_server",
|
|
baseserver.WithLogger(logger),
|
|
baseserver.WithConfig(cfg.Server),
|
|
baseserver.WithMetricsRegistry(registry),
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to initialize public api server: %w", err)
|
|
}
|
|
|
|
var billingService billingservice.Interface = &billingservice.NoOpClient{}
|
|
if cfg.BillingServiceAddress != "" {
|
|
billingService, err = billingservice.New(cfg.BillingServiceAddress)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to initialize billing service client: %w", err)
|
|
}
|
|
}
|
|
|
|
srv.HTTPMux().Handle("/stripe/invoices/webhook",
|
|
handlers.ContentTypeHandler(webhooks.NewStripeWebhookHandler(billingService), "application/json"),
|
|
)
|
|
|
|
if registerErr := register(srv, gitpodAPI, registry); registerErr != nil {
|
|
return fmt.Errorf("failed to register services: %w", registerErr)
|
|
}
|
|
|
|
if listenErr := srv.ListenAndServe(); listenErr != nil {
|
|
return fmt.Errorf("failed to serve public api server: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func register(srv *baseserver.Server, serverAPIURL *url.URL, registry *prometheus.Registry) error {
|
|
proxy.RegisterMetrics(registry)
|
|
|
|
connPool := &proxy.NoConnectionPool{ServerAPI: serverAPIURL}
|
|
|
|
v1.RegisterWorkspacesServiceServer(srv.GRPC(), apiv1.NewWorkspaceService(connPool))
|
|
v1.RegisterPrebuildsServiceServer(srv.GRPC(), v1.UnimplementedPrebuildsServiceServer{})
|
|
|
|
return nil
|
|
}
|