Skip to content

Commit

Permalink
Merge branch 'main' into no-protocol-redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
reneaaron committed Nov 1, 2023
2 parents 49f6a28 + 680b7f7 commit 20aad22
Show file tree
Hide file tree
Showing 30 changed files with 738 additions and 406 deletions.
1 change: 0 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
views/apps/show.html
4 changes: 2 additions & 2 deletions echo_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,6 @@ func (svc *Service) AppsNewHandler(c echo.Context) error {

requestMethods = strings.Join(keys, " ")
}
budgetEnabled := maxAmount != "" || budgetRenewal != ""
csrf, _ := c.Get(middleware.DefaultCSRFConfig.ContextKey).(string)

user, err := svc.GetUser(c)
Expand All @@ -280,13 +279,15 @@ func (svc *Service) AppsNewHandler(c echo.Context) error {
//and indicate which ones are checked by default in the front-end
type RequestMethodHelper struct {
Description string
Icon string
Checked bool
}

requestMethodHelper := map[string]*RequestMethodHelper{}
for k, v := range nip47MethodDescriptions {
requestMethodHelper[k] = &RequestMethodHelper{
Description: v,
Icon: nip47MethodIcons[k],
}
}

Expand All @@ -304,7 +305,6 @@ func (svc *Service) AppsNewHandler(c echo.Context) error {
"MaxAmount": maxAmount,
"BudgetRenewal": budgetRenewal,
"ExpiresAt": expiresAt,
"BudgetEnabled": budgetEnabled,
"RequestMethods": requestMethods,
"RequestMethodHelper": requestMethodHelper,
"Disabled": disabled,
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
google.golang.org/grpc v1.53.0
gopkg.in/DataDog/dd-trace-go.v1 v1.47.0
gopkg.in/macaroon.v2 v2.1.0
gorm.io/gorm v1.25.0
gorm.io/gorm v1.25.4
)

require (
Expand Down Expand Up @@ -55,6 +55,7 @@ require (
github.com/fergusstrange/embedded-postgres v1.19.0 // indirect
github.com/glebarez/go-sqlite v1.20.3 // indirect
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-gormigrate/gormigrate/v2 v2.1.1 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-macaroon-bakery/macaroonpb v1.0.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3Bop
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gormigrate/gormigrate/v2 v2.1.1 h1:eGS0WTFRV30r103lU8JNXY27KbviRnqqIDobW3EV3iY=
github.com/go-gormigrate/gormigrate/v2 v2.1.1/go.mod h1:L7nJ620PFDKei9QOhJzqA8kRCk+E3UbV2f5gv+1ndLc=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY=
Expand Down Expand Up @@ -1319,6 +1321,8 @@ gorm.io/driver/sqlserver v1.0.4 h1:V15fszi0XAo7fbx3/cF50ngshDSN4QT0MXpWTylyPTY=
gorm.io/gorm v1.24.0/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA=
gorm.io/gorm v1.25.0 h1:+KtYtb2roDz14EQe4bla8CbQlmb9dN3VejSai3lprfU=
gorm.io/gorm v1.25.0/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
gorm.io/gorm v1.25.4 h1:iyNd8fNAe8W9dvtlgeRI5zSVZPsq3OpcTu37cYcpCmw=
gorm.io/gorm v1.25.4/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
2 changes: 1 addition & 1 deletion handle_payment_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (svc *Service) HandlePayInvoiceEvent(ctx context.Context, request *Nip47Req
},
}, ss)
}
payment.Preimage = preimage
payment.Preimage = &preimage
nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_EXECUTED
svc.db.Save(&nostrEvent)
svc.db.Save(&payment)
Expand Down
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

