mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-23 05:35:58 +08:00
coreapi name: add some missing options
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
This commit is contained in:
@ -13,6 +13,10 @@ const (
|
|||||||
type NamePublishSettings struct {
|
type NamePublishSettings struct {
|
||||||
ValidTime time.Duration
|
ValidTime time.Duration
|
||||||
Key string
|
Key string
|
||||||
|
|
||||||
|
TTL *time.Duration
|
||||||
|
|
||||||
|
AllowOffline bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type NameResolveSettings struct {
|
type NameResolveSettings struct {
|
||||||
@ -29,6 +33,8 @@ func NamePublishOptions(opts ...NamePublishOption) (*NamePublishSettings, error)
|
|||||||
options := &NamePublishSettings{
|
options := &NamePublishSettings{
|
||||||
ValidTime: DefaultNameValidTime,
|
ValidTime: DefaultNameValidTime,
|
||||||
Key: "self",
|
Key: "self",
|
||||||
|
|
||||||
|
AllowOffline: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
@ -82,6 +88,24 @@ func (nameOpts) Key(key string) NamePublishOption {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AllowOffline is an option for Name.Publish which specifies whether to allow
|
||||||
|
// publishing when the node is offline. Default value is false
|
||||||
|
func (nameOpts) AllowOffline(allow bool) NamePublishOption {
|
||||||
|
return func(settings *NamePublishSettings) error {
|
||||||
|
settings.AllowOffline = allow
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TTL is an option for Name.Publish which specifies the time duration the
|
||||||
|
// published record should be cached for (caution: experimental).
|
||||||
|
func (nameOpts) TTL(ttl time.Duration) NamePublishOption {
|
||||||
|
return func(settings *NamePublishSettings) error {
|
||||||
|
settings.TTL = &ttl
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Local is an option for Name.Resolve which specifies if the lookup should be
|
// Local is an option for Name.Resolve which specifies if the lookup should be
|
||||||
// offline. Default value is false
|
// offline. Default value is false
|
||||||
func (nameOpts) Local(local bool) NameResolveOption {
|
func (nameOpts) Local(local bool) NameResolveOption {
|
||||||
|
@ -45,6 +45,9 @@ func (api *NameAPI) Publish(ctx context.Context, p coreiface.Path, opts ...caopt
|
|||||||
n := api.node
|
n := api.node
|
||||||
|
|
||||||
if !n.OnlineMode() {
|
if !n.OnlineMode() {
|
||||||
|
if !options.AllowOffline {
|
||||||
|
return nil, coreiface.ErrOffline
|
||||||
|
}
|
||||||
err := n.SetupOfflineRouting()
|
err := n.SetupOfflineRouting()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -65,6 +68,10 @@ func (api *NameAPI) Publish(ctx context.Context, p coreiface.Path, opts ...caopt
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if options.TTL != nil {
|
||||||
|
ctx = context.WithValue(ctx, "ipns-publish-ttl", *options.TTL)
|
||||||
|
}
|
||||||
|
|
||||||
eol := time.Now().Add(options.ValidTime)
|
eol := time.Now().Add(options.ValidTime)
|
||||||
err = n.Namesys.PublishWithEOL(ctx, k, pth, eol)
|
err = n.Namesys.PublishWithEOL(ctx, k, pth, eol)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Reference in New Issue
Block a user