From c19cebe537a158951730a93ae592249ef3919938 Mon Sep 17 00:00:00 2001 From: fsociety Date: Mon, 23 Sep 2024 21:10:47 +0200 Subject: [PATCH] feat: add support for relay list from json file This commit adds the ability to specify relay lists in a json file for both import and blastr settings. This provides more flexibility in configuring the application. It also includes error handling for reading and parsing the json file. --- .env.example | 2 ++ config.go | 37 +++++++++++++++++++++++++++++++++++-- relays_blastr.json | 30 ++++++++++++++++++++++++++++++ relays_import.json | 30 ++++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 relays_blastr.json create mode 100644 relays_import.json diff --git a/.env.example b/.env.example index cd5af6a..317256c 100644 --- a/.env.example +++ b/.env.example @@ -32,6 +32,7 @@ INBOX_PULL_INTERVAL_SECONDS=600 IMPORT_START_DATE="2023-01-20" IMPORT_QUERY_INTERVAL_SECONDS=600 IMPORT_SEED_RELAYS="relay.damus.io,nos.lol,relay.nostr.band,relay.snort.social,nostr.land,nostr.mom,relay.nos.social,relay.primal.net,relay.nostr.bg,no.str.cr,nostr21.com,nostrue.com,relay.siamstr.com,wot.utxo.one,nostrelites.org,wot.nostr.party,wot.sovbit.host,wot.girino.org,relay.lnau.net,wot.siamstr.com,wot.sudocarlos.com,relay.otherstuff.fyi,relay.lexingtonbitcoin.org,wot.azzamo.net,wot.swarmstr.com,zap.watch,satsage.xyz,wons.calva.dev" +IMPORT_SEED_RELAYS_FILE="" # If you want to use a json file, set this to the path of the file (e.g. "relays_import.json") ## Backup Settings BACKUP_PROVIDER="aws" @@ -45,6 +46,7 @@ AWS_BUCKET_NAME="utxo-relay-backups" ## Blastr Settings BLASTR_RELAYS="relay.damus.io,nos.lol,relay.nostr.band,relay.snort.social,nostr.land,nostr.mom,relay.nos.social,relay.primal.net,relay.nostr.bg,no.str.cr,nostr21.com,nostrue.com,relay.siamstr.com,wot.utxo.one,nostrelites.org,wot.nostr.party,wot.sovbit.host,wot.girino.org,relay.lnau.net,wot.siamstr.com,wot.sudocarlos.com,relay.otherstuff.fyi,relay.lexingtonbitcoin.org,wot.azzamo.net,wot.swarmstr.com,zap.watch,satsage.xyz,wons.calva.dev" +BLASTR_RELAYS_FILE="" # If you want to use a json file, set this to the path of the file (e.g. "relays_blastr.json") ## OPTIONAL: Docker UID and GID - should be the same as the user running the docker container DOCKER_UID=1000 diff --git a/config.go b/config.go index 8288732..4da83a5 100644 --- a/config.go +++ b/config.go @@ -5,6 +5,8 @@ import ( "os" "strconv" "strings" + "encoding/json" + "io/ioutil" "github.com/joho/godotenv" ) @@ -48,6 +50,20 @@ type AwsConfig struct { Bucket string `json:"bucket"` } +func getRelayListFromEnvOrFile(envKey, fileKey string) []string { + envValue := getEnv(envKey) + if envValue != "" { + return getRelayList(envValue) + } + + filePath := getEnv(fileKey) + if filePath != "" { + return getRelayListFromFile(filePath) + } + + return []string{} +} + func loadConfig() Config { godotenv.Load(".env") @@ -77,10 +93,10 @@ func loadConfig() Config { InboxPullIntervalSeconds: getEnvInt("INBOX_PULL_INTERVAL_SECONDS", 3600), ImportStartDate: getEnv("IMPORT_START_DATE"), ImportQueryIntervalSeconds: getEnvInt("IMPORT_QUERY_INTERVAL_SECONDS", 360000), - ImportSeedRelays: getRelayList(getEnv("IMPORT_SEED_RELAYS")), + ImportSeedRelays: getRelayListFromEnvOrFile("IMPORT_SEED_RELAYS", "IMPORT_SEED_RELAYS_FILE"), BackupProvider: getEnv("BACKUP_PROVIDER"), BackupIntervalHours: getEnvInt("BACKUP_INTERVAL_HOURS", 24), - BlastrRelays: getRelayList(getEnv("BLASTR_RELAYS")), + BlastrRelays: getRelayListFromEnvOrFile("BLASTR_RELAYS", "BLASTR_RELAYS_FILE"), } } @@ -92,6 +108,23 @@ func getRelayList(commaList string) []string { return relayList } +func getRelayListFromFile(filePath string) []string { + file, err := ioutil.ReadFile(filePath) + if err != nil { + log.Fatalf("Failed to read file: %s", err) + } + + var relayList []string + if err := json.Unmarshal(file, &relayList); err != nil { + log.Fatalf("Failed to parse JSON: %s", err) + } + + for i, relay := range relayList { + relayList[i] = "wss://" + strings.TrimSpace(relay) + } + return relayList +} + func getEnv(key string) string { value, exists := os.LookupEnv(key) if !exists { diff --git a/relays_blastr.json b/relays_blastr.json new file mode 100644 index 0000000..6476d61 --- /dev/null +++ b/relays_blastr.json @@ -0,0 +1,30 @@ +[ + "relay.damus.io", + "nos.lol", + "relay.nostr.band", + "relay.snort.social", + "nostr.land", + "nostr.mom", + "relay.nos.social", + "relay.primal.net", + "relay.nostr.bg", + "no.str.cr", + "nostr21.com", + "nostrue.com", + "relay.siamstr.com", + "wot.utxo.one", + "nostrelites.org", + "wot.nostr.party", + "wot.sovbit.host", + "wot.girino.org", + "relay.lnau.net", + "wot.siamstr.com", + "wot.sudocarlos.com", + "relay.otherstuff.fyi", + "relay.lexingtonbitcoin.org", + "wot.azzamo.net", + "wot.swarmstr.com", + "zap.watch", + "satsage.xyz", + "wons.calva.dev" +] diff --git a/relays_import.json b/relays_import.json new file mode 100644 index 0000000..6476d61 --- /dev/null +++ b/relays_import.json @@ -0,0 +1,30 @@ +[ + "relay.damus.io", + "nos.lol", + "relay.nostr.band", + "relay.snort.social", + "nostr.land", + "nostr.mom", + "relay.nos.social", + "relay.primal.net", + "relay.nostr.bg", + "no.str.cr", + "nostr21.com", + "nostrue.com", + "relay.siamstr.com", + "wot.utxo.one", + "nostrelites.org", + "wot.nostr.party", + "wot.sovbit.host", + "wot.girino.org", + "relay.lnau.net", + "wot.siamstr.com", + "wot.sudocarlos.com", + "relay.otherstuff.fyi", + "relay.lexingtonbitcoin.org", + "wot.azzamo.net", + "wot.swarmstr.com", + "zap.watch", + "satsage.xyz", + "wons.calva.dev" +]