echologrus "github.com/davrux/echo-logrus/v4"
"github.com/getAlby/nostr-wallet-connect/migrations"
"github.com/glebarez/sqlite"
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
Expand Down Expand Up @@ -79,11 +80,11 @@ func main() {
sqlDb.SetMaxIdleConns(cfg.DatabaseMaxIdleConns)
sqlDb.SetConnMaxLifetime(time.Duration(cfg.DatabaseConnMaxLifetime) * time.Second)

// Migrate the schema
err = db.AutoMigrate(&User{}, &App{}, &AppPermission{}, &NostrEvent{}, &Payment{}, &Identity{})
err = migrations.Migrate(db)
if err != nil {
log.Fatalf("Failed migrate DB %v", err)
log.Fatalf("Migration failed: %v", err)
}
log.Println("Any pending migrations ran successfully")

if cfg.NostrSecretKey == "" {
if cfg.LNBackendType == AlbyBackendType {
Expand Down
42 changes: 42 additions & 0 deletions migrations/202309271616_initial_migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package migrations

import (
_ "embed"
"log"

"github.com/go-gormigrate/gormigrate/v2"
"gorm.io/gorm"
)

//go:embed initial_migration_postgres.sql
var initialMigrationPostgres string
//go:embed initial_migration_sqlite.sql
var initialMigrationSqlite string

var initialMigrations = map[string]string {
"postgres": initialMigrationPostgres,
"sqlite": initialMigrationSqlite,
}

// Initial migration
var _202309271616_initial_migration = &gormigrate.Migration {
ID: "202309271616_initial_migration",
Migrate: func(tx *gorm.DB) error {
// only execute migration if apps table doesn't exist
err := tx.Exec("SELECT * FROM apps").Error;
if err != nil {
// find which initial migration should be executed
initialMigration := initialMigrations[tx.Dialector.Name()]
if initialMigration == "" {
log.Fatalf("unsupported database type: %s", tx.Dialector.Name())
}

return tx.Exec(initialMigration).Error
}

return nil
},
Rollback: func(tx *gorm.DB) error {
return nil;
},
}
17 changes: 17 additions & 0 deletions migrations/202309271617_fix_preimage_null.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package migrations

import (
"github.com/go-gormigrate/gormigrate/v2"
"gorm.io/gorm"
)

// Update payments with preimage as an empty string to use NULL instead
var _202309271617_fix_preimage_null = &gormigrate.Migration {
ID: "202309271617_fix_preimage_null",
Migrate: func(tx *gorm.DB) error {
return tx.Table("payments").Where("preimage = ?", "").Update("preimage", nil).Error;
},
Rollback: func(tx *gorm.DB) error {
return nil;
},
}
29 changes: 29 additions & 0 deletions migrations/202309271618_add_payment_sum_index.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package migrations

import (
"log"

"github.com/go-gormigrate/gormigrate/v2"
"gorm.io/gorm"
)

// Create a composite index to improve performance of summing payments in the current budget period
var _202309271618_add_payment_sum_index = &gormigrate.Migration {
ID: "202309271618_add_payment_sum_index",
Migrate: func(tx *gorm.DB) error {

var sql string
if tx.Dialector.Name() == "postgres" {
sql = "CREATE INDEX idx_payment_sum ON payments USING btree (app_id, preimage, created_at) INCLUDE(amount)"
} else if tx.Dialector.Name() == "sqlite" {
sql = "CREATE INDEX idx_payment_sum ON payments (app_id, preimage, created_at)"
} else {
log.Fatalf("unsupported database type: %s", tx.Dialector.Name())
}

return tx.Exec(sql).Error
},
Rollback: func(tx *gorm.DB) error {
return nil;
},
}
28 changes: 28 additions & 0 deletions migrations/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Creating a new migration

1. Create a new file based on the current date and time (see existing migration format)
2. Copy the following code and update MY_ID_HERE and MY_COMMENT_HERE and DO_SOMETHING_HERE
3. Add the ID to the list of migrations in migrate.go
4. If possible, add a rollback function.

*For Postgres/Sqlite specific migrations, see the [initial migration](202309271616.go)*

```go
package migrations

import (
"github.com/go-gormigrate/gormigrate/v2"
"gorm.io/gorm"
)

// MY_COMMENT_HERE
var _MY_ID_HERE = &gormigrate.Migration {
ID: "MY_ID_HERE",
Migrate: func(tx *gorm.DB) error {
return DO_SOMETHING_HERE.Error;
},
Rollback: func(tx *gorm.DB) error {
return nil;
},
}
```
Loading

0 comments on commit 20aad22

Please sign in to comment.