Skip to content

Commit

Permalink
feat: refactor, add spreadsheetservice from envvar
Browse files Browse the repository at this point in the history
  • Loading branch information
stillmatic committed Nov 13, 2023
1 parent f1ed1ef commit bed9b1b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 32 deletions.
7 changes: 1 addition & 6 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"net/http"
"os"
"time"

"github.com/Yiling-J/theine-go"
Expand All @@ -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 {
Expand Down
28 changes: 2 additions & 26 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ package main

import (
"context"
"encoding/json"
"net/http"
"os"
"time"

"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"
Expand Down Expand Up @@ -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)
Expand Down
36 changes: 36 additions & 0 deletions featuresheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}

0 comments on commit bed9b1b

Please sign in to comment.