From 1b7f3162b5254454893180c62188b6bf6bcfe773 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Sun, 21 Dec 2025 21:58:15 -0300 Subject: [PATCH] automatically create hints, store, and kvstore for system always. --- main.go | 33 +++++++++++++++++++++++++----- outbox.go | 61 +------------------------------------------------------ 2 files changed, 29 insertions(+), 65 deletions(-) diff --git a/main.go b/main.go index 2ed12c3..e0124c1 100644 --- a/main.go +++ b/main.go @@ -2,14 +2,17 @@ package main import ( "context" - "fmt" "net/http" "net/textproto" "os" "path/filepath" "fiatjaf.com/nostr" + "fiatjaf.com/nostr/eventstore/boltdb" + "fiatjaf.com/nostr/eventstore/nullstore" "fiatjaf.com/nostr/sdk" + "fiatjaf.com/nostr/sdk/hints/bbolth" + "fiatjaf.com/nostr/sdk/kvstore/bbolt" "github.com/fatih/color" "github.com/urfave/cli/v3" ) @@ -44,7 +47,7 @@ var app = &cli.Command{ encrypt, decrypt, gift, - outbox, + outboxCmd, wallet, mcpServer, curl, @@ -64,7 +67,7 @@ var app = &cli.Command{ if home, err := os.UserHomeDir(); err == nil { return filepath.Join(home, ".config/nak") } else { - return filepath.Join("/dev/null") + return "" } })(), }, @@ -100,8 +103,28 @@ var app = &cli.Command{ Before: func(ctx context.Context, c *cli.Command) (context.Context, error) { sys = sdk.NewSystem() - if err := initializeOutboxHintsDB(c, sys); err != nil { - return ctx, fmt.Errorf("failed to initialize outbox hints: %w", err) + configPath := c.String("config-path") + if configPath != "" { + os.MkdirAll(filepath.Join("outbox"), 0755) + hintsFilePath := filepath.Join(configPath, "outbox/hints.db") + _, err := bbolth.NewBoltHints(hintsFilePath) + if err != nil { + log("failed to create bolt hints db at '%s': %s\n", hintsFilePath, err) + } + + eventsPath := filepath.Join(configPath, "events") + sys.Store = &boltdb.BoltBackend{Path: eventsPath} + if err := sys.Store.Init(); err != nil { + log("failed to create boltdb events db at '%s': %s\n", eventsPath, err) + sys.Store = &nullstore.NullStore{} + } + + kvPath := filepath.Join(configPath, "kvstore") + if kv, err := bbolt.NewStore(kvPath); err != nil { + log("failed to create boltdb kvstore db at '%s': %s\n", kvPath, err) + } else { + sys.KVStore = kv + } } sys.Pool = nostr.NewPool(nostr.PoolOptions{ diff --git a/outbox.go b/outbox.go index 82fc775..9d95329 100644 --- a/outbox.go +++ b/outbox.go @@ -3,80 +3,21 @@ package main import ( "context" "fmt" - "os" - "path/filepath" - "fiatjaf.com/nostr/sdk" - "fiatjaf.com/nostr/sdk/hints/bbolth" - "github.com/fatih/color" "github.com/urfave/cli/v3" ) -var ( - hintsFilePath string - hintsFileExists bool -) - -func initializeOutboxHintsDB(c *cli.Command, sys *sdk.System) error { - configPath := c.String("config-path") - if configPath != "" { - hintsFilePath = filepath.Join(configPath, "outbox/hints.db") - } - if hintsFilePath != "" { - if _, err := os.Stat(hintsFilePath); err == nil { - hintsFileExists = true - } else if !os.IsNotExist(err) { - return err - } - } - if hintsFileExists && hintsFilePath != "" { - hintsdb, err := bbolth.NewBoltHints(hintsFilePath) - if err == nil { - sys.Hints = hintsdb - } - } - - return nil -} - -var outbox = &cli.Command{ +var outboxCmd = &cli.Command{ Name: "outbox", Usage: "manage outbox relay hints database", DisableSliceFlagSeparator: true, Commands: []*cli.Command{ - { - Name: "init", - Usage: "initialize the outbox hints database", - DisableSliceFlagSeparator: true, - Action: func(ctx context.Context, c *cli.Command) error { - if hintsFileExists { - return nil - } - if hintsFilePath == "" { - return fmt.Errorf("couldn't find a place to store the hints, pass --config-path to fix.") - } - - os.MkdirAll(hintsFilePath, 0755) - _, err := bbolth.NewBoltHints(hintsFilePath) - if err != nil { - return fmt.Errorf("failed to create bolt hints db at '%s': %w", hintsFilePath, err) - } - - log("initialized hints database at %s\n", hintsFilePath) - return nil - }, - }, { Name: "list", Usage: "list outbox relays for a given pubkey", ArgsUsage: "", DisableSliceFlagSeparator: true, Action: func(ctx context.Context, c *cli.Command) error { - if !hintsFileExists { - log(color.YellowString("running with temporary fragile data.\n")) - log(color.YellowString("call `nak outbox init` to setup persistence.\n")) - } - if c.Args().Len() != 1 { return fmt.Errorf("expected exactly one argument (pubkey)") }