SimpleCloudNotifier/scnserver/logic/appcontext.go

116 lines
2.7 KiB
Go
Raw Normal View History

2022-11-18 21:25:40 +01:00
package logic
import (
"blackforestbytes.com/simplecloudnotifier/api/apierr"
2022-12-20 13:55:09 +01:00
"blackforestbytes.com/simplecloudnotifier/api/ginresp"
2022-11-18 21:25:40 +01:00
"blackforestbytes.com/simplecloudnotifier/db"
2023-01-13 17:17:17 +01:00
"blackforestbytes.com/simplecloudnotifier/models"
2022-11-18 21:25:40 +01:00
"context"
"errors"
2022-11-20 17:19:11 +01:00
"github.com/gin-gonic/gin"
2022-11-20 03:06:08 +01:00
"github.com/rs/zerolog/log"
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
"gogs.mikescher.com/BlackForestBytes/goext/sq"
2022-11-18 21:25:40 +01:00
"time"
)
type TxContext interface {
Deadline() (deadline time.Time, ok bool)
Done() <-chan struct{}
Err() error
Value(key any) any
GetOrCreateTransaction(db db.DatabaseImpl) (sq.Tx, error)
}
2022-11-18 21:25:40 +01:00
type AppContext struct {
2023-04-21 21:45:16 +02:00
app *Application
2022-11-18 21:25:40 +01:00
inner context.Context
cancelFunc context.CancelFunc
cancelled bool
transaction sq.Tx
2023-01-13 17:17:17 +01:00
permissions models.PermissionSet
2022-11-20 17:19:11 +01:00
ginContext *gin.Context
2022-11-18 23:12:37 +01:00
}
2023-04-21 21:45:16 +02:00
func CreateAppContext(app *Application, g *gin.Context, innerCtx context.Context, cancelFn context.CancelFunc) *AppContext {
2022-11-18 23:12:37 +01:00
return &AppContext{
2023-04-21 21:45:16 +02:00
app: app,
2022-11-18 23:12:37 +01:00
inner: innerCtx,
cancelFunc: cancelFn,
cancelled: false,
transaction: nil,
2023-01-13 17:17:17 +01:00
permissions: models.NewEmptyPermissions(),
2022-11-20 17:19:11 +01:00
ginContext: g,
2022-11-18 23:12:37 +01:00
}
2022-11-18 21:25:40 +01:00
}
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 {
2022-11-20 17:19:11 +01:00
log.Error().Str("uri", ac.RequestURI()).Msg("Rollback transaction (ctx-cancel)")
2022-11-18 21:25:40 +01:00
err := ac.transaction.Rollback()
if err != nil {
2022-11-20 17:19:11 +01:00
log.Err(err).Stack().Msg("Failed to rollback transaction")
2022-11-18 21:25:40 +01:00
}
ac.transaction = nil
}
if ac.cancelFunc != nil {
ac.cancelFunc()
}
2022-11-18 21:25:40 +01:00
}
2022-11-20 17:19:11 +01:00
func (ac *AppContext) RequestURI() string {
if ac.ginContext != nil && ac.ginContext.Request != nil {
return ac.ginContext.Request.Method + " :: " + ac.ginContext.Request.RequestURI
} else {
return ""
}
}
func (ac *AppContext) _FinishSuccess(res ginext.HTTPResponse) ginext.HTTPResponse {
2022-11-18 21:25:40 +01:00
if ac.cancelled {
panic("Cannot finish a cancelled request")
}
if ac.transaction != nil {
err := ac.transaction.Commit()
if err != nil {
2022-11-20 20:34:18 +01:00
return ginresp.APIError(ac.ginContext, 500, apierr.COMMIT_FAILED, "Failed to comit changes to DB", err)
2022-11-18 21:25:40 +01:00
}
ac.transaction = nil
}
return res
}
func (ac *AppContext) GetOrCreateTransaction(db db.DatabaseImpl) (sq.Tx, error) {
2022-11-18 21:25:40 +01:00
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
}