mirror of
https://github.com/getAlby/lndhub.go.git
synced 2025-12-20 14:14:47 +01:00
chore: further refactoring
This commit is contained in:
@@ -153,7 +153,6 @@ func main() {
|
|||||||
logMw := transport.CreateLoggingMiddleware(logger)
|
logMw := transport.CreateLoggingMiddleware(logger)
|
||||||
// strict rate limit for requests for sending payments
|
// strict rate limit for requests for sending payments
|
||||||
strictRateLimitMiddleware := transport.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)
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/getAlby/lndhub.go/common"
|
"github.com/getAlby/lndhub.go/common"
|
||||||
|
"github.com/getAlby/lndhub.go/db/models"
|
||||||
"github.com/getAlby/lndhub.go/lib/responses"
|
"github.com/getAlby/lndhub.go/lib/responses"
|
||||||
"github.com/getAlby/lndhub.go/lib/service"
|
"github.com/getAlby/lndhub.go/lib/service"
|
||||||
"github.com/getAlby/lndhub.go/lnd"
|
"github.com/getAlby/lndhub.go/lnd"
|
||||||
@@ -164,7 +165,7 @@ func (controller *KeySendController) MultiKeySend(c echo.Context) error {
|
|||||||
return c.JSON(status, result)
|
return c.JSON(status, result)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (controller *KeySendController) checkKeysendPaymentAllowed(ctx context.Context, amount, userID int64, limits *lnd.Limits) (resp *responses.ErrorResponse) {
|
func (controller *KeySendController) checkKeysendPaymentAllowed(ctx context.Context, amount, userID int64, limits *models.Limits) (resp *responses.ErrorResponse) {
|
||||||
syntheticPayReq := &lnd.LNPayReq{
|
syntheticPayReq := &lnd.LNPayReq{
|
||||||
PayReq: &lnrpc.PayReq{
|
PayReq: &lnrpc.PayReq{
|
||||||
NumSatoshis: amount,
|
NumSatoshis: amount,
|
||||||
|
|||||||
@@ -21,6 +21,14 @@ type User struct {
|
|||||||
Deactivated bool
|
Deactivated bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Limits struct {
|
||||||
|
MaxSendVolume int64
|
||||||
|
MaxSendAmount int64
|
||||||
|
MaxReceiveVolume int64
|
||||||
|
MaxReceiveAmount int64
|
||||||
|
MaxAccountBalance int64
|
||||||
|
}
|
||||||
|
|
||||||
func (u *User) BeforeAppendModel(ctx context.Context, query bun.Query) error {
|
func (u *User) BeforeAppendModel(ctx context.Context, query bun.Query) error {
|
||||||
switch query.(type) {
|
switch query.(type) {
|
||||||
case *bun.UpdateQuery:
|
case *bun.UpdateQuery:
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func LndHubTestServiceInit(lndClientMock lnd.LightningClientWrapper) (svc *service.LndhubService, err error) {
|
func LndHubTestServiceInit(lndClientMock lnd.LightningClientWrapper) (svc *service.LndhubService, err error) {
|
||||||
dbUri := "postgresql://im-adithya:password@localhost:5432/lndhub?sslmode=disable"
|
dbUri := "postgresql://user:password@localhost/lndhub?sslmode=disable"
|
||||||
c := &service.Config{
|
c := &service.Config{
|
||||||
DatabaseUri: dbUri,
|
DatabaseUri: dbUri,
|
||||||
DatabaseMaxConns: 1,
|
DatabaseMaxConns: 1,
|
||||||
|
|||||||
@@ -126,7 +126,7 @@ func (svc *LndhubService) FindUserByLogin(ctx context.Context, login string) (*m
|
|||||||
return &user, nil
|
return &user, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (svc *LndhubService) CheckOutgoingPaymentAllowed(ctx context.Context, lnpayReq *lnd.LNPayReq, userId int64, limits *lnd.Limits) (result *responses.ErrorResponse, err error) {
|
func (svc *LndhubService) CheckOutgoingPaymentAllowed(ctx context.Context, lnpayReq *lnd.LNPayReq, userId int64, limits *models.Limits) (result *responses.ErrorResponse, err error) {
|
||||||
if limits.MaxSendAmount > 0 {
|
if limits.MaxSendAmount > 0 {
|
||||||
if lnpayReq.PayReq.NumSatoshis > limits.MaxSendAmount {
|
if lnpayReq.PayReq.NumSatoshis > limits.MaxSendAmount {
|
||||||
svc.Logger.Errorf("Max send amount exceeded for user_id %v (amount:%v)", userId, lnpayReq.PayReq.NumSatoshis)
|
svc.Logger.Errorf("Max send amount exceeded for user_id %v (amount:%v)", userId, lnpayReq.PayReq.NumSatoshis)
|
||||||
@@ -157,7 +157,7 @@ func (svc *LndhubService) CheckOutgoingPaymentAllowed(ctx context.Context, lnpay
|
|||||||
return svc.CheckVolumeAllowed(ctx, userId, limits.MaxSendVolume, common.InvoiceTypeOutgoing)
|
return svc.CheckVolumeAllowed(ctx, userId, limits.MaxSendVolume, common.InvoiceTypeOutgoing)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (svc *LndhubService) CheckIncomingPaymentAllowed(ctx context.Context, amount, userId int64, limits *lnd.Limits) (result *responses.ErrorResponse, err error) {
|
func (svc *LndhubService) CheckIncomingPaymentAllowed(ctx context.Context, amount, userId int64, limits *models.Limits) (result *responses.ErrorResponse, err error) {
|
||||||
if limits.MaxReceiveAmount > 0 {
|
if limits.MaxReceiveAmount > 0 {
|
||||||
if amount > limits.MaxReceiveAmount {
|
if amount > limits.MaxReceiveAmount {
|
||||||
svc.Logger.Errorf("Max receive amount exceeded for user_id %d", userId)
|
svc.Logger.Errorf("Max receive amount exceeded for user_id %d", userId)
|
||||||
@@ -275,27 +275,27 @@ func (svc *LndhubService) GetVolumeOverPeriod(ctx context.Context, userId int64,
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (svc *LndhubService) GetLimits(c echo.Context) (limits *lnd.Limits) {
|
func (svc *LndhubService) GetLimits(c echo.Context) (limits *models.Limits) {
|
||||||
limits = &lnd.Limits{
|
limits = &models.Limits{
|
||||||
MaxSendVolume: svc.Config.MaxSendVolume,
|
MaxSendVolume: svc.Config.MaxSendVolume,
|
||||||
MaxSendAmount: svc.Config.MaxSendAmount,
|
MaxSendAmount: svc.Config.MaxSendAmount,
|
||||||
MaxReceiveVolume: svc.Config.MaxReceiveVolume,
|
MaxReceiveVolume: svc.Config.MaxReceiveVolume,
|
||||||
MaxReceiveAmount: svc.Config.MaxReceiveAmount,
|
MaxReceiveAmount: svc.Config.MaxReceiveAmount,
|
||||||
MaxAccountBalance: svc.Config.MaxAccountBalance,
|
MaxAccountBalance: svc.Config.MaxAccountBalance,
|
||||||
}
|
}
|
||||||
if val, ok := c.Get("MaxSendVolume").(int64); ok {
|
if val, ok := c.Get("MaxSendVolume").(int64); ok && val > 0 {
|
||||||
limits.MaxSendVolume = val
|
limits.MaxSendVolume = val
|
||||||
}
|
}
|
||||||
if val, ok := c.Get("MaxSendAmount").(int64); ok {
|
if val, ok := c.Get("MaxSendAmount").(int64); ok && val > 0 {
|
||||||
limits.MaxSendAmount = val
|
limits.MaxSendAmount = val
|
||||||
}
|
}
|
||||||
if val, ok := c.Get("MaxReceiveVolume").(int64); ok {
|
if val, ok := c.Get("MaxReceiveVolume").(int64); ok && val > 0 {
|
||||||
limits.MaxReceiveVolume = val
|
limits.MaxReceiveVolume = val
|
||||||
}
|
}
|
||||||
if val, ok := c.Get("MaxReceiveAmount").(int64); ok {
|
if val, ok := c.Get("MaxReceiveAmount").(int64); ok && val > 0 {
|
||||||
limits.MaxReceiveAmount = val
|
limits.MaxReceiveAmount = val
|
||||||
}
|
}
|
||||||
if val, ok := c.Get("MaxAccountBalance").(int64); ok {
|
if val, ok := c.Get("MaxAccountBalance").(int64); ok && val > 0 {
|
||||||
limits.MaxAccountBalance = val
|
limits.MaxAccountBalance = val
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,14 +22,6 @@ type Config struct {
|
|||||||
LNDClusterPubkeys string `envconfig:"LND_CLUSTER_PUBKEYS"` //comma-seperated list of public keys of the cluster
|
LNDClusterPubkeys string `envconfig:"LND_CLUSTER_PUBKEYS"` //comma-seperated list of public keys of the cluster
|
||||||
}
|
}
|
||||||
|
|
||||||
type Limits struct {
|
|
||||||
MaxSendVolume int64
|
|
||||||
MaxSendAmount int64
|
|
||||||
MaxReceiveVolume int64
|
|
||||||
MaxReceiveAmount int64
|
|
||||||
MaxAccountBalance int64
|
|
||||||
}
|
|
||||||
|
|
||||||
func LoadConfig() (c *Config, err error) {
|
func LoadConfig() (c *Config, err error) {
|
||||||
c = &Config{}
|
c = &Config{}
|
||||||
err = envconfig.Process("", c)
|
err = envconfig.Process("", c)
|
||||||
|
|||||||
Reference in New Issue
Block a user