Update authentication, reauth, logout to use new transactions
This commit is contained in:
47
handlers/withtransaction.go
Normal file
47
handlers/withtransaction.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"projectreshoot/db"
|
||||
"projectreshoot/view/page"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
// A helper function to create a transaction with a cancellable context.
|
||||
func WithTransaction(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
logger *zerolog.Logger,
|
||||
conn *db.SafeConn,
|
||||
handler func(
|
||||
ctx context.Context,
|
||||
tx *db.SafeTX,
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
),
|
||||
) {
|
||||
// Create a cancellable context from the request context
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
// Start the transaction
|
||||
tx, err := conn.Begin(ctx)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
logger.Warn().Err(err).Msg("Request failed to start a transaction")
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
page.Error(
|
||||
"503",
|
||||
http.StatusText(503),
|
||||
"This service is currently unavailable. It could be down for maintenance").
|
||||
Render(r.Context(), w)
|
||||
return
|
||||
}
|
||||
|
||||
// Pass the context and transaction to the handler
|
||||
handler(ctx, tx, w, r)
|
||||
}
|
||||
Reference in New Issue
Block a user