Added 503 error page and streamlined error paging

This commit is contained in:
2025-02-18 21:37:15 +11:00
parent 1f7a9e08e6
commit 90bf997578
8 changed files with 39 additions and 22 deletions

View File

@@ -10,6 +10,7 @@ import (
"projectreshoot/contexts"
"projectreshoot/cookies"
"projectreshoot/db"
"projectreshoot/handlers"
"projectreshoot/jwt"
"github.com/pkg/errors"
@@ -106,7 +107,7 @@ func Authentication(
next.ServeHTTP(w, r)
return
}
ctx, cancel := context.WithTimeout(r.Context(), 15*time.Second)
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
defer cancel()
if atomic.LoadUint32(maint) == 1 {
cancel()
@@ -115,10 +116,10 @@ func Authentication(
// Start the transaction
tx, err := conn.Begin(ctx)
if err != nil {
// Failed to start transaction, send 503 code to client
logger.Warn().Err(err).Msg("Skipping Auth - unable to start a transaction")
w.WriteHeader(http.StatusServiceUnavailable)
next.ServeHTTP(w, r)
// Failed to start transaction, skip auth
logger.Warn().Err(err).
Msg("Skipping Auth - unable to start a transaction")
handlers.ErrorPage(http.StatusServiceUnavailable, w, r)
return
}
user, err := getAuthenticatedUser(config, ctx, tx, w, r)

View File

@@ -3,6 +3,7 @@ package middleware
import (
"net/http"
"projectreshoot/contexts"
"projectreshoot/handlers"
"time"
"github.com/rs/zerolog"
@@ -30,7 +31,7 @@ func Logging(logger *zerolog.Logger, next http.Handler) http.Handler {
}
start, err := contexts.GetStartTime(r.Context())
if err != nil {
// TODO: Handle failure here. internal server error maybe
handlers.ErrorPage(http.StatusInternalServerError, w, r)
return
}
wrapped := &wrappedWriter{

View File

@@ -3,7 +3,7 @@ package middleware
import (
"net/http"
"projectreshoot/contexts"
"projectreshoot/view/page"
"projectreshoot/handlers"
)
// Checks if the user is set in the context and shows 401 page if not logged in
@@ -11,12 +11,7 @@ func RequiresLogin(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user := contexts.GetUser(r.Context())
if user == nil {
w.WriteHeader(http.StatusUnauthorized)
page.Error(
"401",
"Unauthorized",
"Please login to view this page",
).Render(r.Context(), w)
handlers.ErrorPage(http.StatusUnauthorized, w, r)
return
}
next.ServeHTTP(w, r)