|
| 1 | +// Copyright (c) 2022 Gitpod GmbH. All rights reserved. |
| 2 | +// Licensed under the GNU Affero General Public License (AGPL). |
| 3 | +// See License-AGPL.txt in the project root for license information. |
| 4 | + |
| 5 | +package server |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "github.com/gitpod-io/gitpod/common-go/baseserver" |
| 10 | + "github.com/gitpod-io/gitpod/common-go/log" |
| 11 | + "github.com/gitpod-io/gitpod/usage/pkg/controller" |
| 12 | + "github.com/gitpod-io/gitpod/usage/pkg/db" |
| 13 | + "github.com/gitpod-io/gitpod/usage/pkg/stripe" |
| 14 | + "net" |
| 15 | + "os" |
| 16 | + "time" |
| 17 | +) |
| 18 | + |
| 19 | +type Config struct { |
| 20 | + // ControllerSchedule determines how frequently to run the Usage/Billing controller |
| 21 | + ControllerSchedule time.Duration `json:"controllerSchedule,omitempty"` |
| 22 | + |
| 23 | + StripeCredentialsFile string `json:"stripeCredentialsFile,omitempty"` |
| 24 | + |
| 25 | + Server *baseserver.Configuration `json:"server,omitempty"` |
| 26 | +} |
| 27 | + |
| 28 | +func Start(cfg Config) error { |
| 29 | + log.WithField("config", cfg).Info("Starting usage component.") |
| 30 | + |
| 31 | + conn, err := db.Connect(db.ConnectionParams{ |
| 32 | + User: os.Getenv("DB_USERNAME"), |
| 33 | + Password: os.Getenv("DB_PASSWORD"), |
| 34 | + Host: net.JoinHostPort(os.Getenv("DB_HOST"), os.Getenv("DB_PORT")), |
| 35 | + Database: "gitpod", |
| 36 | + }) |
| 37 | + if err != nil { |
| 38 | + return fmt.Errorf("failed to establish database connection: %w", err) |
| 39 | + } |
| 40 | + |
| 41 | + var billingController controller.BillingController = &controller.NoOpBillingController{} |
| 42 | + |
| 43 | + if cfg.StripeCredentialsFile != "" { |
| 44 | + config, err := stripe.ReadConfigFromFile(cfg.StripeCredentialsFile) |
| 45 | + if err != nil { |
| 46 | + return fmt.Errorf("failed to load stripe credentials: %w", err) |
| 47 | + } |
| 48 | + |
| 49 | + c, err := stripe.New(config) |
| 50 | + if err != nil { |
| 51 | + return fmt.Errorf("failed to initialize stripe client: %w", err) |
| 52 | + } |
| 53 | + billingController = controller.NewStripeBillingController(c, controller.DefaultWorkspacePricer) |
| 54 | + } |
| 55 | + |
| 56 | + ctrl, err := controller.New(cfg.ControllerSchedule, controller.NewUsageReconciler(conn, billingController)) |
| 57 | + if err != nil { |
| 58 | + return fmt.Errorf("failed to initialize usage controller: %w", err) |
| 59 | + } |
| 60 | + |
| 61 | + err = ctrl.Start() |
| 62 | + if err != nil { |
| 63 | + return fmt.Errorf("failed to start usage controller: %w", err) |
| 64 | + } |
| 65 | + defer ctrl.Stop() |
| 66 | + |
| 67 | + var serverOpts []baseserver.Option |
| 68 | + if cfg.Server != nil { |
| 69 | + serverOpts = append(serverOpts, baseserver.WithConfig(cfg.Server)) |
| 70 | + } |
| 71 | + srv, err := baseserver.New("usage", serverOpts...) |
| 72 | + if err != nil { |
| 73 | + return fmt.Errorf("failed to initialize usage server: %w", err) |
| 74 | + } |
| 75 | + |
| 76 | + err = controller.RegisterMetrics(srv.MetricsRegistry()) |
| 77 | + if err != nil { |
| 78 | + return fmt.Errorf("failed to register controller metrics: %w", err) |
| 79 | + } |
| 80 | + |
| 81 | + err = srv.ListenAndServe() |
| 82 | + if err != nil { |
| 83 | + return fmt.Errorf("failed to listen and server: %w", err) |
| 84 | + } |
| 85 | + |
| 86 | + return nil |
| 87 | +} |
0 commit comments