91 lines
2.2 KiB
Go
91 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
|
|
"github.com/ekzyis/magicwallet/db"
|
|
"github.com/ekzyis/magicwallet/env"
|
|
"github.com/ekzyis/magicwallet/lightning"
|
|
"github.com/ekzyis/magicwallet/server"
|
|
"github.com/lightninglabs/lndclient"
|
|
)
|
|
|
|
func banner() {
|
|
s := ""+
|
|
` .__
|
|
_____ _____ ____ |__| ____
|
|
/ \\__ \ / ___\| |/ ___\
|
|
| Y Y \/ __ \_/ /_/ > \ \___
|
|
|__|_| (____ /\___ /|__|\___ >
|
|
\/ \//_____/..wallet..\/ `
|
|
fmt.Printf("%s\n\n", s)
|
|
}
|
|
|
|
func main() {
|
|
var (
|
|
dbUrl string
|
|
lndAddress string
|
|
lndCert string
|
|
lndMacaroon string
|
|
lndNetwork string
|
|
)
|
|
banner()
|
|
|
|
// env
|
|
if err := env.Load(); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
flag.StringVar(&dbUrl, "DATABASE_URL", "", "Public URL of website")
|
|
flag.StringVar(&lndAddress, "LND_ADDRESS", "localhost:10001", "LND gRPC server address")
|
|
flag.StringVar(&lndCert, "LND_CERT", "", "LND TLS certificate in hex")
|
|
flag.StringVar(&lndMacaroon, "LND_MACAROON", "", "LND macaroon in hex")
|
|
flag.StringVar(&lndNetwork, "LND_NETWORK", "regtest", "LND network")
|
|
env.Parse()
|
|
|
|
log.Printf("Commit: %s", env.CommitShortSha)
|
|
log.Printf("Environment: %s", env.Env)
|
|
|
|
// database
|
|
db_, err := db.New(dbUrl)
|
|
if err != nil {
|
|
log.Printf("❌ failed to connect to db: %v\n", err)
|
|
}
|
|
|
|
// lightning
|
|
tlsData, err := hex.DecodeString(lndCert)
|
|
if err != nil {
|
|
log.Printf("❌ failed to decode LND TLS certificate: %v\n", err)
|
|
}
|
|
lnd_, err := lightning.New(&lightning.LNDConfig{
|
|
LndAddress: lndAddress,
|
|
CustomMacaroonHex: lndMacaroon,
|
|
TLSData: string(tlsData),
|
|
Network: lndclient.Network(lndNetwork),
|
|
Insecure: false,
|
|
})
|
|
if err != nil {
|
|
log.Printf("❌ failed to connect to LND: %v\n", err)
|
|
lnd_ = nil
|
|
} else {
|
|
log.Printf("✅ connected to %s LND v%s\n", lndAddress, lnd_.Version.Version)
|
|
}
|
|
|
|
// server
|
|
ctx := server.Context{
|
|
Environment: env.Env,
|
|
CommitShortSha: env.CommitShortSha,
|
|
Db: db_,
|
|
Lnd: lnd_,
|
|
}
|
|
s := server.New(ctx)
|
|
|
|
// TODO: read port from env
|
|
if err = s.Start(fmt.Sprintf("127.0.0.1:%d", env.Port)); err != http.ErrServerClosed {
|
|
log.Fatal(err)
|
|
}
|
|
}
|