Cleaned up middleware/route handlers

This commit is contained in:
2025-02-22 20:34:09 +11:00
parent 0a3796914f
commit f34c1c11aa
18 changed files with 58 additions and 96 deletions

View File

@@ -10,7 +10,7 @@ import (
"projectreshoot/contexts"
"projectreshoot/cookies"
"projectreshoot/db"
"projectreshoot/handlers"
"projectreshoot/handler"
"projectreshoot/jwt"
"github.com/pkg/errors"
@@ -119,7 +119,7 @@ func Authentication(
// Failed to start transaction, skip auth
logger.Warn().Err(err).
Msg("Skipping Auth - unable to start a transaction")
handlers.ErrorPage(http.StatusServiceUnavailable, w, r)
handler.ErrorPage(http.StatusServiceUnavailable, w, r)
return
}
user, err := getAuthenticatedUser(config, ctx, tx, w, r)

View File

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

View File

@@ -3,15 +3,15 @@ package middleware
import (
"net/http"
"projectreshoot/contexts"
"projectreshoot/handlers"
"projectreshoot/handler"
)
// Checks if the user is set in the context and shows 401 page if not logged in
func RequiresLogin(next http.Handler) http.Handler {
func LoginReq(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user := contexts.GetUser(r.Context())
if user == nil {
handlers.ErrorPage(http.StatusUnauthorized, w, r)
handler.ErrorPage(http.StatusUnauthorized, w, r)
return
}
next.ServeHTTP(w, r)
@@ -20,7 +20,7 @@ func RequiresLogin(next http.Handler) http.Handler {
// Checks if the user is set in the context and redirects them to profile if
// they are logged in
func RequiresLogout(next http.Handler) http.Handler {
func LogoutReq(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user := contexts.GetUser(r.Context())
if user != nil {

View File

@@ -33,7 +33,7 @@ func TestPageLoginRequired(t *testing.T) {
var maint uint32
atomic.StoreUint32(&maint, 0)
// Add the middleware and create the server
loginRequiredHandler := RequiresLogin(testHandler)
loginRequiredHandler := LoginReq(testHandler)
authHandler := Authentication(logger, cfg, sconn, loginRequiredHandler, &maint)
server := httptest.NewServer(authHandler)
defer server.Close()

View File

@@ -6,7 +6,7 @@ import (
"time"
)
func RequiresFresh(
func FreshReq(
next http.Handler,
) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

View File

@@ -33,8 +33,8 @@ func TestReauthRequired(t *testing.T) {
var maint uint32
atomic.StoreUint32(&maint, 0)
// Add the middleware and create the server
reauthRequiredHandler := RequiresFresh(testHandler)
loginRequiredHandler := RequiresLogin(reauthRequiredHandler)
reauthRequiredHandler := FreshReq(testHandler)
loginRequiredHandler := LoginReq(reauthRequiredHandler)
authHandler := Authentication(logger, cfg, sconn, loginRequiredHandler, &maint)
server := httptest.NewServer(authHandler)
defer server.Close()