More cleanup of store cf

This commit is contained in:
Asim Aslam 2019-10-23 23:10:44 +01:00
parent 68d0efbeaa
commit 232c8ac7a1

View File

@ -13,6 +13,7 @@ import (
"math" "math"
"net/http" "net/http"
"net/url" "net/url"
"os"
"strconv" "strconv"
"time" "time"
@ -73,6 +74,29 @@ type apiMessage struct {
Message string `json:"message"` Message string `json:"message"`
} }
// getOptions returns account id, token and namespace
func getOptions() (string, string, string) {
accountID := os.Getenv("CF_ACCOUNT_ID")
apiToken := os.Getenv("CF_API_TOKEN")
namespace := os.Getenv("KV_NAMESPACE_ID")
return accountID, apiToken, namespace
}
func validateOptions(account, token, namespace string) {
if len(account) == 0 {
log.Fatal("Store: CF_ACCOUNT_ID is blank")
}
if len(token) == 0 {
log.Fatal("Store: CF_API_TOKEN is blank")
}
if len(namespace) == 0 {
log.Fatal("Store: KV_NAMESPACE_ID is blank")
}
}
// In the cloudflare workers KV implemention, List() doesn't guarantee // In the cloudflare workers KV implemention, List() doesn't guarantee
// anything as the workers API is eventually consistent. // anything as the workers API is eventually consistent.
func (w *workersKV) List() ([]*store.Record, error) { func (w *workersKV) List() ([]*store.Record, error) {
@ -257,39 +281,48 @@ func (w *workersKV) request(ctx context.Context, method, path string, body inter
} }
// New returns a cloudflare Store implementation. // New returns a cloudflare Store implementation.
// Options expects CF_API_TOKEN to a cloudflare API token scoped to Workers KV, // Account ID, Token and Namespace must either be passed as options or
// CF_ACCOUNT_ID to contain a string with your cloudflare account ID and // environment variables. If set as env vars we expect the following;
// CF_API_TOKEN to a cloudflare API token scoped to Workers KV.
// CF_ACCOUNT_ID to contain a string with your cloudflare account ID.
// KV_NAMESPACE_ID to contain the namespace UUID for your KV storage. // KV_NAMESPACE_ID to contain the namespace UUID for your KV storage.
func NewStore(opts ...options.Option) store.Store { func NewStore(opts ...options.Option) store.Store {
// Validate Options // create new Options
options := options.NewOptions(opts...) options := options.NewOptions(opts...)
var account, token, namespace string // get values from the environment
account, token, namespace := getOptions()
apiToken, ok := options.Values().Get("CF_API_TOKEN") // set api token from options if exists
if !ok { if apiToken, ok := options.Values().Get("CF_API_TOKEN"); ok {
log.Fatal("Store: No CF_API_TOKEN passed as an option") tk, ok := apiToken.(string)
} if !ok {
if token, ok = apiToken.(string); !ok { log.Fatal("Store: Option CF_API_TOKEN contains a non-string")
log.Fatal("Store: Option CF_API_TOKEN contains a non-string") }
token = tk
} }
accountID, ok := options.Values().Get("CF_ACCOUNT_ID") // set account id from options if exists
if !ok { if accountID, ok := options.Values().Get("CF_ACCOUNT_ID"); ok {
log.Fatal("Store: No CF_ACCOUNT_ID passed as an option") id, ok := accountID.(string)
} if !ok {
if account, ok = accountID.(string); !ok { log.Fatal("Store: Option CF_ACCOUNT_ID contains a non-string")
log.Fatal("Store: Option CF_ACCOUNT_ID contains a non-string") }
account = id
} }
uuid, ok := options.Values().Get("KV_NAMESPACE_ID") // set namespace from options if exists
if !ok { if uuid, ok := options.Values().Get("KV_NAMESPACE_ID"); ok {
log.Fatal("Store: No KV_NAMESPACE_ID passed as an option") ns, ok := uuid.(string)
} if !ok {
if namespace, ok = uuid.(string); !ok { log.Fatal("Store: Option KV_NAMESPACE_ID contains a non-string")
log.Fatal("Store: Option KV_NAMESPACE_ID contains a non-string") }
namespace = ns
} }
// validate options are not blank or log.Fatal
validateOptions(account, token, namespace)
return &workersKV{ return &workersKV{
account: account, account: account,
namespace: namespace, namespace: namespace,