mirror of
https://github.com/fiatjaf/nak.git
synced 2025-12-22 22:48:50 +00:00
automatically create hints, store, and kvstore for system always.
This commit is contained in:
33
main.go
33
main.go
@@ -2,14 +2,17 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/textproto"
|
"net/textproto"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"fiatjaf.com/nostr"
|
"fiatjaf.com/nostr"
|
||||||
|
"fiatjaf.com/nostr/eventstore/boltdb"
|
||||||
|
"fiatjaf.com/nostr/eventstore/nullstore"
|
||||||
"fiatjaf.com/nostr/sdk"
|
"fiatjaf.com/nostr/sdk"
|
||||||
|
"fiatjaf.com/nostr/sdk/hints/bbolth"
|
||||||
|
"fiatjaf.com/nostr/sdk/kvstore/bbolt"
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
@@ -44,7 +47,7 @@ var app = &cli.Command{
|
|||||||
encrypt,
|
encrypt,
|
||||||
decrypt,
|
decrypt,
|
||||||
gift,
|
gift,
|
||||||
outbox,
|
outboxCmd,
|
||||||
wallet,
|
wallet,
|
||||||
mcpServer,
|
mcpServer,
|
||||||
curl,
|
curl,
|
||||||
@@ -64,7 +67,7 @@ var app = &cli.Command{
|
|||||||
if home, err := os.UserHomeDir(); err == nil {
|
if home, err := os.UserHomeDir(); err == nil {
|
||||||
return filepath.Join(home, ".config/nak")
|
return filepath.Join(home, ".config/nak")
|
||||||
} else {
|
} 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) {
|
Before: func(ctx context.Context, c *cli.Command) (context.Context, error) {
|
||||||
sys = sdk.NewSystem()
|
sys = sdk.NewSystem()
|
||||||
|
|
||||||
if err := initializeOutboxHintsDB(c, sys); err != nil {
|
configPath := c.String("config-path")
|
||||||
return ctx, fmt.Errorf("failed to initialize outbox hints: %w", err)
|
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{
|
sys.Pool = nostr.NewPool(nostr.PoolOptions{
|
||||||
|
|||||||
61
outbox.go
61
outbox.go
@@ -3,80 +3,21 @@ package main
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"fiatjaf.com/nostr/sdk"
|
|
||||||
"fiatjaf.com/nostr/sdk/hints/bbolth"
|
|
||||||
"github.com/fatih/color"
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var outboxCmd = &cli.Command{
|
||||||
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{
|
|
||||||
Name: "outbox",
|
Name: "outbox",
|
||||||
Usage: "manage outbox relay hints database",
|
Usage: "manage outbox relay hints database",
|
||||||
DisableSliceFlagSeparator: true,
|
DisableSliceFlagSeparator: true,
|
||||||
Commands: []*cli.Command{
|
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",
|
Name: "list",
|
||||||
Usage: "list outbox relays for a given pubkey",
|
Usage: "list outbox relays for a given pubkey",
|
||||||
ArgsUsage: "<pubkey>",
|
ArgsUsage: "<pubkey>",
|
||||||
DisableSliceFlagSeparator: true,
|
DisableSliceFlagSeparator: true,
|
||||||
Action: func(ctx context.Context, c *cli.Command) error {
|
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 {
|
if c.Args().Len() != 1 {
|
||||||
return fmt.Errorf("expected exactly one argument (pubkey)")
|
return fmt.Errorf("expected exactly one argument (pubkey)")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user