mirror of
https://github.com/getAlby/lndhub.go.git
synced 2025-12-21 06:34:58 +01:00
refactor create endpoint
This commit is contained in:
@@ -1,21 +1,12 @@
|
|||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"database/sql"
|
|
||||||
"math/rand"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/getAlby/lndhub.go/db/models"
|
|
||||||
"github.com/getAlby/lndhub.go/lib"
|
"github.com/getAlby/lndhub.go/lib"
|
||||||
"github.com/getAlby/lndhub.go/lib/security"
|
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"github.com/labstack/gommon/random"
|
|
||||||
"github.com/uptrace/bun"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const alphaNumBytes = random.Alphanumeric
|
|
||||||
|
|
||||||
// CreateUserController : Create user controller struct
|
// CreateUserController : Create user controller struct
|
||||||
type CreateUserController struct {
|
type CreateUserController struct {
|
||||||
svc *lib.LndhubService
|
svc *lib.LndhubService
|
||||||
@@ -37,34 +28,8 @@ func (controller *CreateUserController) CreateUser(c echo.Context) error {
|
|||||||
if err := c.Bind(&body); err != nil {
|
if err := c.Bind(&body); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
user, err := controller.svc.Create()
|
||||||
user := models.User{}
|
//todo json response
|
||||||
|
|
||||||
// generate user login/password (TODO: allow the user to choose a login/password?)
|
|
||||||
user.Login = randStringBytes(8)
|
|
||||||
password := randStringBytes(15)
|
|
||||||
// we only store the hashed password but return the initial plain text password in the HTTP response
|
|
||||||
hashedPassword := security.HashPassword(password)
|
|
||||||
user.Password = hashedPassword
|
|
||||||
|
|
||||||
// Create user and the user's accounts
|
|
||||||
// We use double-entry bookkeeping so we use 4 accounts: incoming, current, outgoing and fees
|
|
||||||
// Wrapping this in a transaction in case something fails
|
|
||||||
err := controller.svc.DB.RunInTx(context.TODO(), &sql.TxOptions{}, func(ctx context.Context, tx bun.Tx) error {
|
|
||||||
if _, err := tx.NewInsert().Model(&user).Exec(ctx); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
accountTypes := []string{"incoming", "current", "outgoing", "fees"}
|
|
||||||
for _, accountType := range accountTypes {
|
|
||||||
account := models.Account{UserID: user.ID, Type: accountType}
|
|
||||||
if _, err := controller.svc.DB.NewInsert().Model(&account).Exec(ctx); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
// Was the DB transaction successful?
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -74,15 +39,7 @@ func (controller *CreateUserController) CreateUser(c echo.Context) error {
|
|||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
}
|
}
|
||||||
ResponseBody.Login = user.Login
|
ResponseBody.Login = user.Login
|
||||||
ResponseBody.Password = password
|
ResponseBody.Password = user.Password
|
||||||
|
|
||||||
return c.JSON(http.StatusOK, &ResponseBody)
|
return c.JSON(http.StatusOK, &ResponseBody)
|
||||||
}
|
}
|
||||||
|
|
||||||
func randStringBytes(n int) string {
|
|
||||||
b := make([]byte, n)
|
|
||||||
for i := range b {
|
|
||||||
b[i] = alphaNumBytes[rand.Intn(len(alphaNumBytes))]
|
|
||||||
}
|
|
||||||
return string(b)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -2,15 +2,21 @@ package lib
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math/rand"
|
||||||
|
|
||||||
"github.com/getAlby/lndhub.go/db/models"
|
"github.com/getAlby/lndhub.go/db/models"
|
||||||
|
"github.com/getAlby/lndhub.go/lib/security"
|
||||||
"github.com/getAlby/lndhub.go/lib/tokens"
|
"github.com/getAlby/lndhub.go/lib/tokens"
|
||||||
|
"github.com/labstack/gommon/random"
|
||||||
"github.com/lightningnetwork/lnd/lnrpc"
|
"github.com/lightningnetwork/lnd/lnrpc"
|
||||||
"github.com/uptrace/bun"
|
"github.com/uptrace/bun"
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const alphaNumBytes = random.Alphanumeric
|
||||||
|
|
||||||
type LndhubService struct {
|
type LndhubService struct {
|
||||||
Config *Config
|
Config *Config
|
||||||
DB *bun.DB
|
DB *bun.DB
|
||||||
@@ -124,3 +130,41 @@ func (svc *LndhubService) Payinvoice(userId int64, invoice string) error {
|
|||||||
return err
|
return err
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (svc *LndhubService) Create() (user *models.User, err error) {
|
||||||
|
|
||||||
|
user = &models.User{}
|
||||||
|
|
||||||
|
// generate user login/password (TODO: allow the user to choose a login/password?)
|
||||||
|
user.Login = randStringBytes(8)
|
||||||
|
password := randStringBytes(15)
|
||||||
|
// we only store the hashed password but return the initial plain text password in the HTTP response
|
||||||
|
hashedPassword := security.HashPassword(password)
|
||||||
|
user.Password = hashedPassword
|
||||||
|
|
||||||
|
// Create user and the user's accounts
|
||||||
|
// We use double-entry bookkeeping so we use 4 accounts: incoming, current, outgoing and fees
|
||||||
|
// Wrapping this in a transaction in case something fails
|
||||||
|
err = svc.DB.RunInTx(context.TODO(), &sql.TxOptions{}, func(ctx context.Context, tx bun.Tx) error {
|
||||||
|
if _, err := tx.NewInsert().Model(&user).Exec(ctx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
accountTypes := []string{"incoming", "current", "outgoing", "fees"}
|
||||||
|
for _, accountType := range accountTypes {
|
||||||
|
account := models.Account{UserID: user.ID, Type: accountType}
|
||||||
|
if _, err := svc.DB.NewInsert().Model(&account).Exec(ctx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
return user, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func randStringBytes(n int) string {
|
||||||
|
b := make([]byte, n)
|
||||||
|
for i := range b {
|
||||||
|
b[i] = alphaNumBytes[rand.Intn(len(alphaNumBytes))]
|
||||||
|
}
|
||||||
|
return string(b)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user