mirror of
https://github.com/getAlby/lndhub.go.git
synced 2025-12-20 06:05:08 +01:00
more refactoring
This commit is contained in:
@@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -47,12 +47,16 @@ func main() {
|
|||||||
e := echo.New()
|
e := echo.New()
|
||||||
|
|
||||||
// Init new LND client
|
// 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{
|
lndClient, err := lnd.NewLNDclient(lnd.LNDoptions{
|
||||||
Address: c.LNDAddress,
|
Address: lnCfg.LNDAddress,
|
||||||
MacaroonFile: c.LNDMacaroonFile,
|
MacaroonFile: lnCfg.LNDMacaroonFile,
|
||||||
MacaroonHex: c.LNDMacaroonHex,
|
MacaroonHex: lnCfg.LNDMacaroonHex,
|
||||||
CertFile: c.LNDCertFile,
|
CertFile: lnCfg.LNDCertFile,
|
||||||
CertHex: c.LNDCertHex,
|
CertHex: lnCfg.LNDCertHex,
|
||||||
}, startupCtx)
|
}, startupCtx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
e.Logger.Fatalf("Error initializing the LND connection: %v", err)
|
e.Logger.Fatalf("Error initializing the LND connection: %v", err)
|
||||||
@@ -2,7 +2,6 @@ package server
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"embed"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -22,6 +21,7 @@ import (
|
|||||||
"github.com/getAlby/lndhub.go/lib"
|
"github.com/getAlby/lndhub.go/lib"
|
||||||
"github.com/getAlby/lndhub.go/lib/service"
|
"github.com/getAlby/lndhub.go/lib/service"
|
||||||
"github.com/getAlby/lndhub.go/lib/tokens"
|
"github.com/getAlby/lndhub.go/lib/tokens"
|
||||||
|
"github.com/getAlby/lndhub.go/lib/transport"
|
||||||
"github.com/getsentry/sentry-go"
|
"github.com/getsentry/sentry-go"
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
"github.com/kelseyhightower/envconfig"
|
"github.com/kelseyhightower/envconfig"
|
||||||
@@ -30,12 +30,6 @@ import (
|
|||||||
"github.com/uptrace/bun/migrate"
|
"github.com/uptrace/bun/migrate"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed templates/index.html
|
|
||||||
var indexHtml string
|
|
||||||
|
|
||||||
//go:embed static/*
|
|
||||||
var staticContent embed.FS
|
|
||||||
|
|
||||||
// @title LndHub.go
|
// @title LndHub.go
|
||||||
// @version 0.9.0
|
// @version 0.9.0
|
||||||
// @description Accounting wrapper for the Lightning Network providing separate accounts for end-users.
|
// @description Accounting wrapper for the Lightning Network providing separate accounts for end-users.
|
||||||
@@ -148,7 +142,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//init echo server
|
//init echo server
|
||||||
e := initEcho(c, logger)
|
e := transport.InitEcho(c, logger)
|
||||||
//if Datadog is configured, add datadog middleware
|
//if Datadog is configured, add datadog middleware
|
||||||
if c.DatadogAgentUrl != "" {
|
if c.DatadogAgentUrl != "" {
|
||||||
tracer.Start(tracer.WithAgentAddr(c.DatadogAgentUrl))
|
tracer.Start(tracer.WithAgentAddr(c.DatadogAgentUrl))
|
||||||
@@ -156,14 +150,14 @@ func main() {
|
|||||||
e.Use(ddEcho.Middleware(ddEcho.WithServiceName("lndhub.go")))
|
e.Use(ddEcho.Middleware(ddEcho.WithServiceName("lndhub.go")))
|
||||||
}
|
}
|
||||||
|
|
||||||
logMw := createLoggingMiddleware(logger)
|
logMw := transport.CreateLoggingMiddleware(logger)
|
||||||
// strict rate limit for requests for sending payments
|
// 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)
|
secured := e.Group("", tokens.Middleware(c.JWTSecret), logMw)
|
||||||
securedWithStrictRateLimit := e.Group("", tokens.Middleware(c.JWTSecret), strictRateLimitMiddleware, logMw)
|
securedWithStrictRateLimit := e.Group("", tokens.Middleware(c.JWTSecret), strictRateLimitMiddleware, logMw)
|
||||||
|
|
||||||
RegisterLegacyEndpoints(svc, e, secured, securedWithStrictRateLimit, strictRateLimitMiddleware, tokens.AdminTokenMiddleware(c.AdminToken), logMw)
|
transport.RegisterLegacyEndpoints(svc, e, secured, securedWithStrictRateLimit, strictRateLimitMiddleware, tokens.AdminTokenMiddleware(c.AdminToken), logMw)
|
||||||
RegisterV2Endpoints(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
|
//Swagger API spec
|
||||||
docs.SwaggerInfo.Host = c.Host
|
docs.SwaggerInfo.Host = c.Host
|
||||||
@@ -174,7 +168,7 @@ func main() {
|
|||||||
// Subscribe to LND invoice updates in the background
|
// Subscribe to LND invoice updates in the background
|
||||||
backgroundWg.Add(1)
|
backgroundWg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
err = StartInvoiceRoutine(svc, backGroundCtx)
|
err = svc.StartInvoiceRoutine(backGroundCtx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
sentry.CaptureException(err)
|
sentry.CaptureException(err)
|
||||||
//we want to restart in case of an error here
|
//we want to restart in case of an error here
|
||||||
@@ -187,7 +181,7 @@ func main() {
|
|||||||
// Check the status of all pending outgoing payments
|
// Check the status of all pending outgoing payments
|
||||||
backgroundWg.Add(1)
|
backgroundWg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
err = StartPendingPaymentRoutine(svc, backGroundCtx)
|
err = svc.StartPendingPaymentRoutine(backGroundCtx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
sentry.CaptureException(err)
|
sentry.CaptureException(err)
|
||||||
//in case of an error here no restart is necessary
|
//in case of an error here no restart is necessary
|
||||||
@@ -228,7 +222,7 @@ func main() {
|
|||||||
//Start Prometheus server if necessary
|
//Start Prometheus server if necessary
|
||||||
var echoPrometheus *echo.Echo
|
var echoPrometheus *echo.Echo
|
||||||
if svc.Config.EnablePrometheus {
|
if svc.Config.EnablePrometheus {
|
||||||
go startPrometheusEcho(logger, svc, e)
|
go transport.StartPrometheusEcho(logger, svc, e)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start server
|
// Start server
|
||||||
|
|||||||
@@ -54,8 +54,6 @@ func LndHubTestServiceInit(lndClientMock lnd.LightningClientWrapper) (svc *servi
|
|||||||
JWTSecret: []byte("SECRET"),
|
JWTSecret: []byte("SECRET"),
|
||||||
JWTAccessTokenExpiry: 3600,
|
JWTAccessTokenExpiry: 3600,
|
||||||
JWTRefreshTokenExpiry: 3600,
|
JWTRefreshTokenExpiry: 3600,
|
||||||
LNDAddress: mockLNDAddress,
|
|
||||||
LNDMacaroonHex: mockLNDMacaroonHex,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rabbitmqUri, ok := os.LookupEnv("RABBITMQ_URI")
|
rabbitmqUri, ok := os.LookupEnv("RABBITMQ_URI")
|
||||||
|
|||||||
32
lib/service/background_routines.go
Normal file
32
lib/service/background_routines.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package transport
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -21,7 +22,13 @@ import (
|
|||||||
"golang.org/x/time/rate"
|
"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
|
// New Echo app
|
||||||
e = echo.New()
|
e = echo.New()
|
||||||
@@ -45,7 +52,8 @@ func initEcho(c *service.Config, logger *lecho.Logger) (e *echo.Echo) {
|
|||||||
}
|
}
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
func createLoggingMiddleware(logger *lecho.Logger) echo.MiddlewareFunc {
|
|
||||||
|
func CreateLoggingMiddleware(logger *lecho.Logger) echo.MiddlewareFunc {
|
||||||
return lecho.Middleware(lecho.Config{
|
return lecho.Middleware(lecho.Config{
|
||||||
Logger: logger,
|
Logger: logger,
|
||||||
Enricher: func(c echo.Context, logger zerolog.Context) zerolog.Context {
|
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{
|
config := middleware.RateLimiterConfig{
|
||||||
Store: middleware.NewRateLimiterMemoryStoreWithConfig(
|
Store: middleware.NewRateLimiterMemoryStoreWithConfig(
|
||||||
middleware.RateLimiterMemoryStoreConfig{Rate: rate.Limit(requestsPerSecond), Burst: burst},
|
middleware.RateLimiterMemoryStoreConfig{Rate: rate.Limit(requestsPerSecond), Burst: burst},
|
||||||
@@ -96,7 +104,7 @@ func createCacheClient() *cache.Client {
|
|||||||
return cacheClient
|
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
|
// Create Prometheus server and Middleware
|
||||||
echoPrometheus := echo.New()
|
echoPrometheus := echo.New()
|
||||||
echoPrometheus.HideBanner = true
|
echoPrometheus.HideBanner = true
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package transport
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
Before Width: | Height: | Size: 8.5 KiB After Width: | Height: | Size: 8.5 KiB |
|
Before Width: | Height: | Size: 2.3 KiB After Width: | Height: | Size: 2.3 KiB |
|
Before Width: | Height: | Size: 2.7 KiB After Width: | Height: | Size: 2.7 KiB |
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package transport
|
||||||
|
|
||||||
import (
|
import (
|
||||||
v2controllers "github.com/getAlby/lndhub.go/controllers_v2"
|
v2controllers "github.com/getAlby/lndhub.go/controllers_v2"
|
||||||
Reference in New Issue
Block a user