Added error 503 popup

This commit is contained in:
2025-02-18 20:51:22 +11:00
parent 0ece08726d
commit 1f7a9e08e6
9 changed files with 291 additions and 177 deletions

View File

@@ -3,6 +3,7 @@ package handlers
import (
"context"
"net/http"
"time"
"projectreshoot/contexts"
"projectreshoot/cookies"
@@ -47,8 +48,16 @@ func HandleChangeUsername(
) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
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()
// Start the transaction
tx, err := conn.Begin(ctx)
if err != nil {
logger.Warn().Err(err).Msg("Error updating username")
w.WriteHeader(http.StatusServiceUnavailable)
return
}
r.ParseForm()
newUsername := r.FormValue("username")
unique, err := db.CheckUsernameUnique(ctx, tx, newUsername)
@@ -76,8 +85,6 @@ func HandleChangeUsername(
w.Header().Set("HX-Refresh", "true")
},
)
},
)
}
// Handles a request to change the users bio
@@ -87,8 +94,16 @@ func HandleChangeBio(
) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
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()
// Start the transaction
tx, err := conn.Begin(ctx)
if err != nil {
logger.Warn().Err(err).Msg("Error updating bio")
w.WriteHeader(http.StatusServiceUnavailable)
return
}
r.ParseForm()
newBio := r.FormValue("bio")
leng := len([]rune(newBio))
@@ -99,7 +114,7 @@ func HandleChangeBio(
return
}
user := contexts.GetUser(r.Context())
err := user.ChangeBio(ctx, tx, newBio)
err = user.ChangeBio(ctx, tx, newBio)
if err != nil {
tx.Rollback()
logger.Error().Err(err).Msg("Error updating bio")
@@ -110,8 +125,6 @@ func HandleChangeBio(
w.Header().Set("HX-Refresh", "true")
},
)
},
)
}
func validateChangePassword(
ctx context.Context,
@@ -137,8 +150,16 @@ func HandleChangePassword(
) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
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()
// Start the transaction
tx, err := conn.Begin(ctx)
if err != nil {
logger.Warn().Err(err).Msg("Error updating password")
w.WriteHeader(http.StatusServiceUnavailable)
return
}
newPass, err := validateChangePassword(ctx, tx, r)
if err != nil {
tx.Rollback()
@@ -157,6 +178,4 @@ func HandleChangePassword(
w.Header().Set("HX-Refresh", "true")
},
)
},
)
}

View File

@@ -3,6 +3,7 @@ package handlers
import (
"context"
"net/http"
"time"
"projectreshoot/config"
"projectreshoot/cookies"
@@ -55,8 +56,16 @@ func HandleLoginRequest(
) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
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()
// Start the transaction
tx, err := conn.Begin(ctx)
if err != nil {
logger.Warn().Err(err).Msg("Failed to set token cookies")
w.WriteHeader(http.StatusServiceUnavailable)
return
}
r.ParseForm()
user, err := validateLogin(ctx, tx, r)
if err != nil {
@@ -82,7 +91,6 @@ func HandleLoginRequest(
tx.Commit()
pageFrom := cookies.CheckPageFrom(w, r)
w.Header().Set("HX-Redirect", pageFrom)
})
},
)
}

View File

@@ -4,6 +4,7 @@ import (
"context"
"net/http"
"strings"
"time"
"projectreshoot/config"
"projectreshoot/cookies"
@@ -86,9 +87,17 @@ func HandleLogout(
) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
WithTransaction(w, r, logger, conn,
func(ctx context.Context, tx *db.SafeTX, w http.ResponseWriter, r *http.Request) {
err := revokeTokens(config, ctx, tx, r)
ctx, cancel := context.WithTimeout(r.Context(), 15*time.Second)
defer cancel()
// Start the transaction
tx, err := conn.Begin(ctx)
if err != nil {
logger.Warn().Err(err).Msg("Error occured on user logout")
w.WriteHeader(http.StatusServiceUnavailable)
return
}
err = revokeTokens(config, ctx, tx, r)
if err != nil {
tx.Rollback()
logger.Error().Err(err).Msg("Error occured on user logout")
@@ -99,7 +108,6 @@ func HandleLogout(
cookies.DeleteCookie(w, "access", "/")
cookies.DeleteCookie(w, "refresh", "/")
w.Header().Set("HX-Redirect", "/login")
})
},
)
}

View File

@@ -3,6 +3,7 @@ package handlers
import (
"context"
"net/http"
"time"
"projectreshoot/config"
"projectreshoot/contexts"
@@ -105,9 +106,17 @@ func HandleReauthenticate(
) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
WithTransaction(w, r, logger, conn,
func(ctx context.Context, tx *db.SafeTX, w http.ResponseWriter, r *http.Request) {
err := validatePassword(r)
ctx, cancel := context.WithTimeout(r.Context(), 15*time.Second)
defer cancel()
// Start the transaction
tx, err := conn.Begin(ctx)
if err != nil {
logger.Warn().Err(err).Msg("Failed to refresh user tokens")
w.WriteHeader(http.StatusServiceUnavailable)
return
}
err = validatePassword(r)
if err != nil {
tx.Rollback()
w.WriteHeader(445)
@@ -123,7 +132,6 @@ func HandleReauthenticate(
}
tx.Commit()
w.WriteHeader(http.StatusOK)
})
},
)
}

