Added 503 error page and streamlined error paging
This commit is contained in:
24
handlers/errorpage.go
Normal file
24
handlers/errorpage.go
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"projectreshoot/view/page"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ErrorPage(
|
||||||
|
errorCode int,
|
||||||
|
w http.ResponseWriter,
|
||||||
|
r *http.Request,
|
||||||
|
) {
|
||||||
|
message := map[int]string{
|
||||||
|
401: "You need to login to view this page.",
|
||||||
|
403: "You do not have permission to view this page.",
|
||||||
|
404: "The page or resource you have requested does not exist.",
|
||||||
|
500: `An error occured on the server. Please try again, and if this
|
||||||
|
continues to happen contact an administrator.`,
|
||||||
|
503: "The server is currently down for maintenance and should be back soon. =)",
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusUnauthorized)
|
||||||
|
page.Error(errorCode, http.StatusText(errorCode), message[errorCode]).
|
||||||
|
Render(r.Context(), w)
|
||||||
|
}
|
||||||
@@ -12,11 +12,7 @@ func HandleRoot() http.Handler {
|
|||||||
return http.HandlerFunc(
|
return http.HandlerFunc(
|
||||||
func(w http.ResponseWriter, r *http.Request) {
|
func(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.URL.Path != "/" {
|
if r.URL.Path != "/" {
|
||||||
page.Error(
|
ErrorPage(http.StatusNotFound, w, r)
|
||||||
"404",
|
|
||||||
"Page not found",
|
|
||||||
"The page or resource you have requested does not exist",
|
|
||||||
).Render(r.Context(), w)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
page.Index().Render(r.Context(), w)
|
page.Index().Render(r.Context(), w)
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ func HandleLoginRequest(
|
|||||||
) http.Handler {
|
) http.Handler {
|
||||||
return http.HandlerFunc(
|
return http.HandlerFunc(
|
||||||
func(w http.ResponseWriter, r *http.Request) {
|
func(w http.ResponseWriter, r *http.Request) {
|
||||||
ctx, cancel := context.WithTimeout(r.Context(), 15*time.Second)
|
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
// Start the transaction
|
// Start the transaction
|
||||||
|
|||||||
@@ -27,7 +27,6 @@ func CheckTokenNotRevoked(ctx context.Context, tx *db.SafeTX, t Token) (bool, er
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return false, errors.Wrap(err, "tx.Query")
|
return false, errors.Wrap(err, "tx.Query")
|
||||||
}
|
}
|
||||||
// NOTE: rows.Close()
|
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
revoked := rows.Next()
|
revoked := rows.Next()
|
||||||
return !revoked, nil
|
return !revoked, nil
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"projectreshoot/contexts"
|
"projectreshoot/contexts"
|
||||||
"projectreshoot/cookies"
|
"projectreshoot/cookies"
|
||||||
"projectreshoot/db"
|
"projectreshoot/db"
|
||||||
|
"projectreshoot/handlers"
|
||||||
"projectreshoot/jwt"
|
"projectreshoot/jwt"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
@@ -106,7 +107,7 @@ func Authentication(
|
|||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ctx, cancel := context.WithTimeout(r.Context(), 15*time.Second)
|
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
if atomic.LoadUint32(maint) == 1 {
|
if atomic.LoadUint32(maint) == 1 {
|
||||||
cancel()
|
cancel()
|
||||||
@@ -115,10 +116,10 @@ func Authentication(
|
|||||||
// Start the transaction
|
// Start the transaction
|
||||||
tx, err := conn.Begin(ctx)
|
tx, err := conn.Begin(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Failed to start transaction, send 503 code to client
|
// Failed to start transaction, skip auth
|
||||||
logger.Warn().Err(err).Msg("Skipping Auth - unable to start a transaction")
|
logger.Warn().Err(err).
|
||||||
w.WriteHeader(http.StatusServiceUnavailable)
|
Msg("Skipping Auth - unable to start a transaction")
|
||||||
next.ServeHTTP(w, r)
|
handlers.ErrorPage(http.StatusServiceUnavailable, w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
user, err := getAuthenticatedUser(config, ctx, tx, w, r)
|
user, err := getAuthenticatedUser(config, ctx, tx, w, r)
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package middleware
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"projectreshoot/contexts"
|
"projectreshoot/contexts"
|
||||||
|
"projectreshoot/handlers"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
@@ -30,7 +31,7 @@ func Logging(logger *zerolog.Logger, next http.Handler) http.Handler {
|
|||||||
}
|
}
|
||||||
start, err := contexts.GetStartTime(r.Context())
|
start, err := contexts.GetStartTime(r.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO: Handle failure here. internal server error maybe
|
handlers.ErrorPage(http.StatusInternalServerError, w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
wrapped := &wrappedWriter{
|
wrapped := &wrappedWriter{
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package middleware
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"projectreshoot/contexts"
|
"projectreshoot/contexts"
|
||||||
"projectreshoot/view/page"
|
"projectreshoot/handlers"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Checks if the user is set in the context and shows 401 page if not logged in
|
// 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) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
user := contexts.GetUser(r.Context())
|
user := contexts.GetUser(r.Context())
|
||||||
if user == nil {
|
if user == nil {
|
||||||
w.WriteHeader(http.StatusUnauthorized)
|
handlers.ErrorPage(http.StatusUnauthorized, w, r)
|
||||||
page.Error(
|
|
||||||
"401",
|
|
||||||
"Unauthorized",
|
|
||||||
"Please login to view this page",
|
|
||||||
).Render(r.Context(), w)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
package page
|
package page
|
||||||
|
|
||||||
import "projectreshoot/view/layout"
|
import "projectreshoot/view/layout"
|
||||||
|
import "strconv"
|
||||||
|
|
||||||
// Page template for Error pages. Error code should be a HTTP status code as
|
// Page template for Error pages. Error code should be a HTTP status code as
|
||||||
// a string, and err should be the corresponding response title.
|
// a string, and err should be the corresponding response title.
|
||||||
// Message is a custom error message displayed below the code and error.
|
// Message is a custom error message displayed below the code and error.
|
||||||
templ Error(code string, err string, message string) {
|
templ Error(code int, err string, message string) {
|
||||||
@layout.Global() {
|
@layout.Global() {
|
||||||
<div
|
<div
|
||||||
class="grid mt-24 left-0 right-0 top-0 bottom-0
|
class="grid mt-24 left-0 right-0 top-0 bottom-0
|
||||||
@@ -14,7 +15,7 @@ templ Error(code string, err string, message string) {
|
|||||||
<div class="text-center">
|
<div class="text-center">
|
||||||
<h1
|
<h1
|
||||||
class="text-9xl text-text"
|
class="text-9xl text-text"
|
||||||
>{ code }</h1>
|
>{ strconv.Itoa(code) }</h1>
|
||||||
<p
|
<p
|
||||||
class="text-2xl font-bold tracking-tight text-subtext1
|
class="text-2xl font-bold tracking-tight text-subtext1
|
||||||
sm:text-4xl"
|
sm:text-4xl"
|
||||||
|
|||||||
Reference in New Issue
Block a user