Skip to content

Commit

Permalink
config: alternative toml config
Browse files Browse the repository at this point in the history
  • Loading branch information
hieblmi committed May 13, 2024
1 parent 4888382 commit 0369ac4
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 18 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/hieblmi/go-host-lnaddr
go 1.19

require (
github.com/BurntSushi/toml v0.3.1
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible
github.com/lightningnetwork/lnd v0.17.2-beta
Expand Down
83 changes: 65 additions & 18 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import (
"encoding/json"
"flag"
"fmt"
"github.com/btcsuite/btclog"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/macaroons"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"gopkg.in/macaroon.v2"
Expand All @@ -16,6 +13,11 @@ import (
"net/http"
"os"
"strings"

"github.com/BurntSushi/toml"
"github.com/btcsuite/btclog"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/macaroons"
)

type ServerConfig struct {
Expand Down Expand Up @@ -73,21 +75,38 @@ type NostrConfig struct {
}

func main() {
c := flag.String(
"config", "./config.json", "Specify the configuration file",
)
configPath := flag.String("config", "", "path to configuration file "+
"(JSON or TOML)")

flag.Parse()
file, err := os.Open(*c)
if err != nil {
default_log.Fatalf("cannot open config file %v", err)

if *configPath == "" {
fmt.Println("Please specify a configuration file path.")
os.Exit(1)
}
defer file.Close()

config := ServerConfig{}
decoder := json.NewDecoder(file)
err = decoder.Decode(&config)
if err != nil {
default_log.Fatalf("cannot decode config JSON %v", err)
var config *ServerConfig
var err error
if strings.Contains(*configPath, ".json") {
config, err = loadJSONConfig(*configPath)
if err != nil {
fmt.Printf("Error loading JSON config: %v\n", err)

os.Exit(1)
}
fmt.Printf("Loaded JSON Config: %+v\n", config)
} else if strings.Contains(*configPath, ".toml") {
config, err = loadTOMLConfig(*configPath)
if err != nil {
fmt.Printf("Error loading TOML config: %v\n", err)

os.Exit(1)
}
fmt.Printf("Loaded TOML Config: %+v\n", config)
} else {
fmt.Println("Unsupported config file type.")

os.Exit(1)
}

workingDir := config.WorkingDir
Expand Down Expand Up @@ -117,16 +136,44 @@ func main() {
},
)

setupHandlerPerAddress(config)
setupHandlerPerAddress(*config)
setupNostrHandlers(config.Nostr)
setupNotificators(config)
setupNotificators(*config)

http.HandleFunc("/invoice/", invoiceManager.handleInvoiceCreation(
config),
*config,
),
)
http.ListenAndServe(fmt.Sprintf(":%d", config.AddressServerPort), nil)
}

func loadJSONConfig(filename string) (*ServerConfig, error) {
var config ServerConfig
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
decoder := json.NewDecoder(file)
err = decoder.Decode(&config)
if err != nil {
return nil, err
}
return &config, nil
}

func loadTOMLConfig(filename string) (*ServerConfig, error) {
var config ServerConfig
content, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
if _, err := toml.Decode(string(content), &config); err != nil {
return nil, err
}
return &config, nil
}

func setupHandlerPerAddress(config ServerConfig) {
metadata, err := metadataToString(config)
if err != nil {
Expand Down
27 changes: 27 additions & 0 deletions sample_config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
RPCHost = "https://localhost:8080"
InvoiceMacaroonPath = "/home/sato/.lnd/data/chain/bitcoin/mainnet/admin.macaroon"
TLSCertPath = "/home/sato/.lnd/tls.cert"
Private = false
LightningAddresses = ["[email protected]"]
MinSendable = 1000
MaxSendable = 1000000000
CommentAllowed = 150
Tag = "payRequest"
Metadata = [["text/plain", "Welcome to satswellspent.com"], ["text/identifier", "[email protected]"]]
Thumbnail = "/home/sato/.go-host-lnaddr/thumb.jpeg"
SuccessMessage = "Thank you!"
InvoiceCallback = "https://satswellspent.com/invoice/"
AddressServerPort = 9990

[Nostr]
[Nostr.names]
nostr.name = ""
[Nostr.relays]
"" = ["wss://nostr-pub.wellorder.net", "wss://relay.damus.io", "wss://nostr-verified.wellorder.net", "wss://relay.minds.com/nostr/v1/ws"]

[[Notificators]]
Type = "telegram"
MinAmount = 1
[Notificators.Params]
ChatId = ""
Token = ""
File renamed without changes.

0 comments on commit 0369ac4

Please sign in to comment.