POST:/users works

This commit is contained in:
2022-11-18 21:25:40 +01:00
parent 34a27d9ca4
commit 5991631bfa
44 changed files with 2131 additions and 737 deletions

View File

@@ -2,10 +2,8 @@ package logic
import (
scn "blackforestbytes.com/simplecloudnotifier"
"blackforestbytes.com/simplecloudnotifier/common/ginresp"
"blackforestbytes.com/simplecloudnotifier/db"
"context"
"database/sql"
"fmt"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
"math/rand"
@@ -20,10 +18,10 @@ import (
type Application struct {
Config scn.Config
Gin *gin.Engine
Database *sql.DB
Database *db.Database
}
func NewApp(db *sql.DB) *Application {
func NewApp(db *db.Database) *Application {
return &Application{Database: db}
}
@@ -74,30 +72,6 @@ func (app *Application) GenerateRandomAuthKey() string {
return k
}
func (app *Application) RunTransaction(ctx context.Context, opt *sql.TxOptions, fn func(tx *sql.Tx) (ginresp.HTTPResponse, bool)) ginresp.HTTPResponse {
tx, err := app.Database.BeginTx(ctx, opt)
if err != nil {
return ginresp.InternAPIError(0, fmt.Sprintf("Failed to create transaction: %v", err))
}
res, commit := fn(tx)
if commit {
err = tx.Commit()
if err != nil {
return ginresp.InternAPIError(0, fmt.Sprintf("Failed to commit transaction: %v", err))
}
} else {
err = tx.Rollback()
if err != nil {
return ginresp.InternAPIError(0, fmt.Sprintf("Failed to rollback transaction: %v", err))
}
}
return res
}
func (app *Application) QuotaMax(ispro bool) int {
if ispro {
return 1000
@@ -109,3 +83,16 @@ func (app *Application) QuotaMax(ispro bool) int {
func (app *Application) VerifyProToken(token string) (bool, error) {
return false, nil //TODO implement pro verification
}
func (app *Application) Migrate() error {
ctx, cancel := context.WithTimeout(context.Background(), 24*time.Second)
defer cancel()
return app.Database.Migrate(ctx)
}
func (app *Application) StartRequest(g *gin.Context) *AppContext {
ctx, cancel := context.WithTimeout(context.Background(), app.Config.RequestTimeout)
return &AppContext{inner: ctx, cancelFunc: cancel}
}

75
server/logic/context.go Normal file
View File

@@ -0,0 +1,75 @@
package logic
import (
"blackforestbytes.com/simplecloudnotifier/api/apierr"
"blackforestbytes.com/simplecloudnotifier/common/ginresp"
"blackforestbytes.com/simplecloudnotifier/db"
"context"
"database/sql"
"errors"
"time"
)
type AppContext struct {
inner context.Context
cancelFunc context.CancelFunc
cancelled bool
transaction *sql.Tx
}
func (ac *AppContext) Deadline() (deadline time.Time, ok bool) {
return ac.inner.Deadline()
}
func (ac *AppContext) Done() <-chan struct{} {
return ac.inner.Done()
}
func (ac *AppContext) Err() error {
return ac.inner.Err()
}
func (ac *AppContext) Value(key any) any {
return ac.inner.Value(key)
}
func (ac *AppContext) Cancel() {
ac.cancelled = true
if ac.transaction != nil {
err := ac.transaction.Rollback()
if err != nil {
panic("failed to rollback transaction: " + err.Error())
}
ac.transaction = nil
}
ac.cancelFunc()
}
func (ac *AppContext) FinishSuccess(res ginresp.HTTPResponse) ginresp.HTTPResponse {
if ac.cancelled {
panic("Cannot finish a cancelled request")
}
if ac.transaction != nil {
err := ac.transaction.Commit()
if err != nil {
return ginresp.InternAPIError(apierr.COMMIT_FAILED, "Failed to comit changes to DB", err)
}
ac.transaction = nil
}
return res
}
func (ac *AppContext) GetOrCreateTransaction(db *db.Database) (*sql.Tx, error) {
if ac.cancelled {
return nil, errors.New("context cancelled")
}
if ac.transaction != nil {
return ac.transaction, nil
}
tx, err := db.BeginTx(ac)
if err != nil {
return nil, err
}
ac.transaction = tx
return tx, nil
}