View File

@@ -3,6 +3,7 @@ package handlers
import (
"context"
"net/http"
"time"
"projectreshoot/config"
"projectreshoot/cookies"
@@ -50,8 +51,16 @@ func HandleRegisterRequest(
) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
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()
// Start the transaction
tx, err := conn.Begin(ctx)
if err != nil {
logger.Warn().Err(err).Msg("Failed to set token cookies")
w.WriteHeader(http.StatusServiceUnavailable)
return
}
r.ParseForm()
user, err := validateRegistration(ctx, tx, r)
if err != nil {
@@ -80,8 +89,6 @@ func HandleRegisterRequest(
w.Header().Set("HX-Redirect", pageFrom)
},
)
},
)
}
// Handles a request to view the login page. Will attempt to set "pagefrom"

View File

@@ -6,12 +6,11 @@ import (
"time"
"projectreshoot/db"
"projectreshoot/view/page"
"github.com/rs/zerolog"
)
func WithTransaction(
func removeme(
w http.ResponseWriter,
r *http.Request,
logger *zerolog.Logger,
@@ -22,6 +21,7 @@ func WithTransaction(
w http.ResponseWriter,
r *http.Request,
),
onfail func(err error),
) {
ctx, cancel := context.WithTimeout(r.Context(), 15*time.Second)
defer cancel()
@@ -29,13 +29,7 @@ func WithTransaction(
// Start the transaction
tx, err := conn.Begin(ctx)
if err != nil {
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)
onfail(err)
return
}

View File

@@ -1,9 +1,9 @@
package popup
templ ErrorPopup() {
templ Error500Popup() {
<div
x-cloak
x-show="showError"
x-show="showError500"
class="absolute w-82 left-0 right-0 mt-20 mr-5 ml-auto"
x-transition:enter="transform translate-x-[100%] opacity-0 duration-200"
x-transition:enter-start="opacity-0 translate-x-[100%]"
@@ -44,7 +44,7 @@ templ ErrorPopup() {
stroke-width="1.5"
stroke="currentColor"
class="size-6 text-subtext0 hover:cursor-pointer"
@click="showError=false"
@click="showError500=false"
>
<path
stroke-linecap="round"

View File

@@ -0,0 +1,63 @@
package popup
templ Error503Popup() {
<div
x-cloak
x-show="showError503"
class="absolute w-82 left-0 right-0 mt-20 mr-5 ml-auto"
x-transition:enter="transform translate-x-[100%] opacity-0 duration-200"
x-transition:enter-start="opacity-0 translate-x-[100%]"
x-transition:enter-end="opacity-100 translate-x-0"
x-transition:leave="opacity-0 duration-200"
x-transition:leave-start="opacity-100 translate-x-0"
x-transition:leave-end="opacity-0 translate-x-[100%]"
>
<div
role="alert"
class="rounded-sm bg-dark-red p-4"
>
<div class="flex justify-between">
<div class="flex items-center gap-2 text-red w-fit">
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="currentColor"
class="size-5"
>
<path
fill-rule="evenodd"
d="M9.401 3.003c1.155-2 4.043-2 5.197 0l7.355
12.748c1.154 2-.29 4.5-2.599 4.5H4.645c-2.309
0-3.752-2.5-2.598-4.5L9.4 3.003zM12 8.25a.75.75
0 01.75.75v3.75a.75.75 0 01-1.5 0V9a.75.75 0
01.75-.75zm0 8.25a.75.75 0 100-1.5.75.75 0 000 1.5z"
clip-rule="evenodd"
></path>
</svg>
<strong class="block font-medium">Service Unavailable</strong>
</div>
<div class="flex">
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="size-6 text-subtext0 hover:cursor-pointer"
@click="showError503=false"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M6 18L18 6M6 6l12 12"
></path>
</svg>
</div>
</div>
<p class="mt-2 text-sm text-red">
The service is currently available. It could be down for maintenance.
Please try again later.
</p>
</div>
</div>
}

View File

@@ -41,11 +41,12 @@ templ Global() {
<script src="https://unpkg.com/alpinejs" defer></script>
<script>
// uncomment this line to enable logging of htmx events
// htmx.logAll();
htmx.logAll();
</script>
<script>
const bodyData = {
showError: false,
showError500: false,
showError503: false,
showConfirmPasswordModal: false,
handleHtmxBeforeOnLoad(event) {
const requestPath = event.detail.pathInfo.requestPath;
@@ -65,8 +66,13 @@ templ Global() {
// internal server error
if (errorCode.includes('Code 500')) {
this.showError = true;
setTimeout(() => this.showError = false, 6000);
this.showError500 = true;
setTimeout(() => this.showError500 = false, 6000);
}
// service not available error
if (errorCode.includes('Code 503')) {
this.showError503 = true;
setTimeout(() => this.showError503 = false, 6000);
}
// user is authorized but needs to refresh their login
@@ -83,7 +89,8 @@ templ Global() {
x-on:htmx:error="handleHtmxError($event)"
x-on:htmx:before-on-load="handleHtmxBeforeOnLoad($event)"
>
@popup.ErrorPopup()
@popup.Error500Popup()
@popup.Error503Popup()
@popup.ConfirmPasswordModal()
<div
id="main-content"