2019-10-24 00:31:36 +03:00
|
|
|
package cloudflare
|
|
|
|
|
|
|
|
import (
|
2019-12-16 17:38:51 +03:00
|
|
|
"context"
|
2020-03-03 16:15:26 +03:00
|
|
|
"time"
|
2019-12-16 17:38:51 +03:00
|
|
|
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/store"
|
2019-10-24 00:31:36 +03:00
|
|
|
)
|
|
|
|
|
2019-12-16 17:38:51 +03:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2019-10-24 00:31:36 +03:00
|
|
|
// Token sets the cloudflare api token
|
2019-12-16 17:38:51 +03:00
|
|
|
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)
|
|
|
|
}
|
2019-10-24 00:31:36 +03:00
|
|
|
}
|
|
|
|
|
2019-10-24 01:12:45 +03:00
|
|
|
// Account sets the cloudflare account id
|
2019-12-16 17:38:51 +03:00
|
|
|
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)
|
|
|
|
}
|
2019-10-24 00:31:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Namespace sets the KV namespace
|
2019-12-16 17:38:51 +03:00
|
|
|
func Namespace(ns string) store.Option {
|
|
|
|
return func(o *store.Options) {
|
2020-01-08 15:11:31 +03:00
|
|
|
o.Namespace = ns
|
2019-12-16 17:38:51 +03:00
|
|
|
}
|
2019-10-24 00:31:36 +03:00
|
|
|
}
|
2020-03-02 21:14:25 +03:00
|
|
|
|
|
|
|
// CacheTTL sets the timeout in nanoseconds of the read/write cache
|
2020-03-03 16:15:26 +03:00
|
|
|
func CacheTTL(ttl time.Duration) store.Option {
|
2020-03-02 21:14:25 +03:00
|
|
|
return func(o *store.Options) {
|
|
|
|
if o.Context == nil {
|
|
|
|
o.Context = context.Background()
|
|
|
|
}
|
|
|
|
o.Context = context.WithValue(o.Context, "STORE_CACHE_TTL", ttl)
|
|
|
|
}
|
|
|
|
}
|