2022-11-18 21:25:40 +01:00
|
|
|
package logic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"blackforestbytes.com/simplecloudnotifier/api/apierr"
|
|
|
|
"blackforestbytes.com/simplecloudnotifier/common/ginresp"
|
|
|
|
"blackforestbytes.com/simplecloudnotifier/db"
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"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"
|
2022-11-18 21:25:40 +01:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AppContext struct {
|
|
|
|
inner context.Context
|
|
|
|
cancelFunc context.CancelFunc
|
|
|
|
cancelled bool
|
|
|
|
transaction *sql.Tx
|
2022-11-18 23:12:37 +01:00
|
|
|
permissions PermissionSet
|
2022-11-20 17:19:11 +01:00
|
|
|
ginContext *gin.Context
|
2022-11-18 23:12:37 +01:00
|
|
|
}
|
|
|
|
|
2022-11-20 17:19:11 +01:00
|
|
|
func CreateAppContext(g *gin.Context, innerCtx context.Context, cancelFn context.CancelFunc) *AppContext {
|
2022-11-18 23:12:37 +01:00
|
|
|
return &AppContext{
|
|
|
|
inner: innerCtx,
|
|
|
|
cancelFunc: cancelFn,
|
|
|
|
cancelled: false,
|
|
|
|
transaction: nil,
|
|
|
|
permissions: 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
|
|
|
|
}
|
|
|
|
ac.cancelFunc()
|
|
|
|
}
|
|
|
|
|
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 ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-18 21:25:40 +01:00
|
|
|
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 {
|
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.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
|
|
|
|
}
|