diff --git a/go.mod b/go.mod index 166dada..73f8777 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/main.go b/main.go index 22c9a6f..795acb0 100644 --- a/main.go +++ b/main.go @@ -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" @@ -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 { @@ -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 @@ -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 { diff --git a/sample_config.toml b/sample_config.toml new file mode 100644 index 0000000..5630967 --- /dev/null +++ b/sample_config.toml @@ -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 = ["thats@satswellspent.com"] +MinSendable = 1000 +MaxSendable = 1000000000 +CommentAllowed = 150 +Tag = "payRequest" +Metadata = [["text/plain", "Welcome to satswellspent.com"], ["text/identifier", "thats@satswellspent.com"]] +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 = "" diff --git a/config.json b/sample_config_json similarity index 100% rename from config.json rename to sample_config_json