key.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package billing
  2. import (
  3. "net/http"
  4. "github.com/porter-dev/porter/api/server/handlers"
  5. "github.com/porter-dev/porter/api/server/shared"
  6. "github.com/porter-dev/porter/api/server/shared/config"
  7. "github.com/porter-dev/porter/api/types"
  8. "github.com/porter-dev/porter/internal/models"
  9. "github.com/porter-dev/porter/internal/telemetry"
  10. )
  11. // GetPublishableKeyHandler will return the configured publishable key
  12. type GetPublishableKeyHandler struct {
  13. handlers.PorterHandlerReadWriter
  14. }
  15. // NewGetPublishableKeyHandler will return the publishable key
  16. func NewGetPublishableKeyHandler(
  17. config *config.Config,
  18. decoderValidator shared.RequestDecoderValidator,
  19. writer shared.ResultWriter,
  20. ) *GetPublishableKeyHandler {
  21. return &GetPublishableKeyHandler{
  22. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  23. }
  24. }
  25. func (c *GetPublishableKeyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  26. ctx, span := telemetry.NewSpan(r.Context(), "serve-get-publishable-key")
  27. defer span.End()
  28. proj, _ := ctx.Value(types.ProjectScope).(*models.Project)
  29. // There is no easy way to pass environment variables to the frontend,
  30. // so for now pass via the backend. This is acceptable because the key is
  31. // meant to be public
  32. publishableKey := c.Config().BillingManager.StripeClient.GetPublishableKey(ctx)
  33. telemetry.WithAttributes(span,
  34. telemetry.AttributeKV{Key: "project-id", Value: proj.ID},
  35. telemetry.AttributeKV{Key: "customer-id", Value: proj.BillingID},
  36. )
  37. c.WriteResult(w, r, publishableKey)
  38. }