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()
|
||||
|
||||
// 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)
|
||||
@@ -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
|
||||
|
||||
@@ -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")
|
||||
|
||||
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 (
|
||||
"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
|
||||
@@ -1,4 +1,4 @@
|
||||
package main
|
||||
package transport
|
||||
|
||||
import (
|
||||
"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 (
|
||||
v2controllers "github.com/getAlby/lndhub.go/controllers_v2"
|
||||
Reference in New Issue
Block a user