diff --git a/background_routines.go b/background_routines.go deleted file mode 100644 index b01ad07..0000000 --- a/background_routines.go +++ /dev/null @@ -1,34 +0,0 @@ -package main - -import ( - "context" - - "github.com/getAlby/lndhub.go/lib/service" -) - -func StartInvoiceRoutine(svc *service.LndhubService, backGroundCtx context.Context) (err error) { - if svc.RabbitMQClient != nil { - err = svc.RabbitMQClient.SubscribeToLndInvoices(backGroundCtx, svc.ProcessInvoiceUpdate) - if err != nil && err != context.Canceled { - return err - } - - return nil - } else { - err = svc.InvoiceUpdateSubscription(backGroundCtx) - if err != nil && err != context.Canceled { - // in case of an error in this routine, we want to restart LNDhub - return err - } - - return nil - } -} - -func StartPendingPaymentRoutine(svc *service.LndhubService, backGroundCtx context.Context) (err error) { - if svc.RabbitMQClient != nil { - return svc.RabbitMQClient.FinalizeInitializedPayments(backGroundCtx, svc) - } else { - return svc.CheckAllPendingOutgoingPayments(backGroundCtx) - } -} diff --git a/reconciliation_lost_invoices/main.go b/cmd/payment-reconciliation/main.go similarity index 85% rename from reconciliation_lost_invoices/main.go rename to cmd/payment-reconciliation/main.go index bdd41ec..b13b05b 100644 --- a/reconciliation_lost_invoices/main.go +++ b/cmd/payment-reconciliation/main.go @@ -47,12 +47,16 @@ func main() { e := echo.New() // Init new LND client + lnCfg, err := lnd.LoadConfig() + if err != nil { + logger.Fatalf("Failed to load lnd config %v", err) + } lndClient, err := lnd.NewLNDclient(lnd.LNDoptions{ - Address: c.LNDAddress, - MacaroonFile: c.LNDMacaroonFile, - MacaroonHex: c.LNDMacaroonHex, - CertFile: c.LNDCertFile, - CertHex: c.LNDCertHex, + Address: lnCfg.LNDAddress, + MacaroonFile: lnCfg.LNDMacaroonFile, + MacaroonHex: lnCfg.LNDMacaroonHex, + CertFile: lnCfg.LNDCertFile, + CertHex: lnCfg.LNDCertHex, }, startupCtx) if err != nil { e.Logger.Fatalf("Error initializing the LND connection: %v", err) diff --git a/cmd/server/main.go b/cmd/server/main.go index fb53731..8ed1ee9 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -2,7 +2,6 @@ package server import ( "context" - "embed" "fmt" "log" "net/http" @@ -22,6 +21,7 @@ import ( "github.com/getAlby/lndhub.go/lib" "github.com/getAlby/lndhub.go/lib/service" "github.com/getAlby/lndhub.go/lib/tokens" + "github.com/getAlby/lndhub.go/lib/transport" "github.com/getsentry/sentry-go" "github.com/joho/godotenv" "github.com/kelseyhightower/envconfig" @@ -30,12 +30,6 @@ import ( "github.com/uptrace/bun/migrate" ) -//go:embed templates/index.html -var indexHtml string - -//go:embed static/* -var staticContent embed.FS - // @title LndHub.go // @version 0.9.0 // @description Accounting wrapper for the Lightning Network providing separate accounts for end-users. @@ -148,7 +142,7 @@ func main() { } //init echo server - e := initEcho(c, logger) + e := transport.InitEcho(c, logger) //if Datadog is configured, add datadog middleware if c.DatadogAgentUrl != "" { tracer.Start(tracer.WithAgentAddr(c.DatadogAgentUrl)) @@ -156,14 +150,14 @@ func main() { e.Use(ddEcho.Middleware(ddEcho.WithServiceName("lndhub.go"))) } - logMw := createLoggingMiddleware(logger) + logMw := transport.CreateLoggingMiddleware(logger) // strict rate limit for requests for sending payments - strictRateLimitMiddleware := createRateLimitMiddleware(c.StrictRateLimit, c.BurstRateLimit) + strictRateLimitMiddleware := transport.CreateRateLimitMiddleware(c.StrictRateLimit, c.BurstRateLimit) secured := e.Group("", tokens.Middleware(c.JWTSecret), logMw) securedWithStrictRateLimit := e.Group("", tokens.Middleware(c.JWTSecret), strictRateLimitMiddleware, logMw) - RegisterLegacyEndpoints(svc, e, secured, securedWithStrictRateLimit, strictRateLimitMiddleware, tokens.AdminTokenMiddleware(c.AdminToken), logMw) - RegisterV2Endpoints(svc, e, secured, securedWithStrictRateLimit, strictRateLimitMiddleware, tokens.AdminTokenMiddleware(c.AdminToken), logMw) + transport.RegisterLegacyEndpoints(svc, e, secured, securedWithStrictRateLimit, strictRateLimitMiddleware, tokens.AdminTokenMiddleware(c.AdminToken), logMw) + transport.RegisterV2Endpoints(svc, e, secured, securedWithStrictRateLimit, strictRateLimitMiddleware, tokens.AdminTokenMiddleware(c.AdminToken), logMw) //Swagger API spec docs.SwaggerInfo.Host = c.Host @@ -174,7 +168,7 @@ func main() { // Subscribe to LND invoice updates in the background backgroundWg.Add(1) go func() { - err = StartInvoiceRoutine(svc, backGroundCtx) + err = svc.StartInvoiceRoutine(backGroundCtx) if err != nil { sentry.CaptureException(err) //we want to restart in case of an error here @@ -187,7 +181,7 @@ func main() { // Check the status of all pending outgoing payments backgroundWg.Add(1) go func() { - err = StartPendingPaymentRoutine(svc, backGroundCtx) + err = svc.StartPendingPaymentRoutine(backGroundCtx) if err != nil { sentry.CaptureException(err) //in case of an error here no restart is necessary @@ -228,7 +222,7 @@ func main() { //Start Prometheus server if necessary var echoPrometheus *echo.Echo if svc.Config.EnablePrometheus { - go startPrometheusEcho(logger, svc, e) + go transport.StartPrometheusEcho(logger, svc, e) } // Start server diff --git a/integration_tests/util.go b/integration_tests/util.go index 3297ada..1d7812d 100644 --- a/integration_tests/util.go +++ b/integration_tests/util.go @@ -54,8 +54,6 @@ func LndHubTestServiceInit(lndClientMock lnd.LightningClientWrapper) (svc *servi JWTSecret: []byte("SECRET"), JWTAccessTokenExpiry: 3600, JWTRefreshTokenExpiry: 3600, - LNDAddress: mockLNDAddress, - LNDMacaroonHex: mockLNDMacaroonHex, } rabbitmqUri, ok := os.LookupEnv("RABBITMQ_URI") diff --git a/lib/service/background_routines.go b/lib/service/background_routines.go new file mode 100644 index 0000000..11cc270 --- /dev/null +++ b/lib/service/background_routines.go @@ -0,0 +1,32 @@ +package service + +import ( + "context" +) + +func (svc *LndhubService) StartInvoiceRoutine(ctx context.Context) (err error) { + if svc.RabbitMQClient != nil { + err = svc.RabbitMQClient.SubscribeToLndInvoices(ctx, svc.ProcessInvoiceUpdate) + if err != nil && err != context.Canceled { + return err + } + + return nil + } else { + err = svc.InvoiceUpdateSubscription(ctx) + if err != nil && err != context.Canceled { + // in case of an error in this routine, we want to restart LNDhub + return err + } + + return nil + } +} + +func (svc *LndhubService) StartPendingPaymentRoutine(ctx context.Context) (err error) { + if svc.RabbitMQClient != nil { + return svc.RabbitMQClient.FinalizeInitializedPayments(ctx, svc) + } else { + return svc.CheckAllPendingOutgoingPayments(ctx) + } +} diff --git a/init_echo.go b/lib/transport/echo.go similarity index 88% rename from init_echo.go rename to lib/transport/echo.go index 42f4d7d..53e9404 100644 --- a/init_echo.go +++ b/lib/transport/echo.go @@ -1,6 +1,7 @@ -package main +package transport import ( + "embed" "fmt" "log" "strconv" @@ -21,7 +22,13 @@ import ( "golang.org/x/time/rate" ) -func initEcho(c *service.Config, logger *lecho.Logger) (e *echo.Echo) { +//go:embed templates/index.html +var indexHtml string + +//go:embed static/* +var staticContent embed.FS + +func InitEcho(c *service.Config, logger *lecho.Logger) (e *echo.Echo) { // New Echo app e = echo.New() @@ -45,7 +52,8 @@ func initEcho(c *service.Config, logger *lecho.Logger) (e *echo.Echo) { } return e } -func createLoggingMiddleware(logger *lecho.Logger) echo.MiddlewareFunc { + +func CreateLoggingMiddleware(logger *lecho.Logger) echo.MiddlewareFunc { return lecho.Middleware(lecho.Config{ Logger: logger, Enricher: func(c echo.Context, logger zerolog.Context) zerolog.Context { @@ -54,7 +62,7 @@ func createLoggingMiddleware(logger *lecho.Logger) echo.MiddlewareFunc { }) } -func createRateLimitMiddleware(requestsPerSecond int, burst int) echo.MiddlewareFunc { +func CreateRateLimitMiddleware(requestsPerSecond int, burst int) echo.MiddlewareFunc { config := middleware.RateLimiterConfig{ Store: middleware.NewRateLimiterMemoryStoreWithConfig( middleware.RateLimiterMemoryStoreConfig{Rate: rate.Limit(requestsPerSecond), Burst: burst}, @@ -96,7 +104,7 @@ func createCacheClient() *cache.Client { return cacheClient } -func startPrometheusEcho(logger *lecho.Logger, svc *service.LndhubService, e *echo.Echo) { +func StartPrometheusEcho(logger *lecho.Logger, svc *service.LndhubService, e *echo.Echo) { // Create Prometheus server and Middleware echoPrometheus := echo.New() echoPrometheus.HideBanner = true diff --git a/legacy_endpoints.go b/lib/transport/legacy_endpoints.go similarity index 99% rename from legacy_endpoints.go rename to lib/transport/legacy_endpoints.go index f3f82ad..7ac17cd 100644 --- a/legacy_endpoints.go +++ b/lib/transport/legacy_endpoints.go @@ -1,4 +1,4 @@ -package main +package transport import ( "net/http" diff --git a/static/css/style.css b/lib/transport/static/css/style.css similarity index 100% rename from static/css/style.css rename to lib/transport/static/css/style.css diff --git a/static/img/alby.svg b/lib/transport/static/img/alby.svg similarity index 100% rename from static/img/alby.svg rename to lib/transport/static/img/alby.svg diff --git a/static/img/favicon.png b/lib/transport/static/img/favicon.png similarity index 100% rename from static/img/favicon.png rename to lib/transport/static/img/favicon.png diff --git a/static/img/logo.png b/lib/transport/static/img/logo.png similarity index 100% rename from static/img/logo.png rename to lib/transport/static/img/logo.png diff --git a/templates/index.html b/lib/transport/templates/index.html similarity index 100% rename from templates/index.html rename to lib/transport/templates/index.html diff --git a/v2_endpoints.go b/lib/transport/v2_endpoints.go similarity index 98% rename from v2_endpoints.go rename to lib/transport/v2_endpoints.go index 68daa86..170cea3 100644 --- a/v2_endpoints.go +++ b/lib/transport/v2_endpoints.go @@ -1,4 +1,4 @@ -package main +package transport import ( v2controllers "github.com/getAlby/lndhub.go/controllers_v2"