diff --git a/client.go b/client.go index 7938fac..4e683ec 100644 --- a/client.go +++ b/client.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "net/http" - "os" "time" "github.com/Yiling-J/theine-go" @@ -26,11 +25,7 @@ type FlagClient struct { duration time.Duration } -func NewFlagClient() *FlagClient { - flagsURL := os.Getenv("flagsheet_URL") - if flagsURL == "" { - panic("flagsheet_URL env var must be set") - } +func NewFlagClient(flagsURL string) *FlagClient { flagsClient := flagsheetv1connect.NewFlagSheetServiceClient(http.DefaultClient, flagsURL) cache, err := theine.NewBuilder[flagQuery, string](1024).Build() if err != nil { diff --git a/cmd/server/main.go b/cmd/server/main.go index 826e1ab..19510ab 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -2,7 +2,6 @@ package main import ( "context" - "encoding/json" "net/http" "os" "time" @@ -10,8 +9,6 @@ import ( "github.com/bufbuild/connect-go" "golang.org/x/net/http2" "golang.org/x/net/http2/h2c" - "golang.org/x/oauth2/google" - "gopkg.in/Iwark/spreadsheet.v2" grpchealth "github.com/bufbuild/connect-grpchealth-go" "github.com/stillmatic/flagsheet" @@ -49,37 +46,16 @@ func (s *FlagSheetServer) Evaluate( func ok(_ http.ResponseWriter, _ *http.Request) {} func main() { - // copy these from client_secret.json - if os.Getenv("GCP_PROJECT_ID") == "" { - panic("GCP_PROJECT_ID env var must be set") - } - serviceAccountJSON := map[string]interface{}{ - "type": "service_account", - "project_id": os.Getenv("GCP_PROJECT_ID"), - "private_key_id": os.Getenv("GCP_PRIVATE_KEY_ID"), - "private_key": os.Getenv("GCP_PRIVATE_KEY"), - "client_email": os.Getenv("GCP_CLIENT_EMAIL"), - "client_id": os.Getenv("GCP_CLIENT_ID"), - "auth_uri": os.Getenv("GCP_AUTH_URI"), - "token_uri": os.Getenv("GCP_TOKEN_URI"), - "auth_provider_x509_cert_url": os.Getenv("GCP_AUTH_PROVIDER_CERT_URL"), - "client_x509_cert_url": os.Getenv("GCP_CLIENT_CERT_URL"), - } - serviceAccountJSONBytes, err := json.Marshal(serviceAccountJSON) - if err != nil { - panic(err) - } - conf, err := google.JWTConfigFromJSON(serviceAccountJSONBytes, spreadsheet.Scope) + service, err := flagsheet.NewSpreadsheetServiceFromEnv(context.Background()) if err != nil { panic(err) } + spreadsheetID := os.Getenv("SPREADSHEET_ID") if spreadsheetID == "" { panic("SPREADSHEET_ID env var must be set") } - client := conf.Client(context.Background()) - service := spreadsheet.NewServiceWithClient(client) fs, err := flagsheet.NewFlagSheet(service, spreadsheetID, 10*time.Second) if err != nil { panic(err) diff --git a/featuresheet.go b/featuresheet.go index 2275ff6..42c2e44 100644 --- a/featuresheet.go +++ b/featuresheet.go @@ -2,15 +2,19 @@ package flagsheet import ( "bytes" + "context" + "encoding/json" "fmt" "log" "math/rand" + "os" "runtime" "strconv" "sync" "time" "github.com/datadog/mmh3" + "golang.org/x/oauth2/google" "gopkg.in/Iwark/spreadsheet.v2" ) @@ -234,3 +238,35 @@ func NewFlagSheet(service *spreadsheet.Service, sheetID string, duration time.Du return FS, nil } + +func NewSpreadsheetServiceFromEnv(ctx context.Context) (*spreadsheet.Service, error) { + if os.Getenv("GCP_PROJECT_ID") == "" { + return nil, fmt.Errorf("GCP_PROJECT_ID not set") + } + // copy these from client_secret.json + serviceAccountJSON := map[string]interface{}{ + "type": "service_account", + "project_id": os.Getenv("GCP_PROJECT_ID"), + "private_key_id": os.Getenv("GCP_PRIVATE_KEY_ID"), + "private_key": os.Getenv("GCP_PRIVATE_KEY"), + "client_email": os.Getenv("GCP_CLIENT_EMAIL"), + "client_id": os.Getenv("GCP_CLIENT_ID"), + "auth_uri": os.Getenv("GCP_AUTH_URI"), + "token_uri": os.Getenv("GCP_TOKEN_URI"), + "auth_provider_x509_cert_url": os.Getenv("GCP_AUTH_PROVIDER_CERT_URL"), + "client_x509_cert_url": os.Getenv("GCP_CLIENT_CERT_URL"), + } + serviceAccountJSONBytes, err := json.Marshal(serviceAccountJSON) + if err != nil { + return nil, err + } + conf, err := google.JWTConfigFromJSON(serviceAccountJSONBytes, spreadsheet.Scope) + if err != nil { + return nil, err + } + client := conf.Client(ctx) + + service := spreadsheet.NewServiceWithClient(client) + + return service, nil +}