From a83fbc606718dd599d1e5071f95567d987f44324 Mon Sep 17 00:00:00 2001 From: kiwiidb Date: Wed, 19 Jan 2022 14:35:46 +0100 Subject: [PATCH] add constructor funcs for controllers --- controllers/addinvoice.ctrl.go | 4 ++++ controllers/auth.ctrl.go | 8 ++++++++ controllers/balance.ctrl.go | 4 ++++ controllers/checkpayment.ctrl.go | 5 +++++ controllers/create.ctrl.go | 4 ++++ controllers/gettxs.ctrl.go | 5 +++++ controllers/payinvoice.ctrl.go | 4 ++++ main.go | 19 ++++++++++++------- 8 files changed, 46 insertions(+), 7 deletions(-) diff --git a/controllers/addinvoice.ctrl.go b/controllers/addinvoice.ctrl.go index d068ce7..36ba3cd 100644 --- a/controllers/addinvoice.ctrl.go +++ b/controllers/addinvoice.ctrl.go @@ -16,6 +16,10 @@ type AddInvoiceController struct { svc *lib.LndhubService } +func NewAddInvoiceController(svc *lib.LndhubService) *AddInvoiceController { + return &AddInvoiceController{svc: svc} +} + // AddInvoice : Add invoice Controller func (controller *AddInvoiceController) AddInvoice(c echo.Context) error { userID := c.Get("UserID").(int64) diff --git a/controllers/auth.ctrl.go b/controllers/auth.ctrl.go index 2c0d234..4e6905c 100644 --- a/controllers/auth.ctrl.go +++ b/controllers/auth.ctrl.go @@ -18,6 +18,14 @@ type AuthController struct { svc *lib.LndhubService } +func NewAuthController(svc *lib.LndhubService, secret []byte, expiry int) *AuthController { + return &AuthController{ + svc: svc, + JWTSecret: secret, + JWTExpiry: expiry, + } +} + // Auth : Auth Controller func (controller *AuthController) Auth(c echo.Context) error { type RequestBody struct { diff --git a/controllers/balance.ctrl.go b/controllers/balance.ctrl.go index 1f6008e..a05e699 100644 --- a/controllers/balance.ctrl.go +++ b/controllers/balance.ctrl.go @@ -13,6 +13,10 @@ type BalanceController struct { svc *lib.LndhubService } +func NewBalanceController(svc *lib.LndhubService) *BalanceController { + return &BalanceController{svc: svc} +} + // Balance : Balance Controller func (controller *BalanceController) Balance(c echo.Context) error { userId := c.Get("UserID").(int64) diff --git a/controllers/checkpayment.ctrl.go b/controllers/checkpayment.ctrl.go index f7eb077..41b6d57 100644 --- a/controllers/checkpayment.ctrl.go +++ b/controllers/checkpayment.ctrl.go @@ -3,12 +3,17 @@ package controllers import ( "net/http" + "github.com/getAlby/lndhub.go/lib" "github.com/labstack/echo/v4" ) // CheckPaymentController : CheckPaymentController struct type CheckPaymentController struct{} +func NewCheckPaymentController(svc *lib.LndhubService) *CheckPaymentController { + return &CheckPaymentController{} +} + // CheckPayment : Check Payment Controller func (CheckPaymentController) CheckPayment(c echo.Context) error { _ = c.Param("payment_hash") diff --git a/controllers/create.ctrl.go b/controllers/create.ctrl.go index 6e7beaa..27f0950 100644 --- a/controllers/create.ctrl.go +++ b/controllers/create.ctrl.go @@ -21,6 +21,10 @@ type CreateUserController struct { svc *lib.LndhubService } +func NewCreateUserController(svc *lib.LndhubService) *CreateUserController { + return &CreateUserController{svc: svc} +} + // CreateUser : Create user Controller func (controller *CreateUserController) CreateUser(c echo.Context) error { // optional parameters that we currently do not use diff --git a/controllers/gettxs.ctrl.go b/controllers/gettxs.ctrl.go index 88bac37..005f3a8 100644 --- a/controllers/gettxs.ctrl.go +++ b/controllers/gettxs.ctrl.go @@ -4,12 +4,17 @@ import ( "net/http" "github.com/getAlby/lndhub.go/db/models" + "github.com/getAlby/lndhub.go/lib" "github.com/labstack/echo/v4" ) // GetTXSController : GetTXSController struct type GetTXSController struct{} +func NewGetTXSController(svc *lib.LndhubService) *GetTXSController { + return &GetTXSController{} +} + // GetTXS : Get TXS Controller func (GetTXSController) GetTXS(c echo.Context) error { return c.JSON(http.StatusOK, &models.Invoice{}) diff --git a/controllers/payinvoice.ctrl.go b/controllers/payinvoice.ctrl.go index f192572..9907850 100644 --- a/controllers/payinvoice.ctrl.go +++ b/controllers/payinvoice.ctrl.go @@ -14,6 +14,10 @@ type PayInvoiceController struct { svc *lib.LndhubService } +func NewPayInvoiceController(svc *lib.LndhubService) *PayInvoiceController { + return &PayInvoiceController{svc: svc} +} + // PayInvoice : Pay invoice Controller func (controller *PayInvoiceController) PayInvoice(c echo.Context) error { userId := c.Get("UserID").(int64) diff --git a/main.go b/main.go index 75e8c5a..d83174b 100644 --- a/main.go +++ b/main.go @@ -112,15 +112,20 @@ func main() { } logger.Infof("Connected to LND: %s - %s", getInfo.Alias, getInfo.IdentityPubkey) - e.POST("/auth", controllers.AuthController{JWTSecret: c.JWTSecret, JWTExpiry: c.JWTExpiry}.Auth) - e.POST("/create", controllers.CreateUserController{}.CreateUser) + svc := &lib.LndhubService{ + DB: dbConn, + LndClient: &lndClient, + } + + e.POST("/auth", controllers.NewAuthController(svc, c.JWTSecret, c.JWTExpiry).Auth) + e.POST("/create", controllers.NewCreateUserController(svc).CreateUser) secured := e.Group("", tokens.Middleware(c.JWTSecret)) - secured.POST("/addinvoice", controllers.AddInvoiceController{}.AddInvoice) - secured.POST("/payinvoice", controllers.PayInvoiceController{}.PayInvoice) - secured.GET("/gettxs", controllers.GetTXSController{}.GetTXS) - secured.GET("/checkpayment/:payment_hash", controllers.CheckPaymentController{}.CheckPayment) - secured.GET("/balance", controllers.BalanceController{}.Balance) + secured.POST("/addinvoice", controllers.NewAddInvoiceController(svc).AddInvoice) + secured.POST("/payinvoice", controllers.NewPayInvoiceController(svc).PayInvoice) + secured.GET("/gettxs", controllers.NewGetTXSController(svc).GetTXS) + secured.GET("/checkpayment/:payment_hash", controllers.NewCheckPaymentController(svc).CheckPayment) + secured.GET("/balance", controllers.NewBalanceController(svc).Balance) // Start server go func() {