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