change store options
This commit is contained in:
@@ -17,7 +17,6 @@ import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/micro/go-micro/config/options"
|
||||
"github.com/micro/go-micro/store"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
@@ -27,7 +26,7 @@ const (
|
||||
)
|
||||
|
||||
type workersKV struct {
|
||||
options.Options
|
||||
options store.Options
|
||||
// cf account id
|
||||
account string
|
||||
// cf api token
|
||||
@@ -291,38 +290,25 @@ func (w *workersKV) request(ctx context.Context, method, path string, body inter
|
||||
// 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.
|
||||
func NewStore(opts ...options.Option) store.Store {
|
||||
// create new Options
|
||||
options := options.NewOptions(opts...)
|
||||
func NewStore(opts ...store.Option) store.Store {
|
||||
var options store.Options
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
|
||||
// get values from the environment
|
||||
// get options from environment
|
||||
account, token, namespace := getOptions()
|
||||
|
||||
// set api token from options if exists
|
||||
if apiToken, ok := options.Values().Get("CF_API_TOKEN"); ok {
|
||||
tk, ok := apiToken.(string)
|
||||
if !ok {
|
||||
log.Fatal("Store: Option CF_API_TOKEN contains a non-string")
|
||||
}
|
||||
token = tk
|
||||
if len(account) == 0 {
|
||||
account = getAccount(options.Context)
|
||||
}
|
||||
|
||||
// set account id from options if exists
|
||||
if accountID, ok := options.Values().Get("CF_ACCOUNT_ID"); ok {
|
||||
id, ok := accountID.(string)
|
||||
if !ok {
|
||||
log.Fatal("Store: Option CF_ACCOUNT_ID contains a non-string")
|
||||
}
|
||||
account = id
|
||||
if len(token) == 0 {
|
||||
token = getToken(options.Context)
|
||||
}
|
||||
|
||||
// set namespace from options if exists
|
||||
if uuid, ok := options.Values().Get("KV_NAMESPACE_ID"); ok {
|
||||
ns, ok := uuid.(string)
|
||||
if !ok {
|
||||
log.Fatal("Store: Option KV_NAMESPACE_ID contains a non-string")
|
||||
}
|
||||
namespace = ns
|
||||
if len(namespace) == 0 {
|
||||
namespace = getNamespace(options.Context)
|
||||
}
|
||||
|
||||
// validate options are not blank or log.Fatal
|
||||
@@ -332,7 +318,7 @@ func NewStore(opts ...options.Option) store.Store {
|
||||
account: account,
|
||||
namespace: namespace,
|
||||
token: token,
|
||||
Options: options,
|
||||
options: options,
|
||||
httpClient: &http.Client{},
|
||||
}
|
||||
}
|
||||
|
@@ -1,23 +1,60 @@
|
||||
package cloudflare
|
||||
|
||||
import (
|
||||
"github.com/micro/go-micro/config/options"
|
||||
"context"
|
||||
|
||||
"github.com/micro/go-micro/store"
|
||||
)
|
||||
|
||||
func getOption(ctx context.Context, key string) string {
|
||||
if ctx == nil {
|
||||
return ""
|
||||
}
|
||||
val, ok := ctx.Value(key).(string)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
func getToken(ctx context.Context) string {
|
||||
return getOption(ctx, "CF_API_TOKEN")
|
||||
}
|
||||
|
||||
func getAccount(ctx context.Context) string {
|
||||
return getOption(ctx, "CF_ACCOUNT_ID")
|
||||
}
|
||||
|
||||
func getNamespace(ctx context.Context) string {
|
||||
return getOption(ctx, "KV_NAMESPACE_ID")
|
||||
}
|
||||
|
||||
// Token sets the cloudflare api token
|
||||
func Token(t string) options.Option {
|
||||
// TODO: change to store.cf.api_token
|
||||
return options.WithValue("CF_API_TOKEN", t)
|
||||
func Token(t string) store.Option {
|
||||
return func(o *store.Options) {
|
||||
if o.Context == nil {
|
||||
o.Context = context.Background()
|
||||
}
|
||||
o.Context = context.WithValue(o.Context, "CF_API_TOKEN", t)
|
||||
}
|
||||
}
|
||||
|
||||
// Account sets the cloudflare account id
|
||||
func Account(id string) options.Option {
|
||||
// TODO: change to store.cf.account_id
|
||||
return options.WithValue("CF_ACCOUNT_ID", id)
|
||||
func Account(id string) store.Option {
|
||||
return func(o *store.Options) {
|
||||
if o.Context == nil {
|
||||
o.Context = context.Background()
|
||||
}
|
||||
o.Context = context.WithValue(o.Context, "CF_ACCOUNT_ID", id)
|
||||
}
|
||||
}
|
||||
|
||||
// Namespace sets the KV namespace
|
||||
func Namespace(ns string) options.Option {
|
||||
// TODO: change to store.cf.namespace
|
||||
return options.WithValue("KV_NAMESPACE_ID", ns)
|
||||
func Namespace(ns string) store.Option {
|
||||
return func(o *store.Options) {
|
||||
if o.Context == nil {
|
||||
o.Context = context.Background()
|
||||
}
|
||||
o.Context = context.WithValue(o.Context, "KV_NAMESPACE_ID", ns)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user