Made auth middleware turn skip timeout if maintenance mode is on
This commit is contained in:
@@ -11,7 +11,6 @@ import (
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
// A helper function to create a transaction with a cancellable context.
|
||||
func WithTransaction(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
@@ -24,8 +23,7 @@ func WithTransaction(
|
||||
r *http.Request,
|
||||
),
|
||||
) {
|
||||
// Create a cancellable context from the request context
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 2*time.Second)
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 15*time.Second)
|
||||
defer cancel()
|
||||
|
||||
// Start the transaction
|
||||
@@ -41,6 +39,5 @@ func WithTransaction(
|
||||
return
|
||||
}
|
||||
|
||||
// Pass the context and transaction to the handler
|
||||
handler(ctx, tx, w, r)
|
||||
}
|
||||
|
||||
2
main.go
2
main.go
@@ -120,7 +120,7 @@ func run(ctx context.Context, w io.Writer, args map[string]string) error {
|
||||
return errors.Wrap(err, "getStaticFiles")
|
||||
}
|
||||
|
||||
srv := server.NewServer(config, logger, conn, &staticFS)
|
||||
srv := server.NewServer(config, logger, conn, &staticFS, &maint)
|
||||
httpServer := &http.Server{
|
||||
Addr: net.JoinHostPort(config.Host, config.Port),
|
||||
Handler: srv,
|
||||
|
||||
@@ -3,13 +3,13 @@ package middleware
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"projectreshoot/config"
|
||||
"projectreshoot/contexts"
|
||||
"projectreshoot/cookies"
|
||||
"projectreshoot/db"
|
||||
"projectreshoot/handlers"
|
||||
"projectreshoot/jwt"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
@@ -98,6 +98,7 @@ func Authentication(
|
||||
config *config.Config,
|
||||
conn *db.SafeConn,
|
||||
next http.Handler,
|
||||
maint *uint32,
|
||||
) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/static/css/output.css" ||
|
||||
@@ -105,8 +106,21 @@ func Authentication(
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
handlers.WithTransaction(w, r, logger, conn,
|
||||
func(ctx context.Context, tx *db.SafeTX, w http.ResponseWriter, r *http.Request) {
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 15*time.Second)
|
||||
defer cancel()
|
||||
if atomic.LoadUint32(maint) == 1 {
|
||||
cancel()
|
||||
}
|
||||
|
||||
// Start the transaction
|
||||
tx, err := conn.Begin(ctx)
|
||||
if err != nil {
|
||||
// Failed to start transaction, warn the user they cant login right now
|
||||
logger.Warn().Err(err).Msg("Request failed to start a transaction")
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
user, err := getAuthenticatedUser(config, ctx, tx, w, r)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
@@ -124,7 +138,5 @@ func Authentication(
|
||||
uctx := contexts.SetUser(r.Context(), user)
|
||||
newReq := r.WithContext(uctx)
|
||||
next.ServeHTTP(w, newReq)
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ func NewServer(
|
||||
logger *zerolog.Logger,
|
||||
conn *db.SafeConn,
|
||||
staticFS *http.FileSystem,
|
||||
maint *uint32,
|
||||
) http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
addRoutes(
|
||||
@@ -29,7 +30,7 @@ func NewServer(
|
||||
// Add middleware here, must be added in reverse order of execution
|
||||
// i.e. First in list will get executed last during the request handling
|
||||
handler = middleware.Logging(logger, handler)
|
||||
handler = middleware.Authentication(logger, config, conn, handler)
|
||||
handler = middleware.Authentication(logger, config, conn, handler, maint)
|
||||
|
||||
// Gzip
|
||||
handler = middleware.Gzip(handler, config.GZIP)
|
||||
|
||||
Reference in New Issue
Block a user