Organize route handlers

This commit is contained in:
2024-09-24 09:57:51 +07:00
parent 757dde58c0
commit db566fd9a4
16 changed files with 226 additions and 341 deletions

View File

@ -4,10 +4,10 @@ import (
"context"
"fmt"
"github.com/fossyy/filekeeper/app"
errorView "github.com/fossyy/filekeeper/view/client/error"
"net/http"
"strings"
errorHandler "github.com/fossyy/filekeeper/handler/error"
"github.com/fossyy/filekeeper/session"
"github.com/fossyy/filekeeper/utils"
)
@ -24,14 +24,24 @@ func (w *wrapper) WriteHeader(code int) {
if code == http.StatusNotFound {
w.Header().Set("Content-Type", "text/html")
w.ResponseWriter.WriteHeader(code)
errorHandler.NotFound(w.ResponseWriter, w.request)
component := errorView.NotFound("Not Found")
err := component.Render(w.request.Context(), w)
if err != nil {
app.Server.Logger.Error(err.Error())
return
}
return
}
if code == http.StatusInternalServerError {
w.Header().Set("Content-Type", "text/html")
w.ResponseWriter.WriteHeader(code)
errorHandler.InternalServerError(w.ResponseWriter, w.request)
component := errorView.InternalServerError("Internal Server Error")
err := component.Render(w.request.Context(), w)
if err != nil {
app.Server.Logger.Error(err.Error())
return
}
return
}
w.ResponseWriter.WriteHeader(code)
@ -80,78 +90,82 @@ func Handler(next http.Handler) http.Handler {
})
}
func Auth(next http.HandlerFunc, w http.ResponseWriter, r *http.Request) {
status, user, sessionID := session.GetSession(r)
func Auth(next http.HandlerFunc) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
status, user, sessionID := session.GetSession(r)
switch status {
case session.Authorized:
ctx := context.WithValue(r.Context(), "user", user)
ctx = context.WithValue(ctx, "sessionID", sessionID)
req := r.WithContext(ctx)
next.ServeHTTP(w, req)
return
case session.Unauthorized:
if r.RequestURI != "/logout" {
switch status {
case session.Authorized:
ctx := context.WithValue(r.Context(), "user", user)
ctx = context.WithValue(ctx, "sessionID", sessionID)
req := r.WithContext(ctx)
next.ServeHTTP(w, req)
return
case session.Unauthorized:
if r.RequestURI != "/logout" {
http.SetCookie(w, &http.Cookie{
Name: "redirect",
Value: r.RequestURI,
Path: "/",
})
}
http.Redirect(w, r, "/signin", http.StatusSeeOther)
return
case session.InvalidSession:
http.SetCookie(w, &http.Cookie{
Name: "redirect",
Value: r.RequestURI,
Path: "/",
Name: "Session",
Value: "",
Path: "/",
MaxAge: -1,
})
}
http.Redirect(w, r, "/signin", http.StatusSeeOther)
return
case session.InvalidSession:
http.SetCookie(w, &http.Cookie{
Name: "Session",
Value: "",
Path: "/",
MaxAge: -1,
})
http.Redirect(w, r, "/signin", http.StatusSeeOther)
return
case session.Suspicious:
userSession := session.Get(sessionID)
err := userSession.Delete()
if err != nil {
app.Server.Logger.Error(err)
}
err = session.RemoveSessionInfo(user.Email, sessionID)
if err != nil {
app.Server.Logger.Error(err)
http.Redirect(w, r, "/signin", http.StatusSeeOther)
return
case session.Suspicious:
userSession := session.Get(sessionID)
err := userSession.Delete()
if err != nil {
app.Server.Logger.Error(err)
}
err = session.RemoveSessionInfo(user.Email, sessionID)
if err != nil {
app.Server.Logger.Error(err)
return
}
http.SetCookie(w, &http.Cookie{
Name: "Session",
Value: "",
Path: "/",
MaxAge: -1,
})
http.Redirect(w, r, "/signin?error=suspicious_session", http.StatusSeeOther)
return
default:
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
http.SetCookie(w, &http.Cookie{
Name: "Session",
Value: "",
Path: "/",
MaxAge: -1,
})
http.Redirect(w, r, "/signin?error=suspicious_session", http.StatusSeeOther)
return
default:
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
})
}
func Guest(next http.HandlerFunc, w http.ResponseWriter, r *http.Request) {
status, _, _ := session.GetSession(r)
func Guest(next http.HandlerFunc) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
status, _, _ := session.GetSession(r)
switch status {
case session.Authorized:
http.Redirect(w, r, "/", http.StatusSeeOther)
return
case session.Unauthorized:
next.ServeHTTP(w, r)
return
case session.InvalidSession:
http.SetCookie(w, &http.Cookie{
Name: "Session",
Value: "",
Path: "/",
MaxAge: -1,
})
next.ServeHTTP(w, r)
return
}
switch status {
case session.Authorized:
http.Redirect(w, r, "/", http.StatusSeeOther)
return
case session.Unauthorized:
next.ServeHTTP(w, r)
return
case session.InvalidSession:
http.SetCookie(w, &http.Cookie{
Name: "Session",
Value: "",
Path: "/",
MaxAge: -1,
})
next.ServeHTTP(w, r)
return
}
})
}